100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > html发照片的文本实例 python发送邮件的实例代码(支持html 图片 附件) -电脑资料...

html发照片的文本实例 python发送邮件的实例代码(支持html 图片 附件) -电脑资料...

时间:2019-12-31 00:24:12

相关推荐

html发照片的文本实例 python发送邮件的实例代码(支持html 图片 附件) -电脑资料...

第一段代码:

复制代码代码如下:

#!/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'] = ''

authInfo['user'] = 'username'

authInfo['password'] = 'password'

fromAdd = 'username@'

toAdd = ['someone@', 'other@']

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 = ''

username = '***'

password = '***'

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

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

smtp = smtplib.SMTP()

smtp.connect('')

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 = ''

username = '***'

password = '***'

msg = MIMEText('

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

msg['Subject'] = subject

smtp = smtplib.SMTP()

smtp.connect('')

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 = ''

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.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 = ''

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.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 = ''

username = '***'

password = '***'

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

msg['Subject'] = subject

smtp = smtplib.SMTP()

smtp.connect('')

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 = ''

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:\n"

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.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 = ''

username = '***'

password = '***'

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

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

smtp = smtplib.SMTP()

smtp.connect('')

smtp.ehlo()

smtp.starttls()

smtp.ehlo()

smtp.set_debuglevel(1)

smtp.login(username, password)

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

smtp.quit()

您可能感兴趣的文章:

使用python解析xml成对应的html示例分享

python使用win32com在 插入html元素示例

python解析html开发库pyquery使用方法

python抓取某汽车网数据解析html存入excel示例

python将xml xsl文件生成html文件存储示例讲解

使用python提取html文件中的特定数据的实现代码

python将html转成PDF的实现代码(包含中文)

python 解析html之BeautifulSoup

python读取html中指定元素生成excle文件示例

QQ空间 搜狐微博 人人网 开心网 百度搜藏更多

Tags:python发送邮件

复制链接收藏本文打印本文关闭本文返回首页

上一篇:python用ConfigObj读写配置文件的实现代码

下一篇:windows下wxPython开发环境安装与配置方法

相关文章

-04-04django自定义Field实现一个字段存储以逗号分隔的字符串

-05-05python生成指定尺寸缩略图的示例

-12-12Python yield使用方法示例

-03-03详解Python中的__init__和__new__

-03-03python用ConfigObj读写配置文件的实现代码

-12-12天翼开放平台免费短信验证码接口使用实例

-02-02简明 Python 基础学习教程

-06-06Python开发的单词频率统计工具wordsworth使用方法

-03-03python3.3使用tkinter开发猜数字游戏示例

-03-03python的几种开发工具介绍

文章评论

最 近 更 新

Python使用Socket(Https)Post登录百度的实

python 参数列表中的self 显式不等于冗余

复制粘贴功能的Python程序

Python 文件读写操作实例详解

Python实例分享:快速查找出被挂马的文件

python连接mongodb操作数据示例(mongodb数

pycharm 使用心得(四)显示行号

python函数缺省值与引用学习笔记分享

Python程序设计入门(4)模块和包

python基础入门详解(文件输入/输出 内建类

热 点 排 行

Python入门教程 超详细1小时学会

python 中文乱码问题深入分析

比较详细Python正则表达式操作指

Python字符串的encode与decode研

Python open读写文件实现脚本

Python enumerate遍历数组示例应

Python 深入理解yield

Python+Django在windows下的开发

python 文件和路径操作函数小结

python 字符串split的用法分享

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。