python3 通过smtplib模块发送邮件

為{幸葍}努か 提交于 2020-02-01 01:15:10

python3 通过smtplib模块发送邮件 

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import smtplib
import email.mime.multipart
import email.mime.text


def send_email(SMTP_host, from_addr, password, to_addrs, subject='', content=''):
    """
    发送邮件
    :param SMTP_host: smtp.163.com
    :param from_addr: 发送地址:xxx@163.com
    :param password: 密码: password
    :param to_addrs: 发送给谁的邮箱: xxx@qq.com
    :param subject:  邮件主题: test
    :param content:  邮件内容: test
    :return: None
    """
    msg = email.mime.multipart.MIMEMultipart()
    msg['from'] = from_addr
    msg['to'] = to_addrs
    msg['subject'] = subject
    content = content
    txt = email.mime.text.MIMEText(content)
    msg.attach(txt)

    smtp = smtplib.SMTP()
    smtp.connect(SMTP_host, '25')
    smtp.login(from_addr, password)
    smtp.sendmail(from_addr, to_addrs, str(msg))
    smtp.quit()


send_email('smtp.163.com', 'xxxx@163.com', 'password', 'to_addrs', 'subject', 'content')

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!