Moving again
[nil.git] / nil / gmail.py
blobd7afae1c602a14ba39bb4f28a870283d3c3a06e7
1 import smtplib
2 from email.MIMEMultipart import MIMEMultipart
3 from email.MIMEBase import MIMEBase
4 from email.MIMEText import MIMEText
5 from email import Encoders
6 import os
8 def send(sender, password, recipient, subject, text, attachment_filename = False, attachment_content = False):
10 try:
11 if attachment_filename != False:
12 message = MIMEMultipart()
14 message['From'] = sender
15 message['To'] = recipient
16 message['Subject'] = subject
18 message.attach(MIMEText(text))
20 part = MIMEBase('application', 'octet-stream')
21 part.set_payload(attachment_content)
22 Encoders.encode_base64(part)
23 part.add_header('Content-Disposition', 'attachment; filename="%s"' % attachment_filename)
24 message.attachment(part)
26 data = message.as_string()
28 else:
29 data = 'From: %s\nTo: %s\nSubject: %s\n\n%s' % (sender, recipient, subject, text)
31 mail_server = smtplib.SMTP('smtp.gmail.com', 587)
32 mail_server.ehlo()
33 mail_server.starttls()
34 mail_server.ehlo()
35 mail_server.login(sender, password)
36 mail_server.sendmail(sender, recipient, data)
37 mail_server.close()
39 return True
40 except:
41 return False