-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlx_sendmail.py
81 lines (67 loc) · 1.97 KB
/
lx_sendmail.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# code a func to send mail from smtp.163.com
import smtplib
from email.mime.text import MIMEText
sm = smtplib.SMTP()
'''
#---------------------------send an email via smtp.163.com --------------------
# init smtp
sm = smtplib.SMTP()
# end init smtp
# define
server_name = '163'
smtp_server = 'smtp.' + server_name + '.com'
user_accoutname = 'liuxun931'
user_account = user_accoutname + '@' + server_name + '.com'
user_pw = 'lx88979888'
receiver_mailaddr = 'liuxun931@163.com'
print(smtp_server)
print(user_account)
print(user_pw)
# try to connect
sm.connect(smtp_server, 25)
sm.helo()
sm.ehlo()
sm.login(user_account, user_pw)
sm.sendmail(user_account, receiver_mailaddr, 'msg')
# quit
sm.quit()
# end quit
#-------------------------------end send an email via smtp.163.com --------------------
'''
#add a mail message
with open('mail_message.txt') as fp:
# Create a text/plain message
msg = MIMEText(fp.read())
me = 'liuxun931@163.com'
you = 'liuxun931@163.com'
msg['Subject'] = 'The contents of mail_message.txt.'
msg['From'] = me
msg['To'] = you
# sm.send_message(msg)
try:
sm = smtplib.SMTP()
sm.connect('smtp.163.com', 25)
sm.helo()
sm.ehlo()
sm.login('liuxun931@163.com', 'lx88979888')
sm.sendmail('liuxun931@163', 'liuxun931@163.com', msg)
sm.quit()
except:
print('Failed to send mail-msg.')
def send_my_email(user_accountname, user_pw, server_name, receiver_mailaddr):
# user_accountname, user_pw, server_name, receiver_mailaddr
# ('liuxun931', 'lx88979888', '163', 'liuxun931@163.com')
# server_name = '163'
smtp_server = 'smtp.' + server_name + '.com'
# user_accoutname = 'liuxun931'
user_account = user_accountname + '@' + server_name + '.com'
# user_pw = 'lx88979888'
# receiver_mailaddr = 'liuxun931@163.com'
sm = smtplib.SMTP()
sm.connect(smtp_server, 25)
sm.helo()
sm.ehlo()
sm.login(user_account, user_pw)
sm.sendmail(user_account, receiver_mailaddr, 'msg')
# quit
sm.quit()