您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

python发送邮件的实例代码(支持html、图片、附件)

2025/9/9 14:13:55发布14次查看
第一段代码:
复制代码 代码如下:
#!/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 = 'username@somehost.com'
        toadd = ['someone@somehost.com', 'other@somehost.com']
        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 mimetextsender = '***'
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 mimeimagesender = '***'
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 mimeimagesender = '***'
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 mimetextsender = '***'
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 mimeimagesender = '***'
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()
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product