Python 发送邮件

ref: 1. http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html      2. http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832745198026a685614e7462fb57dbf733cc9f3ad000

3.http://www.runoob.com/python/python-email.html

import smtplib
from email.mime.text import MIMEText

sender=‘[email protected]‘
user_name=‘[email protected]‘
passwd=‘*****‘
smtp_server=‘smtp.163.com‘

def send_mail(*receivers):
    msg=MIMEText(‘hello‘,_subtype=‘plain‘,_charset=‘utf-8‘)

    ‘‘‘

    msg=MIMEText(‘<html><body>hello</body></html>‘,
                ‘html‘,‘utf-8‘)
    ‘‘‘

    s=smtplib.SMTP()
    s.connect(smtp_server)
    s.login(user_name,passwd)
    s.sendmail(sender,‘;‘.join(receivers),msg.as_string())

if __name__==‘__main__‘:
    send_mail(‘****@163.com‘)
"""
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

def sendMail(sender,reciver,subject):
    username="***@163.com"
    password="*****"

    html="""
        <html>
            <body>
                <img src="cid:img">
            </body>
        </html> 

        """
    htm=MIMEText(html,‘html‘,‘utf-8‘)
    msg.attach(htm)   #添加邮件正文

    att_htm=MIMEText(‘hello‘,‘plain‘,‘utf-8‘)
    att_htm["Content-Disposition"] = ‘attachment; filename=%s‘ %‘1.txt‘
    msg.attach(att_htm)  #添加附件,附件为文本

    att_img=MIMEImage(open(‘/home/lance/1.jpg‘,‘rb‘).read())
    att_img["Content-Disposition"] = ‘attachment; filename=%s‘ %‘1.jpg‘
    msg.attach(att_img)  #添加附件,附件为图片

    img=MIMEImage(open(‘/home/lance/1.jpg‘,‘rb‘).read())
    img["Content-ID"]= "<img>"
    msg.attach(img)     #添加图片,图片显示在正文,不在附件,正文中的 <img src="cid:img">引用

    msg[‘From‘]="***@163.com"
    msg[‘To‘]=";".join(reciver)

    smtp=smtplib.SMTP()
    smtp.connect("smtp.163.com")
    smtp.login(username, password)
    smtp.sendmail(sender, reciver, msg.as_string())
    smtp.quit()

if __name__=="__main__":

    try:
        sendMail(‘***@163.com‘, [‘***@163.com‘], subject=‘subject‘)
        print ‘发送成功‘
    except Exception,e:
        print str(e)
msg attach的顺序很重要,先添加邮件正文,其次是附件....
时间: 2024-08-15 21:14:29

Python 发送邮件的相关文章

【转载】python发送邮件实例

本文转自:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html 这几天要用python发送邮件,上网找到这篇文章感觉蛮全面的,故转载收藏之. 1. 文件形式的邮件 #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header send

python发送邮件(一)

最近设计了一个小的应用程序,主要是根据文件中邮件地址发送一份excel中内容,并且在接受方收到邮件都是以网页的格式呈现的. 下面主要是对python发送邮件涉及到的部分知识点做个总结 一.先介绍一下Smtp协议和POP3协议 SMTP (Simple Mail Transfer Protocol) http://www.rfc-editor.org/info/rfc821    RFC821文档详细描述了这个协议信息: 邮件传送代理 (Mail Transfer Agent,MTA) 程序使用S

解读Python发送邮件

解读Python发送邮件 Python发送邮件需要smtplib和email两个模块.也正是由于我们在实际工作中可以导入这些模块,才使得处理工作中的任务变得更加的简单.今天,就来好好学习一下使用Python发送邮件吧. SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件. 1.邮件正文是文本的格式 1 # -*- codi

python发送邮件的脚本

python发送邮件的脚本,带有邮件内容与附件,邮件内容为串格式,附件为文件.如果想把某个目录下的所有文件当作附件发送,那请去掉注释. 代码如下: #!/usr/bin/python #coding utf-8 from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email import Utils, E

使用python发送邮件

最近需要做一个功能,统计bug的数量,然后发邮件给指定人,所有就先要了解一下使用python发送邮件 代码如下: #coding: utf-8 import smtplib from email.MIMEText import MIMETextfrom email.Header import Header sender = '[email protected]'       #发件人 receiver = ['[email protected]','[email protected]']   #

python 发送邮件例子

想到用python发送邮件 主要是服务器 有时候会产生coredump文件  ,然后因为脚本重启原因,服务器coredump产生后会重启 但是没有主动通知开发人员 想了下可以写个脚本一旦产生coredump文件就可以发送邮件给开发者让其立马知道 下面只介绍简单的发送脚本 如果需要在生产环境用起来  还需要按要求修改脚本 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]]) SMTP类构造函数,表示与SMTP服务器之间的连接,通过这个连

PHP调用Python发送邮件

1 简介 在PHP中发送邮件,通常都是封装一个php的smtp邮件类来发送邮件.但是PHP底层的socket编程相对于python来说效率是非常低的.CleverCode同时写过用python写的爬虫抓取网页,和用php写的爬虫抓取网页.发现虽然用了php的curl抓取网页,但是涉及到超时,多线程同时抓取等等.不得不说python在网络编程的效率要比PHP好的多. PHP在发送邮件时候,自己写的smtp类,发送的效率和速度都比较低.特别是并发发送大量带有附件报表的邮件的时候.php的效率很低.建

Python发送邮件(常见四种邮件内容)

Python发送邮件(常见四种邮件内容) 转载 2017年03月03日 17:17:04 转自:http://lizhenliang.blog.51cto.com/7876557/1875330 在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下,Shell脚本发送邮件告警是件很简单的事,有现成的邮件服务软件或者调用运营商邮箱服务器. 对于Python来说,需要编写脚本调用邮件服务器来发送邮件,使用的协议是SMTP.

【转】【Python】Python发送邮件(常见四种邮件内容)

感谢:梦琪小生的<[转][Python]Python发送邮件(常见四种邮件内容)> 里面详细介绍了Python中发送邮件的方法,以供自己参考 原文地址:https://www.cnblogs.com/Owen-ET/p/8423168.html

python 发送邮件及smtplib.SMTPAuthenticationError 503 错误处理

发送失败错误1:smtplib.SMTPAuthenticationError:    我们使用python发送邮件时相当于自定义客户端根据用户名和密码登录,然后使用SMTP服务发送邮件,邮箱是默认不开启客户端授权的,因此登录总是被拒绝,解决办法(以qq邮箱为例):进入qq邮箱-设置-客户端授权密码-开启(授权码是用于登录第三方邮件客户端的专用密码),非第三方登录密码不变. 原文地址:https://www.cnblogs.com/sixing/p/8665286.html