How to do it...

We can create an email message and attach Python's python-logo.gif file with the email message. Then, this message is sent from a Google account to a different account.

Listing 4.6 shows us how to send an email from your Google account:

#!/usr/bin/env python 
# Python Network Programming Cookbook, Second Edition -- Chapter - 5 
# This program is optimized for Python 2.7.12 and Python 3.5.2. 
# 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 Emaill Test' 
    msg['To'] = recipient 
    msg['From'] = sender 
    subject = 'Python email Test' 
    message = 'Images attached.' 
    # attach imgae 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 your 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 email to any email address if you provide your Google account details correctly. After running this script, you can check your recipient email account to verify that the email 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.
  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.103.154