python的smtplib发邮件

用python的smtplib可以很方便地发邮件。

linux下的调用例子:

./send.sh "*@*;*@*" "title" "content"



shell, send.sh:

#!/bin/sh


HOST=`hostname`

SENDMAIL=send_mail.py


MAIL_HOST="smtp.*"

MAIL_USER="*"

MAIL_PASS="*"

MAIL_FROM="*"

MAIL_TO=$1 

MAIL_SUBJECT=$2 

TEXT=$3

python $SENDMAIL $MAIL_HOST $MAIL_USER $MAIL_PASS $MAIL_FROM "$MAIL_TO" "$MAIL_SUBJECT" "$TEXT" #"hello world"


python, send_mail.py:

#!/usr/bin/python


import sys;

import smtplib;

from email.MIMEText import MIMEText


mail_host = sys.argv[1]

mail_user = sys.argv[2]

mail_pass = sys.argv[3]

mail_from = sys.argv[4]

mail_to = sys.argv[5]

mail_to_list = mail_to.split(';')


mail_subject = sys.argv[6]

mail_content = sys.argv[7]


msg = MIMEText(mail_content, _subtype = 'html', _charset = 'utf-8')

msg['From'] = mail_from

msg['Subject'] = mail_subject

msg['To'] = mail_to

msg['MIME-Version'] = "1.0"

msg['Content-Type'] = "text/html"



try:

    client = smtplib.SMTP()

    client.connect(mail_host) 

    client.login(mail_user, mail_pass)

    client.sendmail(mail_from, mail_to_list, msg.as_string())

except:

    print "something error..."


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值