使用yagmail发送普通邮件的话,只需要几行代码
sender = '[email protected]'
password = 'xxx'
res = '[email protected]'
yag = yagmail.SMTP(user=sender, password=password, host='smtp.qq.com', smtp_ssl=True)
content = 'hello world'
yag.send(to=res, subject='测试发邮件', contents=content)
发送带有附件和网页的邮件
import yagmail, os
sender = '[email protected]'
password = 'xxx'
res = '[email protected]'
html = """
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>这是一个测试邮件</h1>
<img src="http://www.china-7.net/uploads/14770342827041.jpg" alt="">
</body>
</html>
"""
yag = yagmail.SMTP(user=sender, password=password, host='smtp.qq.com', smtp_ssl=True)
contents = ['这是一个测试邮件', html, os.path.join(os.path.dirname(__file__), '613240.jpg')]
yag.send(to=res, subject='测试发邮件', contents=contents)
print('发送成功')