import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header # 设置smtplib所需的参数
# 下面的发件人,收件人是用于邮件传输的。
smtpserver = 'smtp.126.com'
username = 'xiauxue0687@126.com'
password = '**********'
sender = 'xiaoxue0687@126.com'
# receiver='XXX@126.com'
# 收件人为多个收件人
receiver = ['526886905@qq.com'] subject = 'Python email test'
# 通过Header对象编码的文本,包含utf-8编码信息和Base64编码信息。以下中文名测试ok
# subject = '中文标题'
# subject=Header(subject, 'utf-8').encode() # 构造邮件对象MIMEMultipart对象
# 下面的主题,发件人,收件人,日期是显示在邮件页面上的。
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = 'xiaoxue0687@126.com <xiaoxue0687@126.com>'
# msg['To'] = 'XXX@126.com'
# 收件人为多个收件人,通过join将列表转换为以;为间隔的字符串
msg['To'] = ";".join(receiver)
# msg['Date']='2012-3-16' # 构造文字内容
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.baidu.com"
text_plain = MIMEText(text, 'plain', 'utf-8')
msg.attach(text_plain) # 构造图片链接
sendimagefile = open(r'F:\RobotTest\testimage.png', 'rb').read()
image = MIMEImage(sendimagefile)
image.add_header('Content-ID', '<image1>')
image["Content-Disposition"] = 'attachment; filename="testimage.png"'
msg.attach(image) # 构造html
# 发送正文中的图片:由于包含未被许可的信息,网易邮箱定义为垃圾邮件,报554 DT:SPM :<p><img src="cid:image1"></p>
html = """
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.baidu.com">link</a> you wanted.<br>
</p>
</body>
</html>
"""
text_html = MIMEText(html, 'html', 'utf-8')
text_html["Content-Disposition"] = 'attachment; filename="texthtml.html"'
msg.attach(text_html) # 构造附件
sendfile = open(r'F:\RobotTest\1111.txt', 'rb').read()
text_att = MIMEText(sendfile, 'base64', 'utf-8')
text_att["Content-Type"] = 'application/octet-stream'
# 以下附件可以重命名成aaa.txt
# text_att["Content-Disposition"] = 'attachment; filename="aaa.txt"'
# 另一种实现方式
text_att.add_header('Content-Disposition', 'attachment', filename='aaa.txt')
# 以下中文测试不ok
# text_att["Content-Disposition"] = u'attachment; filename="中文附件.txt"'.decode('utf-8')
msg.attach(text_att) # 发送邮件
smtp = smtplib.SMTP()
smtp.connect('smtp.126.com')
# 我们用set_debuglevel(1)就可以打印出和SMTP服务器交互的所有信息。
# smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

python - 发送html格式的邮件的更多相关文章

  1. 使用Python发送HTML格式的邮件(收到的邮件有发送方才是正解)

    发送html格式的和普通文本格式差不多,只是MIMEText(content,"html","utf-8"))与MIMEText(content,"p ...

  2. python发送html格式的邮件

    python发邮件 #!/usr/bin/python # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMET ...

  3. python 发送带附件的邮件

    特别注意的地方:filespart.add_header("Content-Disposition","attachment",filename=file_na ...

  4. 在Linux命令行下发送html格式的邮件

    在Linux利用formail+sendmail来发送带图片的邮件 formail接收html格式的文件作为邮件的内容,这样就可以解决发送带图片邮件的问题了,因为html中可以插入图片,只要给出的im ...

  5. Python发送带附件的邮件

    看别人的博客就不要在往别人的邮箱里发东西了行不行, 有点素质可以吗!!! 写出来分享还不知道珍惜!!!!! #-*-encoding:utf-8 -*- import os import smtpli ...

  6. 【代码片段】Python发送带图片的邮件

    # coding=utf-8 import smtplib from email.mime.text import MIMEText from email.mime.multipart import ...

  7. java mail发送html格式的邮件

    // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("ma ...

  8. python 发送带附件的 邮件

    from email.MIMETextimportMIMETextfrom email.MIMEMultipartimportMIMEMultipartimport smtplib mail_host ...

  9. Django -- 发送HTML格式的邮件

    提前在setting中设置邮箱server from django.core.mai import EmailMessage subject, from_email, to = 'xxx', 'nor ...

随机推荐

  1. Mysql清空表(truncate)与删除表中数据(delete)的区别

    来源:http://blog.is36.com/mysql_difference_of_truncate_and_delete/ 为某基于wordpress搭建的博客长久未除草,某天升级的时候发现已经 ...

  2. ArrayList的底层实现

    package zy809; public class myArrayList { /** 存放元素 */ private Object[] data;// 创建一个数组引用. /** 元素的个数 * ...

  3. 在vscode上 运行typescript 文件

    安装nodejs 安装链接: https://nodejs.org/zh-cn/ 安装测试: node -v npm -v 安装typescript sudo npm install typescri ...

  4. Coursera, Machine Learning, Neural Networks: Representation - week4/5

    Neural Network Motivations 想要拟合一条曲线,在feature 很多的情况下,feature的组合也很多,在现实中不适用,比如在computer vision问题中featu ...

  5. jmeter (六) 登录 token获取

    有时候登录请求中会含有token字段,如下,此时就需要提取token 怎么提取token呢,其实很简单,通过正则表达式就可以了 1.添加http请求:获取登录页面,为“get”方式 2.在此http请 ...

  6. HDFS笔记(一)

    1. HDFS 是什么? Hadoop分布式文件系统(Distributed File System)-HDFS(Hadoop Distributed File System) 2. HDFS 架构 ...

  7. Kotlin中与Java不同的地方 需要注意

    1. 在Kotlin中不会将基本数据类型的自动转型比如 scriptIntrinsicBlur.setRadius(25) //报错, 必须写成 25f 或者 调用.toFloat() 2.Kotli ...

  8. Failed to read artifact descriptor for org.apache.maven.plugins:maven-install-plugin-JavaWeb(四)

    今天使用maven clean, maven install 出现了下图问题,只解决了 maven clean , 还有maven install 今天 使用maven clean 出现以下问题(把下 ...

  9. 磁盘是随机存储设备,但不是随机存储器(RAM)。为什么?

    磁盘是随机存储设备,但不是随机存储器(RAM).为什么?

  10. P5239 回忆京都

    题目地址:P5239 回忆京都 杨辉三角即组合数的"打表"形式 再求一个二维前缀和 然后处理一下负数即可(因为在求前缀和的过程中有减法) #include <bits/std ...