26 邮件示例
邮件简单示例
import yagmail
import os
sender ='1841000786@qq.com' #发送人
password= 'ezdsuwnvfdjkgegj' #申请接入授权服务密码,注意不是QQ邮箱登录密码
target = '318973407@qq.com' #接收邮箱地址
yag = yagmail.SMTP(user=sender, password=password,host='smtp.qq.com', smtp_ssl=True)
contents = '测试test'
yag.send(to=target,subject='测试yagmail发邮件',contents=contents)
print('已发送')
#备注:跨邮箱服务商可能会有延迟,比如QQ邮箱发送到网易邮箱。谷歌邮箱服务器被封找不到
邮件内容为html,携带附件
邮件复杂示例
for i in range(0,50): #外层for循环是发送49封邮件
sender ='1841000786@qq.com' #发送人
password= 'ezdsuwnvfdjkgegj' #申请接入授权服务密码,注意不是QQ邮箱登录密码
target = '2765970564@qq.com' #接收邮箱地址
html = """
<html lang="zh">
<head>
<meta charset="utf-8"
<title>示例2</title>
</head>
<body>
<h1>测试</h1>
<p>lalallalala</p>
<img src="http://5b0988e595225.cdn.sohucs.com/images/20181219/aec27193b3834a1694f535ba307995ad.jpeg"/>
</body>
</html>
"""
attachment_path = os.path.join(os.path.dirname(__file__),'base.png')
contents = ['测试yagmail示例2',html,attachment_path]#[邮件标题、正文、附件】
yag = yagmail.SMTP(user=sender, password=password,host='smtp.qq.com', smtp_ssl=True)
yag.send(to=target,contents=contents)
print('已发送')
time.sleep(2)