人生苦短之Python发邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了人生苦短之Python发邮件相关的知识,希望对你有一定的参考价值。

#coding=utf-8
import smtplib
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
‘‘‘
   一些常用邮箱发件服务器及端口号
   邮箱   发件服务器    非SSL协议端口    SSL协议端口
   163   smtp.163.com      25             465/587
   qq    smtp.qq.com       25             465/587
   
   发送邮件的几个错误:
   1.550错误(smtplib.SMTPAuthenticationError: (550, b‘User has no permission‘))
     535错误(smtplib.SMTPAuthenticationError: (535, b‘Error: authentication failed‘))
     邮箱一般是不开启客户端授权的,所以登录是拒绝的,需要去邮箱开启,然后会发送短信
     获取授权码作为客户端登录的密码(login方法中的密码)
   2.503错误(503 ‘Error: A secure connection is requiered(such as ssl)’之类)
     例如我们使用QQ邮箱是需要SSL登录的,所以需要smtplib.SMTP()改成smtplib.SMTP_SSL()
    
   @from_addr  发送邮件的地址
   @to_addr    接收邮件的地址(可以是列表)
   @mail_host  邮箱的SMTP服务器地址
   @mail_pass  邮箱开启smtp 需要的授权码
‘‘‘
from_addr = ‘[email protected]‘
to_addr = ‘[email protected]‘
mail_host = ‘smtp.qq.com‘
mail_pass = ‘itrwvjhjxupgbhhc‘

#文本形式的邮件
def send_text_mail():
    try:
        ‘‘‘
           MIMETest(content, type, 编码)  创建邮件信息主体
           msg[‘Subject‘]                 邮件主题
           msg[‘From‘]                    邮件发送方
           msg[‘To‘]                      收件方
        ‘‘‘
        msg = MIMEText(‘hello send by python‘, ‘plain‘, ‘utf-8‘)
        msg[‘From‘] = from_addr
        msg[‘To‘] = ‘,‘.join(to_addr)
        msg[‘Subject‘] = ‘主题‘

        server = smtplib.SMTP_SSL(mail_host, 465)
        server.login(from_addr, mail_pass)
        server.sendmail(from_addr, [to_addr, ], msg.as_string())
    except Exception as e:
        print e


#HTML格式的邮件
def send_html_mail():
    msg = MIMEText(‘<html><h1>你好</h1></html>‘, ‘html‘, ‘utf-8‘)
    msg[‘Subject‘] = ‘html‘

    smtp = smtplib.SMTP_SSL(mail_host, 465)
    smtp.login(from_addr, mail_pass)
    smtp.sendmail(from_addr, to_addr, msg
                  .as_string())
    smtp.quit()

#发送附件
def send_attachment_mail():
    #创建邮件对象  MIMEMultipart  指定类型为 alternative可以支持同时发送html和plain,但是
    # 不会都显示,html优先显示
    msg = MIMEMultipart(‘alternative‘)
    msg[‘From‘] = from_addr
    msg[‘To‘] = to_addr
    msg[‘Subject‘] = ‘AttachmentMail‘

    # 邮件的正文还是MIMEText
    part1 = MIMEText(‘这是个带附件的邮件‘, ‘plain‘, ‘utf-8‘)

    # 添加附件(添加一个本地的图片)
    att1 = MIMEText(open("C:\\6.jpg", ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
    att1[‘Content-Type‘] = ‘application/octet-stream‘
    att1[‘Content-Disposition‘] = ‘attachment;filename="6.jpg"‘
    att1[‘Content-ID‘] = ‘<0>‘

    msg.attach(att1)
    msg.attach(part1)

    smtp = smtplib.SMTP_SSL(mail_host, 465)
    smtp.login(from_addr, mail_pass)
    smtp.sendmail(from_addr, to_addr, msg
                  .as_string())
    smtp.quit()

#发送带图片的文本邮件
def send_imagetext_mail():
    msg = MIMEMultipart()
    msg[‘From‘] = from_addr
    msg[‘To‘] = to_addr
    msg[‘Subject‘] = ‘ImagMail‘

    #创建展示图片的html
    msg_html = MIMEText(‘<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!‘,
                        ‘html‘, ‘utf-8‘)
    msg.attach(msg_html)

    #添加图片模块
    fp = open(‘C:\\6.jpg‘, ‘rb‘)
    msg_image = MIMEImage(fp.read())
    fp.close()
    msg_image.add_header(‘Content-ID‘, ‘<image1>‘)
    msg.attach(msg_image)

    smtp = smtplib.SMTP_SSL(mail_host, 465)
    smtp.login(from_addr, mail_pass)
    smtp.sendmail(from_addr, to_addr, msg
                  .as_string())
    smtp.quit()

send_imagetext_mail()

‘‘‘
Message
+- MIMEBase
   +- MIMEMultipart
   +- MIMENonMultipart
      +- MIMEMessage
      +- MIMEText
      +- MIMEImage
      
    邮件信息的层级关系,详细见https://docs.python.org/2/library/email.mime.html
‘‘‘

 

以上是关于人生苦短之Python发邮件的主要内容,如果未能解决你的问题,请参考以下文章

人生苦短之Python迭代器

人生苦短之Python多线程

人生苦短之Python列表拷贝

人生苦短之Python类的一二三

人生苦短之Python文件的IO操作

人生苦短之Python枚举类型enum