Skip to content

发送邮件

python发送邮件

示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate
from email.header import Header

def send_email(to_addr):
    host = 'smtp.exmail.qq.com'
    port = 465
    username = "xxx@xxx.com"
    password = "xxxxxxxxxx"

    smtp = smtplib.SMTP_SSL(host, port)
    smtp.login(username, password)

    subject = '[Notice]hello'
    body = 'hello,this is a mail from '

    # 初始化邮件
    encoding = 'utf-8'
    mail = MIMEText(body.encode(encoding), 'plain', encoding)
    mail['Subject'] = Header(subject, encoding)
    mail['From'] = username
    mail['To'] = to_addr
    mail['Date'] = formatdate()

    smtp.sendmail(username, to_addr, mail.as_string())

send_email("zhaoyangzhen@foxmail.com")

发送 html 格式的内容:

1
2
3
encoding = 'utf-8'
body = '<h1>content</h1>'
mail = MIMEText(body.encode(), 'html', encoding)

qq 邮箱使用

首先开启 IMAP/SMTP服务

接收邮件服务器:imap.qq.com
发送邮件服务器:smtp.qq.com

点击生成授权码-》发送短信-》点击“我已发送”-》获取到授权码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate
from email.header import Header


def send_email(to_addr):
    host = 'smtp.qq.com'
    port = 465
    username = "954241552@qq.com"
    password = "获取到的授权码"

    smtp = smtplib.SMTP_SSL(host, port)
    smtp.login(username, password)

    subject = '[Notice]hello'
    body = 'hello,this is a mail from '

    # 初始化邮件
    encoding = 'utf-8'
    mail = MIMEText(body.encode(encoding), 'plain', encoding)
    mail['Subject'] = Header(subject, encoding)
    mail['From'] = username
    mail['To'] = to_addr
    mail['Date'] = formatdate()

    smtp.sendmail(username, to_addr, mail.as_string())


send_email("zhaoyangzhen@foxmail.com")

发送 html 邮件附加图片

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage


def send_email(to_addr):
    host = 'smtp.exmail.qq.com'
    port = 465
    username = "zhaoyz@xxx.com"
    password = "xxxx"

    smtp = smtplib.SMTP_SSL(host, port)
    smtp.login(username, password)

    subject = '[Notice]hello'
    body = """
    <h1>content</h1>
    <p><img src="cid:image1"></p>
    """

    # 初始化邮件
    encoding = 'utf-8'
    mail = MIMEMultipart('mixed')
    mail.attach(MIMEText(body.encode(), 'html', encoding))
    mail['Subject'] = Header(subject, encoding)
    mail['From'] = username
    mail['To'] = to_addr
    mail['Date'] = formatdate()

    # 添加图片
    attach_image = MIMEImage(open('bar.png', "rb").read())
    attach_image.add_header('Content-ID', '<image1>')
    mail.attach(attach_image)

    smtp.sendmail(username, to_addr, mail.as_string())

send_email("zhaoyangzhen@foxmail.com")

添加图片的 base64

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import base64
import smtplib
from email.header import Header
from email.utils import formatdate
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


host = 'smtp.exmail.qq.com'
port = 465
username = "zhaoyz@xxx.com"
password = "xxx"
encoding = 'utf-8'
subject = f'tt'

smtp = smtplib.SMTP_SSL(host, port)
smtp.login(username, password)

with open("image1.png", "rb") as f:
    img_bin = f.read()
img_b64 = base64.b64encode(img_bin).decode()
body += f"""
<p><img src="data:image/png;base64,{img_b64}" alt="image1" /></p>
"""

with open("image2.png", "rb") as f:
    img_bin = f.read()
img_b64 = base64.b64encode(img_bin).decode()
body += f"""
<p><img src="data:image/png;base64,{img_b64}" alt="image2" /></p>
"""

mail = MIMEMultipart('mixed')
mail.attach(MIMEText(body.encode(), 'html', encoding))
mail['Subject'] = Header(subject, encoding)
mail['From'] = username
mail['To'] = to_addr
mail['Date'] = formatdate()

smtp.sendmail(username, to_addr, mail.as_string())

自己电脑本地的 email

Mac:

1
2
3
4
$ mail
No mail for nocilantro

$ ls /var/mail