linux下python使用sendmail发送邮件

linux下python使用sendmail发送邮件,第1张

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 = "[email protected]"  
receivers = ["[email protected]", "[email protected]"] 
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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3296322.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-05
下一篇 2022-10-05

发表评论

登录后才能评论

评论列表(0条)

保存