2 from email
.MIMEMultipart
import MIMEMultipart
3 from email
.MIMEBase
import MIMEBase
4 from email
.MIMEText
import MIMEText
5 from email
import Encoders
8 def send(sender
, password
, recipient
, subject
, text
, attachment_filename
= False, attachment_content
= False):
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()
29 data
= 'From: %s\nTo: %s\nSubject: %s\n\n%s' % (sender
, recipient
, subject
, text
)
31 mail_server
= smtplib
.SMTP('smtp.gmail.com', 587)
33 mail_server
.starttls()
35 mail_server
.login(sender
, password
)
36 mail_server
.sendmail(sender
, recipient
, data
)