2

I have written a python script to send email from my gmail account to my another email. the code is:

import smtplib
fromaddr = 'my@gmail.com'
toaddrs  = 'to@gmail.com'

msg = 'my message'

username = 'my@gmail.com'
password = 'myPass'

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
print("Done")

I'm also getting the "Done" output as the email is being sent. But the problem is: I can't seem to receive the email. It doesn't show up in the inbox. I can't find the problem :( Can anyone please help?

Thanks in advance...

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
yohowi
  • 41
  • 1
  • 2
  • 2
    Depends on what OS you are on, it might have restrictions on port 587. –  Apr 27 '17 at 20:00
  • RIght now I've logged in to my from@gmail.com to see if the email has been sent. Email was sent & in reply I got this email, "Your message to email@gmail.com has been blocked. See technical details below for more information. LEARN MORE" – yohowi Apr 27 '17 at 20:03
  • What doe this have to do with [google-maps]? – geocodezip Apr 27 '17 at 20:08
  • Possible Dup: https://stackoverflow.com/questions/10147455/how-to-send-an-email-with-gmail-as-provider-using-python – Pedro Lobito Apr 27 '17 at 20:16
  • It could help to tell us what where the *technical details*... – Serge Ballesta May 02 '17 at 07:54

2 Answers2

4

Check this out, this works for me perfectly...

def send_mail(self):
    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText

    gmailUser = 'myemail@gmail.com'
    gmailPassword = 'P@ssw0rd'
    recipient = 'sendto@gmail.com'
    message='your message here '

    msg = MIMEMultipart()
    msg['From'] = gmailUser
    msg['To'] = recipient
    msg['Subject'] = "Subject of the email"
    msg.attach(MIMEText(message))

    mailServer = smtplib.SMTP('smtp.gmail.com', 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmailUser, gmailPassword)
    mailServer.sendmail(gmailUser, recipient, msg.as_string())
    mailServer.close()
  • 1
    Should be `from email.mime.multipart import MIMEMultipart` & `from email.mime.text import MIMEText` and the self parameter is superfluous – Zander Brown Jul 17 '17 at 16:53
0

Enable less secure apps on your gmail account and for Python3 use:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

gmailUser = 'XXXXX@gmail.com'
gmailPassword = 'XXXXX'
recipient = 'XXXXX@gmail.com'

message = f"""
Your message here...
"""

msg = MIMEMultipart()
msg['From'] = f'"Your Name" <{gmailUser}>'
msg['To'] = recipient
msg['Subject'] = "Your Subject..."
msg.attach(MIMEText(message))

try:
    mailServer = smtplib.SMTP('smtp.gmail.com', 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmailUser, gmailPassword)
    mailServer.sendmail(gmailUser, recipient, msg.as_string())
    mailServer.close()
    print ('Email sent!')
except:
    print ('Something went wrong...')
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268