-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathemail_me.py
35 lines (29 loc) · 805 Bytes
/
email_me.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
#!/usr/local/bin/python
# coding: utf-8
import os
import settings
fromaddr = settings.EMAIL_SENDER
password = settings.EMAIL_SENDER_PSWD
toaddr = settings.EMAIL_RECEIVER
mail_server = settings.SERVER_EMAIL_SENDER
def send_email(subject, body):
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
import smtplib
try:
server = smtplib.SMTP(mail_server, 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(fromaddr, password)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
except smtplib.socket.error:
print('mail socket error')
except smtplib.SMTPException:
print('mail smtp exception')