用python发送电子邮件,我的方式
mercurio的Python项目详细描述
汞
用python发送电子邮件,我的方式。
Mercurio是一个简单的类,使用Python发送电子邮件,管理 SMTP,附件和mimetype,多个收件人, 具有特定内容ID(到HTML电子邮件模板)的附件…
安装
安装和运行一样简单:
pip install mercurio
用法
要初始化mercurio实例,只需使用 典型的SMTP参数。不会打开连接 在这个阶段,您可以初始化并重用它 在整个密码中。
frommercurioimportmercuriohg=mercurio(host="smtp.test.com",port=25,smtp_user="myuser",smtp_pwd="mypwd")
为了调试目的,可以创建消息并将其保存在文件中 不发送邮件。你可以打开这样的文件 一个普通的电子邮件客户。
message=hg.make_message(fromaddress="from@me.com",recipient="to@me.com",reply_to_address="reply@to.me",text="a long text, very long.",sbj="The subject, or the object?",attachments=[],cid_attachments={})withopen('test_email.eml','w')aso:o.write(message.as_string())
发送真正的信息很容易:使用 一个hg实例,并调用 send方法及其参数。注意你可以 发送多种类型的附件(图像、xlsx、json, 文本)。mercurio可以选择正确的mime类型。 也可以将附件与特定内容一起使用 id,可以在html电子邮件中使用,包括 邮件正文中的图像。
hg.send(fromaddress="from@me.com",recipients=["to@me.com"],reply_to_address="reply@to.me",text="a long text, very long.",sbj="The subject, or the object?",attachments=[],cid_attachments={"content_id_01":"mylogo.png"})