圆月山庄资源网 Design By www.vgjia.com

发送普通邮件

发送文本和html普通邮件如下:

from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr

import smtplib

def _format_addr(s):
  name, addr = parseaddr(s)
  return formataddr((Header(name, 'utf-8').encode(), addr))

def get_server(username):
  """
  通过邮箱地址获得邮箱服务器
  :param username:用户名,比如:123456@qq.com
  :return: 邮箱服务器地址,可以根据自己实际业务添加,比如:smtp.qq.com
  """
  servers = {'qq' : 'smtp.qq.com'
        , '126' : 'smtp.126.com'
        , '163' : 'smtp.163.com'
        , '139' : 'smtp.139.com'}

  for key,value in servers.items():
    if key in username:
      return value

def send_mail(username, password, to, sender_name, subject, content, email_type):
  """
  :param username: 
  :param password: 
  :param to: 接收者列表 []
  :param sender_name: 
  :param subject: 
  :param content: 
  :param email_type: 
  :return: 
  """
  from_addr = username
  password = password
  to_addr = to
  smtp_server = get_server(username)

  # 邮件正文是MIMEText类型
  msg = MIMEText('%s'%(content), '%s'%(email_type), 'utf-8')
  msg['From'] = _format_addr('%s<%s>' % (sender_name, from_addr))
  msg['To'] = _format_addr('<%s>' % to_addr)
  msg['Subject'] = Header('%s'%(subject), 'utf-8').encode()

  # 普通登陆端口是25,带ssl验证时候端口是465
  # smtp_server = 'smtp.exmail.qq.com'
  # server = smtplib.SMTP_SSL(smtp_server, 465)
  server = smtplib.SMTP(smtp_server, 25)
  server.set_debuglevel(1)
  server.login(from_addr, password)
  server.sendmail(from_addr, to_addr, msg.as_string())
  server.quit()

if __name__ == '__main__':
  """发送简单文本邮件"""
  username = '******@126.com'
  password = '******'
  sender_name = '******@126.com'
  subject = 'test 邮件'
  content = '<html><h1>ikeguang 的来信</h1></html> <a href="http://www.ikeguang.com" rel="external nofollow" >ikeguang.com</a></html>'
  # email_type 取值:plain,文本类型邮件;html,html类型邮件
  email_type = 'html'
  _to = ['******@126.com', '******@qq.com']
  for to in _to:
    send_mail(username, password, to, sender_name, subject, content, email_type)
    print('send mail to %s success' % to)

发送带附件的邮件

def send_mail_multipart(username, password, to, sender_name, subject, content, email_type):
  from_addr = username
  password = password
  to_addr = to
  smtp_server = get_server(username)

  msg = MIMEMultipart()
  # 邮件正文是MIMEText类型
  msg.attach(MIMEText('%s'%(content), '%s'%(email_type), 'utf-8'))
  msg['From'] = _format_addr('%s<%s>' % (sender_name, from_addr))
  msg['To'] = _format_addr('<%s>' % to_addr)
  msg['Subject'] = Header('%s'%(subject), 'utf-8').encode()

  # 读取附件
  filename = 'D:/我的文件/Codes/PyCode/source/image/0.jpg'
  with open(filename, 'rb') as f:
    # 设置附件的MIME和文件名,这里是png类型:
    mime = MIMEBase('image', 'jpg', filename='0.jpg')
    # 加上必要的头信息:
    mime.add_header('Content-Disposition', 'attachment', filename='0.jpg')
    mime.add_header('Content-ID', '<0>')
    mime.add_header('X-Attachment-Id', '0')
    # 把附件的内容读进来:
    mime.set_payload(f.read())
    # 用Base64编码:
    encoders.encode_base64(mime)
    # 添加到MIMEMultipart:
    msg.attach(mime)

  # 普通登陆端口是25,带ssl验证时候端口是465
  # smtplib.SMTP_SSL(smtp_server, 465)
  server = smtplib.SMTP(smtp_server, 25)
  server.set_debuglevel(1)
  server.login(from_addr, password)
  server.sendmail(from_addr, [to_addr], msg.as_string())
  server.quit()

标签:
python2,发送邮件,python3,发送邮件

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。