Email using SMTP python from gmail account with html body

I am trying to send email using a gmail account from a python script using the SMTP library. It works fine with a normal message body. But when I try to send it using HTML body. This prevents me from posting.

# 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   = 'xxxx@localdomain.com'
addr_from = "xxxxx@gmail.com"

# Define SMTP email server details
smtp_server = 'smtp.gmail.com'
smtp_user   = 'xxxxxx@gmail.com'
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()

      

+3


source to share


2 answers


Add these two lines before trying to login, it won't give you authentication errors.

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 = ' xxxx@localdomain.com '
    addr_from = " xxxxx@gmail.com "

    # Define SMTP email server details
    smtp_server = 'smtp.gmail.com'
    smtp_user = ' xxxxxx@gmail.com '
    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 ()

+2


source


The problem might be that google doesn't allow this service anymore as far as I know



0


source







All Articles