-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend_mail.py
43 lines (35 loc) · 1.25 KB
/
send_mail.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
# encoding: utf-8
import smtplib
import os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import ConfigParser
class SendMail(object):
@classmethod
def send(cls, toaddr, subject, message):
"""
Provide gmail user name and password
Credit :
http://code.activestate.com/recipes/577371-sending-gmail-though-python-code/
http://www.pythonforbeginners.com/code-snippets-source-code/using-python-to-send-email/
"""
config = ConfigParser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), 'config.ini'))
username = config.get('DEFAULT', 'username')
password = config.get('DEFAULT', 'password')
smtp_host = config.get('DEFAULT', 'smtp')
fromaddr = username
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject
body = message
msg.attach(MIMEText(body, 'plain'))
# functions to send an email
server = smtplib.SMTP(smtp_host)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username, password)
server.sendmail(fromaddr, toaddr, msg.as_string())
server.quit()