linux下python使用sendmail发送邮件
2019/7/15 0:45:37
本文主要是介绍linux下python使用sendmail发送邮件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例为大家分享了python使用sendmail发送邮件的具体代码,供大家参考,具体内容如下
参考链接:How do I send mail from a Python script?
使用linux下的sendmail程序来发送邮件,利用popen函数(python docs关于popen函数)可以直接调用linux系统程序,需要指定程序所在的位置。
python代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- #Author: Victor Lv SENDMAIL = "/usr/sbin/sendmail" #sendmail(可执行程序)所在的路径 sender = "sender@example.com" receivers = ["user1@example.com", "user2@example.com"] subject = "这是邮件标题" text = "这是邮件正文。" #将这些元素组合成一条message message = """\ From: %s To: %s Subject: %s %s """ % (sender, ", ".join(receivers), subject, text) # Send the mail import os p = os.popen("%s -t -i" % SENDMAIL, "w") p.write(message) status = p.close() if status: print "Sendmail exit status", status
python docs中关于发送邮件的其他方法和例子:email: Examples
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于linux下python使用sendmail发送邮件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战
- 2024-09-18Python量化交易:入门指南与实践
- 2024-09-18Python量化交易:入门指南与实战技巧
- 2024-09-14Python人工智能项目实战:从零开始的实践指南
- 2024-09-14探索Python人工智能资料:初学者的指南
- 2024-09-14Python人工智能资料:初学者的全面指南
- 2024-09-13Matplotlib入门:轻松绘制Python数据可视化图表
- 2024-09-13Python人工智能:初学者的入门指南
- 2024-09-13Python人工智能:轻松入门与实践