HTML Mail via Python

HTML email via Python

Like it? Share it!
# 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 = 'user1@beispiel.de' addr_from = 'user2@beispiel.de' # Define SMTP email server details smtp_server = 'mail.beispiel.de' smtp_user = 'test@beispiel.de' smtp_pass = '1234567889' # Construct email msg = MIMEMultipart('alternative') msg['To'] = 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. Text 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) s.login(smtp_user,smtp_pass) s.sendmail(addr_from, addr_to, msg.as_string()) s.quit()

>_ Author Guest

>_ last updated 4 yearss ago

>_ Language: Python

Mail