2

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

import smtplib
fromaddr = '[email protected]'
toaddrs  = '[email protected]'

msg = 'my message'

username = '[email protected]'
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...

5
  • 2
    Depends on what OS you are on, it might have restrictions on port 587. Commented Apr 27, 2017 at 20:00
  • RIght now I've logged in to my [email protected] to see if the email has been sent. Email was sent & in reply I got this email, "Your message to [email protected] has been blocked. See technical details below for more information. LEARN MORE" Commented Apr 27, 2017 at 20:03
  • What doe this have to do with [google-maps]? Commented Apr 27, 2017 at 20:08
  • Possible Dup: stackoverflow.com/questions/10147455/… Commented Apr 27, 2017 at 20:16
  • It could help to tell us what where the technical details... Commented May 2, 2017 at 7:54

2 Answers 2

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 = '[email protected]'
    gmailPassword = 'P@ssw0rd'
    recipient = '[email protected]'
    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()
Sign up to request clarification or add additional context in comments.

1 Comment

Should be from email.mime.multipart import MIMEMultipart & from email.mime.text import MIMEText and the self parameter is superfluous
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 = '[email protected]'
gmailPassword = 'XXXXX'
recipient = '[email protected]'

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...')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.