前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python使用sendmail在lin

python使用sendmail在lin

作者头像
py3study
发布2020-01-13 14:38:14
5480
发布2020-01-13 14:38:14
举报
文章被收录于专栏:python3python3

参考链接:How do I send mail from a Python script?

使用linux下的sendmail程序来发送邮件,利用popen函数(python docs关于popen函数)可以直接调用linux系统程序,需要指定程序所在的位置。

代码语言:javascript
复制
#!/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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档