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
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Date: Tue Nov 29 10:59:43 2022
# Author: liuliancao <liuliancao@gmail.com>
"""Description: Python smtp example."""
import smtplib
def send_mail(smtp_host, username, password, subject="test subject", content="test message", smtp_port=465, dst=""):
"""
Send mail by smtp.
@smtp_host: smtp host like smtp.xxx.com
@smtp_port: smtp port like 25 or 465
@username: your mail username
@password: your mail password
"""
try:
smtp = smtplib.SMTP()
smtp.set_debuglevel(1)
smtp.connect(smtp_host, smtp_port)
smtp.login(username, password)
msg = MIMEText(content)
smtp.sendmail(msg_from=username, msg_to=dst, msg=msg.as_string())
except Exception as e:
print("smtp connect or login failed ", str(e))
finally:
pass
|