python 发送邮件 图片显示问题_python发送邮件的实例代码(支持html、图片、附件)..._weixin_39583162的博客-程序员宝宝

技术标签: python 发送邮件 图片显示问题  

#!/usr/bin/python

# -*- coding: utf-8 -*-

import email

import mimetypes

from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

from email.MIMEImage import MIMEImage

import smtplib

def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText):

strFrom = fromAdd

strTo = ', '.join(toAdd)

server = authInfo.get('server')

user = authInfo.get('user')

passwd = authInfo.get('password')

if not (server and user and passwd) :

print 'incomplete login info, exit now'

return

# 设定root信息

msgRoot = MIMEMultipart('related')

msgRoot['Subject'] = subject

msgRoot['From'] = strFrom

msgRoot['To'] = strTo

msgRoot.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an

# 'alternative' part, so message agents can decide which they want to display.

msgAlternative = MIMEMultipart('alternative')

msgRoot.attach(msgAlternative)

#设定纯文本信息

msgText = MIMEText(plainText, 'plain', 'utf-8')

msgAlternative.attach(msgText)

#设定HTML信息

msgText = MIMEText(htmlText, 'html', 'utf-8')

msgAlternative.attach(msgText)

#设定内置图片信息

fp = open('test.jpg', 'rb')

msgImage = MIMEImage(fp.read())

fp.close()

msgImage.add_header('Content-ID', '')

msgRoot.attach(msgImage)

#发送邮件

smtp = smtplib.SMTP()

#设定调试级别,依情况而定

smtp.set_debuglevel(1)

smtp.connect(server)

smtp.login(user, passwd)

smtp.sendmail(strFrom, strTo, msgRoot.as_string())

smtp.quit()

return

if __name__ == '__main__' :

authInfo = {}

authInfo['server'] = 'smtp.somehost.com'

authInfo['user'] = 'username'

authInfo['password'] = 'password'

fromAdd = '[email protected]'

toAdd = ['[email protected]', '[email protected]']

subject = '邮件主题'

plainText = '这里是普通文本'

htmlText = 'HTML文本'

sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText)

文件形式的邮件

复制代码 代码如下:

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

from email.header import Header

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要

msg['Subject'] = Header(subject, 'utf-8')

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

HTML形式的邮件

复制代码 代码如下:

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('

你好','html','utf-8')

msg['Subject'] = subject

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

带图片的HTML邮件

复制代码 代码如下:

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msgRoot = MIMEMultipart('related')

msgRoot['Subject'] = 'test message'

msgText = MIMEText('Some HTML text and an image.

good!','html','utf-8')

msgRoot.attach(msgText)

fp = open('h:\\python\\1.jpg', 'rb')

msgImage = MIMEImage(fp.read())

fp.close()

msgImage.add_header('Content-ID', '')

msgRoot.attach(msgImage)

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msgRoot.as_string())

smtp.quit()

带附件的邮件

复制代码 代码如下:

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msgRoot = MIMEMultipart('related')

msgRoot['Subject'] = 'test message'

#构造附件

att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')

att["Content-Type"] = 'application/octet-stream'

att["Content-Disposition"] = 'attachment; filename="1.jpg"'

msgRoot.attach(att)

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msgRoot.as_string())

smtp.quit()

群邮件

复制代码 代码如下:

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

sender = '***'

receiver = ['***','****',……]

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('你好','plain','utf-8')

msg['Subject'] = subject

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

各种元素都包含的邮件

复制代码 代码如下:

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

# Create message container - the correct MIME type is multipart/alternative.

msg = MIMEMultipart('alternative')

msg['Subject'] = "Link"

# Create the body of the message (a plain-text and an HTML version).

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"

html = """\

Hi!

How are you?

Here is the link you wanted.

"""

# Record the MIME types of both parts - text/plain and text/html.

part1 = MIMEText(text, 'plain')

part2 = MIMEText(html, 'html')

# Attach parts into message container.

# According to RFC 2046, the last part of a multipart message, in this case

# the HTML message, is best and preferred.

msg.attach(part1)

msg.attach(part2)

#构造附件

att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')

att["Content-Type"] = 'application/octet-stream'

att["Content-Disposition"] = 'attachment; filename="1.jpg"'

msg.attach(att)

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

基于SSL的邮件

复制代码 代码如下:

#!/usr/bin/env python3

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

from email.header import Header

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

msg = MIMEText('你好','plain','utf-8')#中文需参数‘utf-8',单字节字符不需要

msg['Subject'] = Header(subject, 'utf-8')

smtp = smtplib.SMTP()

smtp.connect('smtp.163.com')

smtp.ehlo()

smtp.starttls()

smtp.ehlo()

smtp.set_debuglevel(1)

smtp.login(username, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

相关文章

相关视频

网友评论

文明上网理性发言,请遵守 新闻评论服务协议我要评论

立即提交

专题推荐独孤九贱-php全栈开发教程

全栈 100W+

主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门

玉女心经-web前端开发教程

入门 50W+

主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门

天龙八部-实战开发教程

实战 80W+

主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习

php中文网:公益在线php培训,帮助PHP学习者快速成长!

Copyright 2014-2020 https://www.php.cn/ All Rights Reserved | 苏ICP备2020058653号-1

  

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39583162/article/details/111513385

智能推荐

数码服务器怎么备份文件,男人的生产力工具 篇一百六十七:群晖数据备份讲堂:Hyper Backup数据还原教程..._道路维护员的博客-程序员宝宝

男人的生产力工具 篇一百六十七:群晖数据备份讲堂:Hyper Backup数据还原教程2019-10-07 16:32:0029点赞116收藏27评论对数据还原的需求男人的生产力工具 篇一百二十一:群晖数据备份讲堂:全能备份工具--Hyper Backup(一)数据无价在分享了个人存储方案的踩坑经验后,各位读者反响热烈。有高手指出:解决了数据的存储之后,数据的保护是否考虑过?也有读者觉得,文章中关...

linux java8 安装包(版本8u131-b11)_jdk8u131下载_摸鱼Java开发的博客-程序员宝宝

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz标记一下

快看,2021 IEEE Fellow出炉!这些华人科学家太强了!_碎碎思的博客-程序员宝宝

2020年11月25日,2021年IEEE Fellow名单正式揭晓,共有282名科学家新晋Fellow 。华人科学家占比近30%。IEEE Fellow当选难度很大IEEE致力于电气、电...

PostgreSQL下载安装_pg数据库下载安装_千叶公子的博客-程序员宝宝

PostgreSQL下载安装1、下载2、安装3、ip设置1、下载LINUX为例:1、pgsql官网地址:https://www.postgresql.org/下载页面地址:https://www.enterprisedb.com/download-postgresql-binaries2、点击download,选择对应系统及相应的Linux发行版3、ctrl+f 直接搜索 tar.gz archive ,点击进入4、选择版本下载2、安装1、上传安装包到服务器:rz -b 2、

springsecurity中的自定义反序列化_戏子☜已入画@的博客-程序员宝宝

1.实体类实现了Userdetails接口,重写了几个方法,但是admin实体类没有Collection<? extends GrantedAuthority>这个属性,json就不能反序列化这个获得角色的类@[email protected](callSuper = false)@Accessors(chain = true)@TableName("t_admin")@ApiModel(value="Admin对象", description="")public

json与java反射_将一组JSON对象映射到java.util.Map,反之亦然_自动抬杠机的博客-程序员宝宝

如何反序列化JSON字符串您可以使用Jackson来反序列化JSON字符串:例如,如果你有类Foopublic class Foo {private Bar[] items;// Constructor / Getters & Setters}那个类有一个类Bar的数组public class Bar {private int field1;private String field2;//...

随便推点

Server-Side XSS Attack Detection with ModSecurity and PhantomJS_using phatomjs xss_cnbird2008的博客-程序员宝宝

Client-Side JS Overriding LimitationsIn a previous blog post, I outlined how you could use ModSecurity to inject defensive JS into the HTML response page sent to the client web browser.  The goal

41个 IE layout BUG_bennman的博客-程序员宝宝

在开发中我们常会在ie中遇到很多莫名的bug,尤其是ie6这个早就该退出历史的浏览器。刚刚在haslayout.net上看到有总结ie的一些 bug,分享一下。1.图片label bug,影响版本ie6/ie7/ie8,当label中有img的时候无法触发点击选中form元素事件.demo [url=http://haslayout.net/demos/Image-Label-Focus-Bug-Demo.html]http://haslayout.net/demos/Image-Label-F

DTU与组态王组态软件配置说明_组态王dtu_佰马科技-罗毅的博客-程序员宝宝

通过DTU来实现组态软件的远程传输控制. 现在物联网技术应用普及,各类使用组态软件都有一定要求实现无线远程传输,下面通过佰马DTU与组态王的例子实现远程无线传输配置说明佰马BMD100 DTU连接组态王软件的具体应用如下图所示:前端设备(PLC、采集器、传感器等)通过串口(RS232、RS485、IO、ADC)连接佰马BMD100 DTU,DTU再通过2G/3G/4G网络,将前端采集的数据...

第三章_字符串向量和数组_3.5数组_奶油麦片糖的博客-程序员宝宝

3.5 数组3.5 数组3.5.1 定义和初始化内置数组3.5.2 访问数组元素3.5.3 指针和数组3.5.4 C 风格字符串3.5.5 与旧代码的接口3.5 数组数组的大小确定不变,不能随意向数组中增加元素。3.5.1 定义和初始化内置数组数组的声明形如a[d],其中a是数组的名字,d是数组的维度。维度说明的是数组中元素的个数,必须大于 0 。维度必须是一个常量表达式。显式初始化数组元素可以对数组的元素进行列表初始化,此时允许忽略数组的维度。const unsigned sz = 3;i

关于JObject的用法,以及实现动态生成实体对象、动态创建一些Josn组合_BruceYang~的博客-程序员宝宝

通过JObject和JArray创建JSON对象我们先用非常简单的方法来动态创建一些JSON,可通过JToken派生的JSON.NET对象来进行,最常见的JToken派生的类是JObject和JArray。正是因为JToken实现了IDynamicMetaProvider动态语言接口,所以可以使用dynamic关键字直观地创建动态对象,并把这个动态对象序列化为JSON字符串。 我们通过JAr...

SpringMVC和Struts2的区别及优势_weixin_30270889的博客-程序员宝宝

1.SpringMVC和Struts2的区别比较1、Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上SpringMVC就容易实现restful url,而struts2的架构实现起来要费劲,因为Struts2中Action的一个方法可以对应一个...

推荐文章

热门文章

相关标签