0

I am using the following code to send email from python program in localhost,

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

me = "tonyr1291@gmail.com"
you = "testaccount@gmail.com"


msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
   <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

s = smtplib.SMTP('localhost',5000)
s.sendmail(me, you, msg.as_string())
s.quit()

This code is from python documentation.

When I run this code, it is running continuously but no email is sent.

I would like to know, do I have to make some other configurations anywhere else other than this code.

I am not seeing any error.

I am using python 2.7

This is given as a solution in Sending HTML email using Python

Community
  • 1
  • 1
Tony Roczz
  • 2,366
  • 6
  • 32
  • 59

1 Answers1

0

It seems that you're using a gmail id. Now, the SMTP server is not your tornado server. it is the server of the email provider.

You can search online for the smtp settings for the gmail server and get the following:

  • Server name : smtp.gmail.com
  • Server port for SSL : 465
  • Server port for TLS : 587

I've gotten them from http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm

Also, you need to ensure you do not enable gmail's 2 step authentication when doing this, otherwise it will fail. Also, gmail specifically may require you to send other things like ehlo and starttls. You can find a previous answer with a complete example here : How to send an email with Gmail as provider using Python?

    import smtplib

    gmail_user = user
    gmail_pwd = pwd
    FROM = user
    TO = recipient if type(recipient) is list else [recipient]
    SUBJECT = subject
    TEXT = body

    # Prepare actual message
    message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        print 'successfully sent the mail'
    except:
        print "failed to send mail"
Community
  • 1
  • 1
AbdealiLoKo
  • 3,261
  • 2
  • 20
  • 36
  • I am getting a `smtplib.SMTPAuthenticationError` – Tony Roczz Jan 05 '16 at 10:09
  • have you logged in using `server.login()` ? If you don't login gmail will tell you you're unauthorized to send a mail from your account. – AbdealiLoKo Jan 05 '16 at 10:11
  • Actually I have logged into my gmail account – Tony Roczz Jan 05 '16 at 10:11
  • Where have you logged into your gmail account ? If you log into your gmail account in a browser, that doesn't count - because the python script doesn't know about it. The python script needs to give gmail your password to be allowed to send emails. – AbdealiLoKo Jan 05 '16 at 10:13
  • I just got a `sign-in attempt prevented` mail from gmail. Do I need to set some permission in gmail? I am passing the `username` and `password` properly. – Tony Roczz Jan 05 '16 at 10:15
  • A script written in a python smtp script isn't very secure according to gmail (It recommends you use oauth and more complicated things). If you just want to get it working simply tell gmail to allow less secure apps https://www.google.com/settings/u/1/security/lesssecureapps . This would be written in detail in the email gmail sent you about why it prevented the sign in. – AbdealiLoKo Jan 05 '16 at 10:21