-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmtp_sender_module.py
More file actions
53 lines (23 loc) · 1.22 KB
/
smtp_sender_module.py
File metadata and controls
53 lines (23 loc) · 1.22 KB
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
import smtplib
from email.header import Header
from email.mime.text import MIMEText
import sys
def send_email(SMTP_host="smtp.163.com", from_account="yourname@163.com", from_passwd="yourpassword", to_account="anotheraccount@QQ.com", subject="EGBot Auto Reply", content="Test email"):
email_client = smtplib.SMTP(SMTP_host) # connect to host
email_client.login(from_account, from_passwd) # login to account
# create msg
msg = MIMEText(content, 'plain', 'utf-8') # text-only email
msg['Subject'] = Header(subject, 'utf-8') # set subject
msg['From'] = from_account # sender mail
msg['To'] = to_account # receiver mail
email_client.sendmail(from_account, to_account, msg.as_string()) # send the mail
print("Email successfully sent!")
email_client.quit() # quit the client
if __name__ == '__main__':
# usage: python3 smtp_sender_module.py [TO_ACCOUNT] [EMAIL_CONTENT]
# input: 2 args: email sender, email body
# output: 1 string: indicating mail successfully delivered
to_account = sys.argv[1] # email destination
content = sys.argv[2] # email body
send_email(to_account=to_account, content=content)
sys.exit(0)