Python SMTP Verify email - python

import smtplib
try:
server = smtplib.SMTP("smtp.gmail.com:587")
server.ehlo()
server.starttls()
server.login("MY USER", "MY PASS")
message = "Hi, recruit?"
server.sendmail("FROME WHO", "TO WHOM", message)
server.quit()
print("email is valid")
except:
print("email invalid")
Goal: I need to verify if the email I'm sending does exist. I would like the script to give me a traceback if the email doesn't exist.
Problem: The email get's sent even when the email doesn't exist, I get to receive email from google though saying that the email doesn't exist in my Gmail.
steps taken: I've tried the instance method SMTP.verify which would send me a status code of 404 if the email doesn't exist, but the email is getting sent even though it doesn't exist.
Question: is there a setting in Gmail where I can change to say if an email doesn't exist, please give me an error. Or maybe there's a workaround on my script that I didn't know yet?
This is the code for verify and also the return
https://i.stack.imgur.com/MTZJE.png

Related

Any idea of how to spoof an email sender in python using smtplib?

I was wasting another day of my life messing around with python and I was thinking about how to spoof an email address using smtplib in python. I thought it could be possible to spoof the from address by using the code below but it didn't work. I have seen projects on github that seem to be able to actually spoof the email but if i were to create a program of my own that does this, how exactly would it work? Any ideas? Code:
import smtplib
username = (mygmailusername)
password = (mypassword)
fakefrom = "donaldtrump#gmail.com"
toEmail = (toAddress)
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login(username,password)
server.sendmail(fakefrom,toEmail,"this is the fbi. OPEN UP")
server.close()
Most providers check the account with the sender address, and disallow if the sender address dosn't match with sender. Try if you have a setup box network, to use your provider network smtp server without authentification on smtp 25 port, and most of time the mail can be sent. But with that method, many receiver servers will tag directly this mail as spam/dangerous. You can try that directly in your message tool (outlook,...) it s the same behavior as you code.
Sorry I'm a bit late. The problem, here lies with the second-to-last line:
server.sendmail(fakefrom,toEmail,"this is the fbi. OPEN UP")
You are trying to use the 'fake from' address to actually send the email which isn't what you want to be doing. You just want to 'spoof' it and make the recipient think that the email came from a different address. You do that by defining the sender details in the message body.
The code that you would need to use to make this work would be:
import smtplib
username = (mygmailusername)
password = (mypassword)
fake_from = "donaldtrump#gmail.com"
fake_name = "Donald Trump"
to_email = (toAddress)
to_name = (toName)
subject = "Bonjour"
content = "This is the fbi. OPEN UP"
message = f"From: {fake_name} <{fake_from}>\nTo: {to_name} <{to_email}>\nSubject: {subject}\n\n{content}"
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(username, password)
server.sendmail(username, to_email, message.encode())
server.close()

Python sendmail() returns {} despite including emails that don't exist

I was experimenting sending emails using python and smtplib. I am aware that if the email was sent to all email addresses then sendmail() returns {}. But I intentionally added 2 email addresses (bbbbbbbbbb#gmail.com and ccccccccccccc#gmail.com) that do not exist (I confirmed they don't exist by checking sent items in my gmail account) and still got an empty dictionary. Is something going wrong here?
import smtplib
smtpobj=smtplib.SMTP('smtp.gmail.com',587) #creating an SMTP object.
smtpobj.ehlo() #saying hello to the server
smtpobj.starttls() #enabling encryption for the connection
password=str(input("Please enter your password: "))
smtpobj.login('thereallasad#gmail.com', password) #logging in to account
lst=['govindsomadas#gmail.com', 'thereallasad#gmail.com', 'bbbbbbbbbbbbbbbbbb#gmail.com','ccccccccccccccccc#gmail.com']
smtpobj.sendmail('thereallasad#gmail.com', lst, 'Subject: Testing\nDear Govind,\n\nThanks for creating me.\n\nRegards,\nLasad')
print(smtpobj.sendmail('thereallasad#gmail.com', lst, 'Subject: Testing\nDear Govind,\n\nThanks for creating me.\n\nRegards,\nLasad'))
smtpobj.quit() #disconnects from smtp server
It will return an empty dictionary if even one of the email addresses worked, if none works then it raises an exception.
Try sending an email to all the wrong emails.

sending emails with smtplib in python from shared mailbox

I am working on script to send emails.
Emails are supposed to be send from a shared mailbox in outlook.
This shared mailbox doesn't have a password and here is the trick: on the one hand I need the authenticate and provide a password, on the other hand I have no password to provide.
I ran the scripted with my professional outlook mailbox (email address + password) and it works.
Here is my config:
MY_ADDRESS = "---emailaddressofthesender---"
PASSWORD = ""
MESSAGE = "some message"
# set up the SMTP server
s = smtplib.SMTP(host='smtp-mail.outlook.com', port=587)
s.starttls()
s.login(MY_ADDRESS, PASSWORD)
msg = MIMEMultipart() # create a message
# setup the parameters of the message
msg['From']=MY_ADDRESS
msg['To']="---emailaddresseofthereceiver---"
msg['Subject']="This is TEST"
# add in the message body
msg.attach(MIMEText(MESSAGE, 'plain'))
# send the message via the server set up earlier.
s.send_message(msg)
del msg
# Terminate the SMTP session and close the connection
s.quit()
I tried:
Read the doc smtplib. Unless mistaken, I found no mentions about
authentications without password.
PASSWORD = "": authentication error
PASSWORD = None: authentication error
s.login(MY_ADDRESS): PASSWORD argument is missing
remove s.login() method from the script: Client was not authenticated to send anonymous mail during MAIL FROM [PR0P264CA0184.FRAP264.PROD.OUTLOOK.COM]
The only working solution I have now is to send from my mailbox credentials and not from the shared mailbox. For the sake of this project, this solution is not acceptable.
Many thanks in advance for your help
Add this right before the s.send() step and it will work.
s.SentOnBehalfOfName = 'SharedFolder'

Is there any other way to google sign in without google smtp?

I have a flask application which will send emails to the users who will register with their email. I have written the following code and switched on to access less secure app in gmail account.
import smtplib
def send_email():
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('Sender's mail id', 'Sender's mail password')
message = 'sending this from python!'
server.sendmail('Sender's mail id', 'Receiver's mail id', message)
server.quit()
send_email()
The following error I am getting for this when send_email() is being called.
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password
not accepted. Learn more at\n5.7.8
https://support.google.com/mail/?p=BadCredentials z10sm10066531pfa.184
- gsmtp')
I later discovered that though I've switched on to access less secure app it remains switched off internally and after some moment on reloading the settings it's shows switched off.
Please help me out I am stuck with my project.

Can't send email with smtp

I want to know why this code can't send email.
import smtplib
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('surapon#gmail.com','222222')
mail.sendmail('surapon#gmail.com','youremail#gmail.com',content)
mail.quit
It shows:
SMTPAuthenticationError: (535, '5.7.8 Username and Password not accepted. Learn more at\n5.7.8 support.google.com/mail/answer/14257 ho10sm6301275pbc.27 - gsmtp')
Later, it shows:
SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbu87\n5.7.14 wdTx8uq_F_WXKXEVia5I3DTMdhzuJL967nviDbOqgBU9lHzjzIHX69az6PFAzff6lA2uGJ\n5.7.14 qCqwJzys1OcoqMzMNUx5o5ja_a3XHatcxE-jqsHjqWCwYR1WVUEmBfGvUIBzgm7iUyGOXq\n5.7.14 RdYOqEx5GLAe05yUhGq-z-JphFKH-x-aA0TwEc-hyEnecghY1ZLtMMsowPhFGa1XGPnNO3\n5.7.14 8XE4yhQctKtYySbTSiQqBUmmV4qE> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 support.google.com/mail/answer/78754 eu5sm6412101pac.37 - gsmtp')
I created yagmail as a package to make it really simple to send emails.
Please try the following:
import yagmail
yag = yagmail.SMTP('surapon#gmail.com', 'password')
yag.send('youremail#gmail.com', subject = 'hi', contents = content)
There are some useful other tricks you can do with the package, such as never having to enter the password again (while still being secure), and it makes it extremely easy to send attachments.
Install with either
pip install yagmail # python 2
pip3 install yagmail # python 3
and for more information please have a look at github.
Due to security issues gmail blocks accessing mail via code or program
But still you can use gmail to send mail via code if you do the following things
What i have done in code :
1.Added a error object to get the error message
import smtplib
try:
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('surapon#gmail.com','222222')
mail.sendmail('surapon#gmail.com','youremail#gmail.com',content)
mail.quit
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
print "Error: unable to send email"
If u ran this code u would see a error message like this stating that google is not allowing u to login via code
Things to change in gmail:
1.Login to gmail
2.Go to this link https://www.google.com/settings/security/lesssecureapps
3.Click enable then retry the code
Hopes it help :)
But there are security threats if u enable it
This is because google considers python SMTPLIB less secure. The link in the error message leads you to the answer.
Within that link there is another link to allowing "less secure" apps to send mail.
https://support.google.com/accounts/answer/6010255
This allows you to specifically allow your application to send email. They provide very little information on what constitutes the security issue.
The link they provide to give explicit access is
http://www.google.com/settings/security/lesssecureapps within the earlier link.
I'll explain what you're trying to do. You're attempting to do an SMTP with the following credentials:'smtp.gmail.com',587
Then you're attempting to login with your gmail creds. Totally wrong.
What you want to do is:
import smtplib
content = 'test'
me = 'surapon#gmail.com'
you = ['someOther#gmail.com']
mail = smtplib.SMTP('localhost')
msg['Subject'] = 'Hello'
msg['From'] = me
msg['To'] = you[0]
#mail.ehlo()
#mail.starttls()
#mail.login('surapon#gmail.com','222222')
mail.sendmail(me, you, msg.as_string())
mail.quit()
Do not include the #gmail.com in your credentials. I used your code with my username (without #gmail.com) and password and was able to send a message.
import smtplib
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('surapon','222222')
mail.sendmail('surapon#gmail.com','youremail#gmail.com',content)
mail.quit

Categories

Resources