-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend_email.py
44 lines (33 loc) · 1.26 KB
/
send_email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#-*-coding:utf-8-*-
import os
import smtplib
import logging
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from config import EMAIL_ACCOUNT, EMAIL_PWD, EMAIL_HOST, EMAIL_PORT
logger = logging.getLogger(__name__)
def send_file_via_email(to_email, attachment_file, subject='Convert'):
msg = MIMEMultipart()
msg['from'] = EMAIL_ACCOUNT
msg['to'] = to_email
msg['subject'] = subject
logger.info('going to send %s to %s from %s', attachment_file, msg['to'],
msg['from'])
with open(attachment_file, 'rb') as fp:
data = fp.read()
att = MIMEText(data, 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="%s"' % os.path.basename(attachment_file)
msg.attach(att)
mail = smtplib.SMTP(timeout = 60)
mail.connect(EMAIL_HOST, EMAIL_PORT)
mail.ehlo()
mail.starttls()
if EMAIL_ACCOUNT and EMAIL_PWD:
mail.login(EMAIL_ACCOUNT, EMAIL_PWD)
logger.info('mail server connected...start sending')
mail.sendmail(msg['from'], msg['to'], msg.as_string())
logger.info('email sent')
mail.close()
if __name__ == '__main__':
send_file_via_email('[email protected]', 'data/e1006646/e1006646.mobi')