python smtp邮件

SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。

python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。

参考:https://www.cnblogs.com/ysocean/p/7653252.html

   https://blog.csdn.net/qq_42543250/article/details/81586663

   https://www.cnblogs.com/captainmeng/p/7683395.html

在第三方客户端使用stmp之前需要开启邮箱中的POP3/SMTP/IMAP服务,并获取到授权码。这里使用是163邮箱

纯文本邮件

 1 import smtplib
 2 from email.header import Header
 3 from email.mime.text import MIMEText
 4
 5 # 第三方 SMTP 服务
 6 mail_host = "smtp.163.com"               # SMTP服务器
 7 mail_user = "*******@163.com"       # 用户名
 8 mail_pass = "*******"               # 授权密码,非登录密码
 9
10 sender = ‘********@163.com‘       # 发件人邮箱(最好写全, 不然会失败)
11 receivers = [‘********@163.com‘]  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
12
13 content = ‘这里是测试文本!‘             #测试文本
14 title = ‘smtp邮件测试‘                       # 邮件主题
15
16  #内置参数执行
17 def sendEmail():
18
19     message = MIMEText(content, ‘plain‘, ‘utf-8‘)  # 内容, 格式, 编码
20     message[‘From‘] = "{}".format(sender)
21     message[‘To‘] = ",".join(receivers)
22     message[‘Subject‘] = title
23
24     try:
25         smtpObj = smtplib.SMTP_SSL(mail_host)  # 启用SSL发信, 端口默认是25(可以不写)
26         smtpObj.login(mail_user, mail_pass)    # 登录验证
27         smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
28         print("mail has been send successfully.")
29     except smtplib.SMTPException as e:
30         print(e)
31
32  #参数传入执行
33 def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content):
34     email_client = smtplib.SMTP(SMTP_host)
35     email_client.login(from_account, from_passwd)
36     # create msg
37     msg = MIMEText(content, ‘plain‘, ‘utf-8‘)
38     msg[‘Subject‘] = Header(subject, ‘utf-8‘)  # subject
39     msg[‘From‘] = from_account
40     msg[‘To‘] = to_account
41     email_client.sendmail(from_account, to_account, msg.as_string())
42
43     email_client.quit()
44
45 if __name__ == ‘__main__‘:
46     sendEmail()
47
48         

插入图片(非附件形式)

 1 # coding: utf-8
 2
 3 import smtplib
 4 from email.mime.multipart import MIMEMultipart
 5 from email.mime.text import MIMEText
 6 from email.mime.base import MIMEBase
 7 from email import encoders
 8
 9 mail_host = ‘smtp.163.com‘
10 receivers = [‘******@163.com‘]
11 receiver = ‘,‘.join(receivers)
12 sender = ‘******@163.com‘
13 password = ‘*****‘
14
15 msg = MIMEMultipart(‘alternative‘)
16 msg[‘From‘] = sender
17 msg[‘To‘] = receiver
18 msg[‘Subject‘] = ‘主题‘
19 #插入一个html格式
20 msg.attach(MIMEText(‘<html><body><h1>Hello</h1>‘ +
21     ‘<p><img src="cid:0"></p>‘ +
22     ‘</body></html>‘, ‘html‘, ‘utf-8‘))
23
24 file_path = r‘E:\stmp_test\img.png‘
25 with open(file_path, ‘rb‘) as f:
26     mm = MIMEBase(‘image‘, ‘png‘, filename=‘b.png‘)    # 设置附件的MIME和文件名,这里是png类型:
27     mm.add_header(‘Content-Disposition‘, ‘attachment‘, filename=‘b.jpg‘)  #加上必要的头信息
28     mm.add_header(‘Content-ID‘, ‘<0>‘)       #头信息
29     mm.add_header(‘X-Attachment-Id‘, ‘0‘)     #头信息
30     mm.set_payload(f.read())    # 把附件的内容读进来
31     encoders.encode_base64(mm)
32     msg.attach(mm)
33
34 try:
35     server = smtplib.SMTP()
36     # 加上这句就可以打印出所有的log,更方便我们定位问题
37     server.set_debuglevel(1)
38     server.connect(mail_host, 25)
39     server.login(sender, password)
40     server.sendmail(sender, receiver,msg.as_string())
41     server.close()
42     print (‘Success‘)
43 except smtplib.SMTPException:
44     print( ‘Error‘)

直接在HTML邮件中链接图片地址是不行的。引文大部分邮件服务商都会自动屏蔽带有外链的图片,因为不知道这些链接是否指向恶意网站。
只需要在HTML中通过引用src="cid:0"就可以把附件作为图片嵌入了。如果有多个图片,给它们依次编号,然后引用不同的cid:x即可。

原文地址:https://www.cnblogs.com/Hamge/p/10710827.html

时间: 2024-08-29 18:34:05

python smtp邮件的相关文章

Python SMTP邮件模块

SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件. 实例: 1.使用Python发送纯文本格式和html格式的邮件. 1 #!/usr/bin/env python 2 #coding:utf-8 3 4 import smtplib 5 from email.mime.text import MIMEText 6 fr

python SMTP邮件发送

import stmplib from email.mime.text import MIMEText from email.Header import Header mailhost='stmp.qq.com'  #设置服务器 mailuser='[email protected]' #使用时亲测不能用qq.com, maipass='XXXXXXXX'#不是登陆密码,填写授权码,在邮箱中设置smtp服务. sender='[email protected]' #发件人 receiver=['

selenium+python smtp邮件

#code:utf-8 import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header smtpserver = 'smtp.163.com' smtp_user = '*******' smtp_pass = '*******' from_addr='**********@163.com' to_a

python发邮件

import smtplib from email.mime.text import MIMEText from email.header import Header # 第三方 SMTP 服务 mail_host="smtp.qq.com" #设置服务器 mail_user="xxxxxx" #用户名 mail_pass="plcfthkdtpoxcabh" #口令QQ需要授权码 sender = '[email protected]' rec

smtp邮件测试

# -*- coding:utf-8 -*- #!/usr/bin/python import sys reload(sys) # reload 才能调用 setdefaultencoding 方法 sys.setdefaultencoding('utf-8') # 设置 'utf-8' import smtplib from email.mime.text import MIMEText from email.header import Header #第三方服务smtp mail_host=

Python——SMTP发送邮件

一.定义 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.python的smtplib提供了一种很方便的途径发送电子邮件.它对smtp协议进行了简单的封装. 二.Python创建STMP对象 import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] ) #host: SMTP 服务器主机. 你可

python email 邮件

. . .参考:https://www.runoob.com/python3/python3-smtp.html #encoding=utf-8 ''' Created on 2019年10月11日 @author: sea ''' import smtplib from email.mime.text import MIMEText from email.header import Header def send(From,to,subject,content): ''' send(From,

用python 发送一个smtp邮件

用python写一个简单的邮件,需要发送的邮件内容自定义,可用于监控警告邮件发送. #!/usr/bin/env python import smtplib    //内置smtp库 import string HOST = "smtp.163.com"    //定义用于发送邮件的主机,这里用网易163 SUBJECT = "Test email from Python"    //定义邮件标题 TO = "[email protected]"

python学习笔记(SMTP邮件发送)

想着给框架添加邮件发送功能.所以整理下python下邮件发送功能 首先python是支持邮件的发送.内置smtp库.支持发送纯文本.HTML及添加附件的邮件 之后是邮箱.像163.qq.新浪等邮箱默认关闭SMTP服务,需要我们手动打开 打开后通过发件人邮箱.授权密码 通过发件人的SMTP服务发送 代码如下: 1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 4 from email.mime.text import MIMEText 5 fr