linux下python使用sendmail发送邮件

(编辑:jimmy 日期: 2025/11/15 浏览:2)

本文实例为大家分享了python使用sendmail发送邮件的具体代码,供大家参考,具体内容如下

参考链接:How do I send mail from a Python script"_blank"" href="https://docs.python.org/2/library/os.html" rel="external nofollow" >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

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

一句话新闻

微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。