Maison > développement back-end > Tutoriel Python > python 七种邮件内容发送方法实例

python 七种邮件内容发送方法实例

WBOY
Libérer: 2016-06-16 08:44:26
original
1206 Les gens l'ont consulté

一、文件形式的邮件

复制代码 代码如下:
#!/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') <br><br>msg['Subject'] = subject <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msg.as_string())<br>smtp.quit()
<p><strong>三、带图片的HTML邮件<br></strong></p><div class="codetitle">
<span><u>复制代码</u></span> 代码如下:<div class="codebody" id="code58012">
<br>#!/usr/bin/env python3<br>#coding: utf-8<br>import smtplib<br>from email.mime.multipart import MIMEMultipart<br>from email.mime.text import MIMEText<br>from email.mime.image import MIMEImage <br><br>sender = '***'<br>receiver = '***'<br>subject = 'python email test'<br>smtpserver = 'smtp.163.com'<br>username = '***'<br>password = '***' <br><br>msgRoot = MIMEMultipart('related')<br>msgRoot['Subject'] = 'test message' <br><br>msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img alt="" src="cid:image1"><br>good!','html','utf-8')<br>msgRoot.attach(msgText) <br><br>fp = open('h:\\python\\1.jpg', 'rb')<br>msgImage = MIMEImage(fp.read())<br>fp.close() <br><br>msgImage.add_header('Content-ID', '')<br>msgRoot.attach(msgImage) <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msgRoot.as_string())<br>smtp.quit()
<p><strong>四、带附件的邮件<br></strong></p>
<div class="codetitle">
<span><u>复制代码</u></span> 代码如下:<div class="codebody" id="code97533">#!/usr/bin/env python3<br>#coding: utf-8<br>import smtplib<br>from email.mime.multipart import MIMEMultipart<br>from email.mime.text import MIMEText<br>from email.mime.image import MIMEImage <br><br>sender = '***'<br>receiver = '***'<br>subject = 'python email test'<br>smtpserver = 'smtp.163.com'<br>username = '***'<br>password = '***' <br><br>msgRoot = MIMEMultipart('related')<br>msgRoot['Subject'] = 'test message' <br><br>#构造附件<br>att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')<br>att["Content-Type"] = 'application/octet-stream'<br>att["Content-Disposition"] = 'attachment; filename="1.jpg"'<br>msgRoot.attach(att) <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msgRoot.as_string())<br>smtp.quit()
<p><strong>五、群邮件<br></strong></p>
<div class="codetitle">
<span><u>复制代码</u></span> 代码如下:<div class="codebody" id="code78971">#!/usr/bin/env python3<br>#coding: utf-8<br>import smtplib<br>from email.mime.text import MIMEText <br><br>sender = '***'<br>receiver = ['***','****',……]<br>subject = 'python email test'<br>smtpserver = 'smtp.163.com'<br>username = '***'<br>password = '***' <br><br>msg = MIMEText('你好','text','utf-8') <br><br>msg['Subject'] = subject <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msg.as_string())<br>smtp.quit()<br><br><strong>六、各种元素都包含的邮件<br></strong><div class="codetitle">
<span><u>复制代码</u></span> 代码如下:<div class="codebody" id="code61217">#!/usr/bin/env python3<br>#coding: utf-8<br>import smtplib<br>from email.mime.multipart import MIMEMultipart<br>from email.mime.text import MIMEText<br>from email.mime.image import MIMEImage <br><br>sender = '***'<br>receiver = '***'<br>subject = 'python email test'<br>smtpserver = 'smtp.163.com'<br>username = '***'<br>password = '***' <br><br># Create message container - the correct MIME type is multipart/alternative.<br>msg = MIMEMultipart('alternative')<br>msg['Subject'] = "Link" <br><br># Create the body of the message (a plain-text and an HTML version).<br>text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"<br>html = """\<br><br> <br>Hi!<br><br>       How are you?<br><br>       Here is the <a href="http://www.python.org">link</a> you wanted.<br><br> <br><br>""" <br><br># Record the MIME types of both parts - text/plain and text/html.<br>part1 = MIMEText(text, 'plain')<br>part2 = MIMEText(html, 'html') <br><br># Attach parts into message container.<br># According to RFC 2046, the last part of a multipart message, in this case<br># the HTML message, is best and preferred.<br>msg.attach(part1)<br>msg.attach(part2)<br>#构造附件<br>att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')<br>att["Content-Type"] = 'application/octet-stream'<br>att["Content-Disposition"] = 'attachment; filename="1.jpg"'<br>msg.attach(att) <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msg.as_string())<br>smtp.quit()
<p><strong>七、基于SSL的邮件</strong></p>
<p></p>
<div class="codetitle">
<span><u>复制代码</u></span> 代码如下:<div class="codebody" id="code87920">#!/usr/bin/env python3<br>#coding: utf-8<br>import smtplib<br>from email.mime.text import MIMEText<br>from email.header import Header<br>sender = '***'<br>receiver = '***'<br>subject = 'python email test'<br>smtpserver = 'smtp.163.com'<br>username = '***'<br>password = '***' <br><br>msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要<br>msg['Subject'] = Header(subject, 'utf-8') <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.ehlo()<br>smtp.starttls()<br>smtp.ehlo()<br>smtp.set_debuglevel(1)<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msg.as_string())<br>smtp.quit()


</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal