MCPcopy Create free account
hub / github.com/ActiveState/code / process

Function process

recipes/Python/577690_Python_Gmail_script_smtp/recipe-577690.py:74–104  ·  view source on GitHub ↗
(options, args)

Source from the content-addressed store, hash-verified

72 process(options, args)
73
74def process(options, args):
75 config = get_config(options)
76 # Write the email
77 msg = MIMEMultipart()
78 msg['From'] = config['fromaddr']
79 msg['To'] = config['toaddrs']
80 msg['Subject'] = options.subject
81 body = options.body
82 msg.attach(MIMEText(body, 'plain'))
83 # Attach image
84 if options.image_file:
85 try:
86 filename = open(options.image_file, "rb")
87 attach_image = MIMEImage(filename.read())
88 attach_image.add_header('Content-Disposition',
89 'attachment; filename = %s'%options.image_file)
90 msg.attach(attach_image)
91 filename.close()
92 except:
93 msg.attach(MIMEText('Image attachment error', 'plain'))
94 # Converting email to text
95 text = msg.as_string()
96
97 # The actual mail send
98 server = smtplib.SMTP('smtp.gmail.com:587')
99 server.ehlo()
100 server.starttls()
101 server.ehlo()
102 server.login(config['username'],config['password'])
103 server.sendmail(config['fromaddr'], config['toaddrs'], text)
104 server.quit()
105
106def get_config(options):
107 conf = {}

Callers 1

mainFunction · 0.70

Calls 8

get_configFunction · 0.85
add_headerMethod · 0.80
loginMethod · 0.80
openFunction · 0.50
attachMethod · 0.45
readMethod · 0.45
closeMethod · 0.45
quitMethod · 0.45

Tested by

no test coverage detected