linux下python使用sendmail发送邮件

yipeiwu_com6年前Python基础

本文实例为大家分享了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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python生态圈图像格式转换问题(推荐)

Python生态圈图像格式转换问题(推荐)

在Python生态圈里,最常用的图像库是PIL——尽管已经被后来的pillow取代,但因为pillow的API几乎完全继承了PIL,所以大家还是约定俗成地称其为PIL。除PIL之外,越来...

python 利用turtle库绘制笑脸和哭脸的例子

python 利用turtle库绘制笑脸和哭脸的例子

我就废话不多说了,直接上代码吧! import turtle turtle.pensize(5) turtle.pencolor("yellow") turtle.fillcolor...

python socket多线程通讯实例分析(聊天室)

本文实例讲述了python socket多线程通讯方法。分享给大家供大家参考,具体如下: #!/usr/bin/evn python """ 这是一个Socket+多进程的例子(聊天...

Python list操作用法总结

Python list操作用法总结

本文实例讲述了Python list操作用法。分享给大家供大家参考,具体如下: List是python中的基本数据结构之一,和Java中的ArrayList有些类似,支持动态的元素的增加...

使用Python & Flask 实现RESTful Web API的实例

环境安装: sudo pip install flask Flask 是一个Python的微服务的框架,基于Werkzeug, 一个 WSGI 类库。 Flask 优点: Written...