3

I am trying to send email using gmail account from python script using SMTP library. It is working fine with normal message body. But when i try to send it using HTML body. It does not allow me to send.

# Import smtplib to provide email functions
import smtplib

# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Define email addresses to use
addr_to   = '[email protected]'
addr_from = "[email protected]"

# Define SMTP email server details
smtp_server = 'smtp.gmail.com'
smtp_user   = '[email protected]'
smtp_pass   = 'xxxxxxx'

# Construct email
msg = MIMEMultipart('alternative')
msg['To'] = *emphasized text*addr_to
msg['From'] = addr_from
msg['Subject'] = 'Test Email From RPi'

# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
html = """\
<html>
  <head></head>
  <body>
    <p>This is a test message.</p>
    <p>Text and HTML</p>
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via an SMTP server
s = smtplib.SMTP(smtp_server,587)
s.login(smtp_user,smtp_pass)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()
4
  • Can you tell us the error you are getting ? Commented Jul 26, 2017 at 9:30
  • it is give "auth" error Commented Jul 26, 2017 at 9:33
  • Try turn on Allow less secure apps in your Gmail account which you used to login Commented Jul 26, 2017 at 9:34
  • 1
    What's up with the msg['To'] = *emphasized text*addr_to? Commented Jul 26, 2017 at 9:39

2 Answers 2

5

Add these two lines before attempting to login, it won't gave you the authentication error.

server.ehlo()
server.starttls()

So your code should look like this:

    # Import smtplib to provide email functions
    import smtplib

    # Import the email modules
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText

    # Define email addresses to use
    addr_to   = '[email protected]'
    addr_from = "[email protected]"

    # Define SMTP email server details
    smtp_server = 'smtp.gmail.com'
    smtp_user   = '[email protected]'
    smtp_pass   = 'xxxxxxx'

    # Construct email
    msg = MIMEMultipart('alternative')
    msg['To'] = *emphasized text*addr_to
    msg['From'] = addr_from
    msg['Subject'] = 'Test Email From RPi'

    # Create the body of the message (a plain-text and an HTML version).
    text = "This is a test message.\nText and html."

    (your html code)

    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')

    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.
    msg.attach(part1)
    msg.attach(part2)

    # Send the message via an SMTP server
    s = smtplib.SMTP(smtp_server,587)
    s.ehlo()
    s.starttls()
    s.login(smtp_user,smtp_pass)
    s.sendmail(addr_from, addr_to, msg.as_string())
    s.quit()

Sign up to request clarification or add additional context in comments.

1 Comment

i guess ´s.starttls()´ is enough. no need for s.ehlo()
0

The issue could be that google isn't letting using that service anymore as far as i know

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.