(转)python发送邮件

#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
#导入smtplib和MIMEText  
 
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
import sys
import time
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
          
# 设置服务器,用户名、口令以及邮箱的后缀  
mail_host="mail.xxxx.com"   
mail_user="[email protected]" #发送者邮箱
mail_pass="xxxxx"        #发送者邮箱密码


def send_mail(to,subject,message):
 
        msg =  MIMEText(message,'text','utf-8')
        me=mail_user+"<"+mail_user+">"
 
        #加邮件头
        msg['to'] = to
        msg['from'] = mail_user+"<"+mail_user+">"
        msg['subject'] = subject
 
        try:
            server = smtplib.SMTP()
            server.connect("127.0.0.1:25")
            server.login("xxxxx",mail_pass)#XXX为用户名不带@xxx,XXXXX为密码            
            server.sendmail(msg['from'],msg['to'],msg.as_string())
            server.close()
        except Exception,e:
            print str(e)
  
if __name__ == '__main__':
 
    to = sys.argv[1]
    subject = sys.argv[2]
    message = sys.argv[3]

    send_mail(to,subject,message)

测试     /usr/bin/python /root/scripts/send_mail_zb.py "[email protected]" "test2222" "test2222"

猜你喜欢

转载自blog.csdn.net/A9925/article/details/85695462