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设计】。

相关文章

关于Python3 lambda函数的深入浅出

我们常常看到一个这样的表达式  A=lambda x:x+1 可能会一头雾水不知道怎么计算 最基本的理解就是 def A(x): return x+1 但是理解程序不会将一个表...

浅谈Django2.0 加xadmin踩的坑

如果是django2.0 必须下载xadmin2.0 不然很多地方不兼容 xadmin2.0下载地址 https://github.com/sshwsfc/xadmin/tree/dja...

Python实现的逻辑回归算法示例【附测试csv文件下载】

Python实现的逻辑回归算法示例【附测试csv文件下载】

本文实例讲述了Python实现的逻辑回归算法。分享给大家供大家参考,具体如下: 使用python实现逻辑回归 Using Python to Implement Logistic Reg...

python 调试冷知识(小结)

python 调试冷知识(小结)

对于 python 代码的调试我们通常都是使用 IDE 自带的调试功能。但是 IDE 提供的调试功能存在局限性,例如在测试服务器上调试代码,但是又不可能在测试服务器上安装 IDE 进行调...

Python3安装Pillow与PIL的方法

关于Pillow与PIL PIL(Python Imaging Library)是Python一个强大方便的图像处理库,名气也比较大。不过只支持到Python 2.7。 PIL官方网站:...