python 发送邮件

# encoding=utf-8

import smtplib
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

pwd = "Axx2345"
sender = "test@xxx.com"
server = 'smtp.xxx.com'
reveivers = ['test01@xxx.com', 'test02@xxx.com']
subject = 'python 邮件发送'


def send_text_email():
    """发送一个普通的文本邮件"""
    text_content = """
    <h2>title</h2>
    <p>eamil content</p>
    """
    msg = MIMEText(text_content, 'plain', 'utf-8')
    msg['From'] = sender
    msg['To'] = ';'.join(reveivers)
    msg['Subject'] = subject  # 邮件标题
    smtp = smtplib.SMTP(server)
    smtp.login(sender, pwd)
    smtp.sendmail(sender, reveivers, msg.as_string())


def send_html_email():
    """发送一个带html格式的邮件"""
    html_content = """
    <h2>title</h2>
    <p>eamil content</p>
    """
    msg = MIMEText(html_content, 'html', 'utf-8')
    msg['From'] = sender
    msg['To'] = ';'.join(reveivers)
    msg['Subject'] = subject  # 邮件标题
    smtp = smtplib.SMTP(server)
    smtp.login(sender, pwd)
    smtp.sendmail(sender, reveivers, msg.as_string())


def send_attach_email():
    """发送一个带附件的邮件"""
    msg = MIMEText('带附件的邮件', 'plain', 'utf-8')

    image_attach = MIMEImage(open('test.JPG', 'rb').read())
    image_attach.add_header('Content-Disposition', 'attachment', filename='test_image.jpg')

    image_attach2 = MIMEApplication(open('test.JPG', 'rb').read())
    image_attach2.add_header('Content-Disposition', 'attachment', filename='test_image2.jpg')

    pdf_attach = MIMEApplication(open('test_pdf.pdf', 'rb').read())
    pdf_attach.add_header('Content-Disposition', 'attachment', filename='test_pdf.pdf')

    multipart = MIMEMultipart()
    multipart['From'] = sender
    multipart['To'] = ';'.join(reveivers)
    multipart['Subject'] = subject
    multipart.attach(msg)
    multipart.attach(image_attach)
    multipart.attach(image_attach2)
    multipart.attach(pdf_attach)

    smtp = smtplib.SMTP(server)
    smtp.login(sender, pwd)
    smtp.sendmail(sender, reveivers, multipart.as_string())


def send_html_contain_image_email():
    """发送一个包含图片的html邮件"""
    html_content = """
    <h2>title</h2>
    <p>eamil content</p>
    <br/>
    <img src="cid:image01">
    """
    msg = MIMEText(html_content, 'html', 'utf-8')

    image_attach = MIMEImage(open('test.JPG', 'rb').read())
    image_attach.add_header('Content-ID', 'image01')

    multipart = MIMEMultipart()
    multipart['From'] = sender
    multipart['To'] = ';'.join(reveivers)
    multipart['Subject'] = subject  # 邮件标题
    multipart.attach(msg)
    multipart.attach(image_attach)

    smtp = smtplib.SMTP(server)
    smtp.login(sender, pwd)
    smtp.sendmail(sender, reveivers, multipart.as_string())


if __name__ == '__main__':
    send_text_email()
    send_html_email()
    send_attach_email()
    send_html_contain_image_email()

发送一个普通的文本邮件

python 发送邮件

发送一个带html格式的邮件

python 发送邮件

发送一个带附件的邮件

python 发送邮件

发送一个包含图片的html邮件

python 发送邮件

调试发送邮件问题

  • 点击 -> 更多操作 -> 查看邮件源码
    python 发送邮件
上一篇:获取音频时长


下一篇:springboot报错说 Failed to parse multipart servlet request; nested exception is java.io.IOException