1. 程式人生 > >python發送郵件方法

python發送郵件方法

lose 接收郵件 jpg Language .com msg 顯示 end multi

python發送郵件方法

1、普通文本郵件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
mail_user="[email protected]" #發送郵件的郵箱
mail_pass="xxxxxxx" #密碼,口令
mailto_list="[email protected]" #接受郵件的郵箱
mail_host="smtp.126.com" #設置服務器 例:smtp.126.com

strstr=‘你好‘ #內容
msg = MIMEText(strstr,‘plain‘,‘utf-8‘) #郵件類型設置為plain
msg[‘Subject‘] = "主題" #主題
msg[‘From‘] = mail_user
msg[‘To‘] = mailto_list
#郵件中文如果顯示亂碼,可以加上下面兩句
msg["Accept-Language"]="zh-CN"
msg["Accept-Charset"]="ISO-8859-1,utf-8"

server = smtplib.SMTP()
server.connect(mail_host) #連接smtp郵件服務器
server.login(mail_user,mail_pass) #登錄
server.sendmail(mail_user, mailto_list, msg.as_string()) #發送
server.close() #關閉

2、HTML格式郵件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
mail_user="[email protected]" #發送郵件的郵箱
mail_pass="xxxxxx" #口令
mailto_list="[email protected]" #接收郵件的郵箱
to_list=[mailto_list,]
mail_host="smtp.126.com" #設置服務器

msg = MIMEMultipart()
msg[‘Subject‘] = "主題" #主題
msg[‘From‘] = mail_user
msg[‘To‘] = mailto_list
#正文
#<img src="cid:image1">為圖片顯示位置
strstr="""
<html>
<head>正文</head>
<body>
<h1>Hello</h1>
<h2>你們好</h2>
</body>
</html>
"""
htm=MIMEText(strstr,‘html‘,‘utf-8‘) #郵件類型設置為html
msg.attach(htm)

server = smtplib.SMTP()
server.connect(mail_host) #連接smtp郵件服務器
server.login(mail_user,mail_pass) #登錄
server.sendmail(mail_user, to_list, msg.as_string()) #發送
server.close() #關閉

3、帶附件的郵件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
mail_user="[email protected]" #發送郵件的郵箱
mail_pass="xxxxxxx" #口令
mailto_list="[email protected]" #接收郵件的郵箱
to_list=[mailto_list,]
mail_host="smtp.126.com" #設置服務器

msg = MIMEMultipart()
msg[‘Subject‘] = "主題" #主題
msg[‘From‘] = mail_user
msg[‘To‘] = mailto_list
#文字部分
strstr="Hello" #文字內容
att = MIMEText(strstr,‘plain‘,‘utf-8‘)
msg.attach(att)
#附件
att = MIMEApplication(open(‘E:\\111.txt‘,‘rb‘).read()) #你要發送的附件地址
att.add_header(‘Content-Disposition‘, ‘attachment‘, filename="222.txt") #filename可隨意取名
msg.attach(att)

server = smtplib.SMTP()
server.connect(mail_host) #連接smtp郵件服務器
server.login(mail_user,mail_pass) #登錄
server.sendmail(mail_user, to_list, msg.as_string()) #發送
server.close() #關閉

4、正文顯示圖片的郵件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
mail_user="[email protected]" #發送郵件的郵箱
mail_pass="xxxxxx" #口令
mailto_list="[email protected]" #接收郵件的郵箱
to_list=[mailto_list,]
mail_host="smtp.126.com" #設置服務器

msg = MIMEMultipart()
msg[‘Subject‘] = "主題" #主題
msg[‘From‘] = mail_user
msg[‘To‘] = mailto_list
#正文
#<img src="cid:image1">為圖片顯示位置
strstr="""
<html>
<head>正文圖片</head>
<body>
<p>Hello<br>
你們好<br>
<br><img src="cid:image1"></br>
</p>
</body>
</html>
"""
htm=MIMEText(strstr,‘html‘,‘utf-8‘)
msg.attach(htm)

image = MIMEImage(open("F:\\111.jpg",‘rb‘).read())
image.add_header(‘Content-ID‘,‘<image1>‘)
msg.attach(image)

server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(mail_user,mailto_list,msg.as_string())
server.quit()

python發送郵件方法