Python邮件发送

2018-01-19  本文已影响0人  xhz的个人小屋
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import argparse
import smtplib
from email.header import Header
from email.mime.text import MIMEText

def send_mail(args):
  from_addr = 'xxx@xxxx.cn'    #发送邮箱的地址
  password = 'pwd'               #发送邮箱的客户端登陆密码,可能和网页登陆密码不同需要单独设置

  smtp_server = 'mail.teleinfo.cn'
  to_addr = 'xxxx@xxxx.cn'   #接收邮箱地址
  subject = u'提醒'
  msg = MIMEText(args.text, 'plain', 'utf-8')
  msg['From'] = from_addr  #发送邮箱地址
  msg['Subject'] = Header(subject, 'utf8').encode()  #邮件主题
  server = smtplib.SMTP(smtp_server, 25)           #连接SMTP服务器
  server.login(from_addr, password)                     #登陆
  server.sendmail(from_addr, [to_addr], msg.as_string())     #发送
  server.quit()
if __name__ == '__main__':

  parser = argparse.ArgumentParser(description='Python send mail script.')
  parser.add_argument('-t', '--text', required=True)
  args = parser.parse_args()

  send_mail(args)
上一篇下一篇

猜你喜欢

热点阅读