Python 邮件带图片发送

Python 邮件带图片发送

这个还是受这位朋友的启发:chenjiandongx/email-subscribe。不过作者似乎删除了部分代码。我稍微改动了一下就成功了!

发件代码为:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# !/usr/bin/env python
# coding=utf-8

import smtplib
import datetime
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# import asyncio
# from pyppeteer import launch
# from common import *

HTML = """
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
<div align="center">
<img src="cid:one" />
</div>
</body>
</html>
"""
IMAGE_NAME = "one.png"

def fetch():
from urllib.request import urlretrieve
urlretrieve("http://image.wufazhuce.com/Ft23lIu-i6SOxrMcwnU2FJEOKQDb", IMAGE_NAME)

# 下面是原作者用于直接截图的函数
# async def fetch():
# browser = await launch(
# {"args": ["--no-sandbox", "--disable-setuid-sandbox"]}
# )
# page = await browser.newPage()
# await page.goto("http://wufazhuce.com/")
# await page.screenshot(
# {
# "path": IMAGE_NAME,
# "clip": {"x": 60, "y": 120, "height": 570, "width": 700},
# }
# )
# await browser.close()


title = '图片发送测试'
mail_host = "smtp.sina.com"
mail_user = "[email protected]"
mail_pass = "zssjmm126,"
sender = '[email protected]'
receivers = ['[email protected]']

def send_email():
html_content = HTML
msg = MIMEMultipart("alternative")
msg['From'] = "{}".format(sender)
msg['To'] = ",".join(receivers)
msg['Subject'] = title

with open(IMAGE_NAME, "rb") as f:
img = MIMEImage(f.read())
img.add_header("Content-ID", "one")
msg.attach(img)
msg.attach(MIMEText(html_content, "html", 'utf-8'))

try:
smtp_obj = smtplib.SMTP_SSL(mail_host, 465)
smtp_obj.login(mail_user, mail_pass)
smtp_obj.sendmail(sender, receivers, msg.as_string())
smtp_obj.quit()
except Exception as e:
print(e)


if __name__ == "__main__":
fetch()
send_email()

暂时还没有想好用来干嘛,或许我可以爬个妹子图网站,然后让程序每天自动给我发一些妹子的图片???哈哈

# Python

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×