You would like to send an e-mail message from your Google e-mail account to another account. You also need to attach a file with this message.
To run this recipe, you should have an e-mail account with Google or any other service provider.
We can create an e-mail message and attach Python's python-logo.gif
file with the e-mail message. Then, this message is sent from a Google account to a different account.
Listing 4.6 shows us how to send an e-mail from your Google account:
#!/usr/bin/env python # Python Network Programming Cookbook -- Chapter - 5 # This program is optimized for Python 2.7. # It may run on any other version with/without modifications. import argparse import os import getpass import re import sys import smtplib from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText SMTP_SERVER = 'smtp.gmail.com' SMTP_PORT = 587 def send_email(sender, recipient): """ Send email message """ msg = MIMEMultipart() msg['Subject'] = 'Python Email Test' msg['To'] = recipient msg['From'] = sender subject = 'Python email Test' message = 'Images attached.' # attach image files files = os.listdir(os.getcwd()) gifsearch = re.compile(".gif", re.IGNORECASE) files = filter(gifsearch.search, files) for filename in files: path = os.path.join(os.getcwd(), filename) if not os.path.isfile(path): continue img = MIMEImage(open(path, 'rb').read(), _subtype="gif") img.add_header('Content-Disposition', 'attachment', filename=filename) msg.attach(img) part = MIMEText('text', "plain") part.set_payload(message) msg.attach(part) # create smtp session session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) session.ehlo() session.starttls() session.ehlo password = getpass.getpass(prompt="Enter you Google password: ") session.login(sender, password) session.sendmail(sender, recipient, msg.as_string()) print "Email sent." session.quit() if __name__ == '__main__': parser = argparse.ArgumentParser(description='Email Sending Example') parser.add_argument('--sender', action="store", dest="sender") parser.add_argument('--recipient', action="store", dest="recipient") given_args = parser.parse_args() send_email(given_args.sender, given_args.recipient)
Running the following script outputs the success of sending an e-mail to any e-mail address if you provide your Google account details correctly. After running this script, you can check your recipient e-mail account to verify that the e-mail is actually sent.
$ python 5_6_send_email_from_gmail.py --sender=<USERNAME>@gmail.com –recipient=<USER>@<ANOTHER_COMPANY.com> Enter you Google password: Email sent.
In this recipe, an e-mail message is created in the send_email()
function. This function is supplied with a Google account from where the e-mail message will be sent. The message header object, msg
, is created by calling the MIMEMultipart()
class and then subject, recipient, and sender information is added on it.
Python's regular expression-handling module is used to filter the .gif
image on the current path. The image attachment object, img
, is then created with the MIMEImage()
method from the email.mime.image
module. A correct image header is added to this object and finally, the image is attached with the msg
object created earlier. We can attach multiple image files within a for
loop as shown in this recipe. We can also attach a plain text attachment in a similar way.
To send the e-mail message, we create an SMTP session. We call some testing method on this session object, such as ehlo()
or starttls()
. Then, log in to the Google SMTP server with a username and password and a sendmail()
method is called to send the e-mail.
3.133.146.47