使用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('發送成功')