Python: Code Snippets - Send Email (SMTP)
Saturday 22nd July 2017 5:58pm
This script is used to send emails using an SMTP (Simple Mail Transfer Protocol). To send emails you will need to supply the following information:
- Email Addres to Send To
- Email Addres to be Sent From
- Email Account Username
- Email Account Password
- SMTP Server
- SMTP Port Number
To run this script copy the code below and save to a file called send_mail.py
send_mail.py:
#!/usr/bin/env python # SendEmail # A quick way to send emails # Date: 07 March 2016 # Written By: Phantom Raspberry Blower import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage class SendEmail(): # Initialize object def __init__(self, verbose): self.verbose = verbose # Used to send email def send_email(self, send_to, sent_from, username, password, smtp_server, port, subject, message, output_path='', filename='', image_full_path='', image_type='jpg', hyper_txt=''): if len(send_to) > 0: # Send email with images as attachments try: msg = MIMEMultipart('related') msg["From"] = sent_from msg["To"] = send_to msg["Subject"] = subject msg.preamble = "The Phantom Raspberry Blower has struck again!" # Encapsulate the plain and HTML versions of the message # body in an 'alternative' part, so message agents can # decide which they want to display. msgAlternative = MIMEMultipart('alternative') msg.attach(msgAlternative) msgText = MIMEText(message, 'plain') msgAlternative.attach(msgText) if len(image_full_path) > 0: fp = open(image_full_path) msgImage = MIMEImage(fp.read(), image_type) fp.close() # We reference the image in the IMG SRC attribute by # the ID we give it below msgText = MIMEText('<img src="cid:image1">%s' % hyper_txt, 'html') msgAlternative.attach(msgText) # Define the image's ID as referenced above msgImage.add_header('Content-Type', 'image/jpg', filename=filename + '.jpg') msgImage.add_header('Content-ID', 'image1') msgImage.add_header('Content-Disposition', 'inline', filename=filename + '.jpg') msg.attach(msgImage) # Send email and image attachment smtpObj = smtplib.SMTP(smtp_server, port) smtpObj.login(username, password) smtpObj.sendmail(sent_from, send_to, msg.as_string()) smtpObj.quit() if self.verbose: print("Email sent successfully :)") return True except smtplib.SMTPException: print("Something wicked happened :(") return False else: print("No email settings!") return False # Check if running stand-alone or imported if __name__ == '__main__': from send_mail import SendEmail se = SendEmail(True) send_to = raw_input("Type email address to send to: ") sent_from = raw_input("Type email address to be sent from: ") username = raw_input("Type email account username: ") password = raw_input("Type email account password: ") smtp_server = raw_input("Type SMTP server: ") port = raw_input("Type SMTP port number: ") subject = 'Test Email sent using Python' message = 'A message to you Rudy!' info = se.send_email(send_to, sent_from, username, password, smtp_server, int(port), subject, message)
Output:
python send_mail.py Type email address to send to: Type email address to be sent from: Type email account username: Type email account password: Type SMTP server: Type SMTP port number: Email sent successfully : )