forked from seven-liu/python_study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtplib_sendemail.py
42 lines (31 loc) · 1023 Bytes
/
smtplib_sendemail.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
#encoding:utf-8
import smtplib
import email.utils
from email.mime.text import MIMEText
import getpass
#prompt the user for connection info
to_email=raw_input('Recipient:')
servername=raw_input('Mail server name:')
username=raw_input('Mail username:')
password=getpass.getpass("%s's password:" %username)
#create message
msg=MIMEText('This is the body of message.')
msg.set_unixfrom('author')
msg['To']=email.Utils.formataddr(('Recipient',to_email))
msg['From']=email.utils.formataddr(('Author','author@example.com'))
msg['Subject']='simple test message'
server=smtplib.SMTP(servername)
#show communication with the server
try:
server.set_debuglevel(True)
if server.has_extn('STARTTLS'):
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail('author@example.com',[to_email],msg.as_string())
#try:
# server.sendemail('author@example.com',
# ['recipient@example.com'],
# msg.as_string())
finally:
server.quit()