python发送邮件的介绍比较多,都是需要登录某个邮件服务商,有密码修改和频繁发送被限制的问题。这里介绍一下,调用本机linux自身sendmail服务发送邮件。不需要登录,发送邮件名可以是任意名字,没有限制。
1. 安装:
#yum install -y sendmail
2. 启动服务:
#service sendmail start
检查服务是否加入自启行列
#chkconfig --list |grep sendmail
3 python代码:
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
import commands
def send_mail(sender, recevier, subject, html_content):
msg = MIMEText(html_content, 'html', 'utf-8')
msg["From"] = sender
msg["To"] = recevier
msg["Subject"] = subject
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
p.communicate(msg.as_string())
send_mail("
[email protected]","
[email protected],
[email protected]","title", “mail_text”)
[email protected]可以是任意邮箱名
[email protected]是收邮件的邮箱
title是邮件标题
mail_text是邮件内容
可以结合其他介绍python发邮件的资料,发出更复杂的邮件