Python發送郵件

使用email模塊和smtplib模塊,內容比較固定,配好了便可實現,代碼以下:python

1、普通郵件發送
import smtplib
from email.mime.text import MIMEText
email_host = 'smtp.163.com' #郵箱地址
email_user = 'XXX@163.com' # 發送者帳號
email_pwd = 'XXX' # 發送者的密碼
maillist ='XXX@XXXX.com'
#收件人郵箱,多個帳號的話,用逗號隔開
me = email_user
msg = MIMEText('這是個python測試郵件,不用回覆。') # 郵件內容
msg['Subject'] = 'python測試' # 郵件主題
msg['From'] = me # 發送者帳號
msg['To'] = maillist # 接收者帳號列表
smtp = smtplib.SMTP(email_host,port=25) # 鏈接郵箱,傳入郵箱地址,和端口號,smtp的端口號是25
smtp.login(email_user, email_pwd) # 發送者的郵箱帳號,密碼
smtp.sendmail(me, maillist, msg.as_string())
# 參數分別是發送者,接收者,第三個是把上面的發送郵件的內容變成字符串
smtp.quit() # 發送完畢後退出smtp
print ('email send success.')

注意
1. 發送者密碼這裏不是平時登陸郵箱的密碼,而是開啓登陸第三方郵件客戶端的受權碼。
2. 多數郵箱的smtp的端口號都是25,個別的請具體確認。


發郵件的代碼封裝成函數:

import smtplib
from email.mime.text import MIMEText


def send_mail(username, passwd, recv, title, content, mail_host='smtp.163.com', port=25):
'''
發送郵件函數,默認使用163smtp
:param username: 郵箱帳號 xx@163.com
:param passwd: 郵箱密碼
:param recv: 郵箱接收人地址,多個帳號以逗號隔開
:param title: 郵件標題
:param content: 郵件內容
:param mail_host: 郵箱服務器
:param port: 端口號
:return:
'''
msg = MIMEText(content) # 郵件內容
msg['Subject'] = title # 郵件主題
msg['From'] = username # 發送者帳號
msg['To'] = recv # 接收者帳號列表
smtp = smtplib.SMTP(mail_host, port=port) # 鏈接郵箱,傳入郵箱地址,和端口號,smtp的端口號是25
smtp.login(username, passwd) # 發送者的郵箱帳號,密碼
smtp.sendmail(username, recv, msg.as_string())
# 參數分別是發送者,接收者,第三個是把上面的發送郵件的內容變成字符串
smtp.quit() # 發送完畢後退出smtp
print('email send success.')


email_user = 'xxxx@163.com' # 發送者帳號
email_pwd = 'xxxxx' # 發送者密碼
maillist = 'XXX@XXX.com'
title = '測試郵件標題'
content = '這裏是郵件內容'
send_mail(email_user, email_pwd, maillist, title, content)

2、髮帶附件的郵件

import smtplib
#smtplib這個模塊是管發郵件
from email.mime.text import MIMEText
#構造郵件內容
from email.mime.multipart import MIMEMultipart
#髮帶附件的郵件用的
email_host = 'smtp.163.com' #郵箱服務器地址
email_user = 'XXX@163.com' # 發送者帳號
email_pwd = 'XXX'
# 發送者密碼是郵箱的受權碼,不是登陸的密碼
maillist = 'XXX@XXX.com'
#收件人郵箱,多個帳號的話,用逗號隔開
new_msg = MIMEMultipart()
#構建了一個能發附件的郵件對象
new_msg.attach(MIMEText('這是Python測試發郵件的郵件,不要回復'))
# 郵件內容
new_msg['Subject'] = 'Python測試郵件帶附件' # 郵件主題
new_msg['From'] = email_user # 發送者帳號
new_msg['To'] = maillist # 接收者帳號列表
att = MIMEText(open('like_report.txt').read())
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="haha.txt"'
new_msg.attach(att)
smtp = smtplib.SMTP(email_host,port=25) # 鏈接郵箱,傳入郵箱地址,和端口號,smtp的端口號是25
smtp.login(email_user, email_pwd) # 發送者的郵箱帳號,密碼
smtp.sendmail(email_user, maillist, new_msg.as_string())
# 參數分別是發送者,接收者,第三個是把上面的發送郵件的內容變成字符串
smtp.quit() # 發送完畢後退出smtp
print ('email send success.')

3、封裝發送郵件的類並驗證

class SendMail(object):    def __init__(self,username,passwd,recv,title,content,                 file=None,                 email_host='smtp.163.com',port=25):        self.username = username        self.passwd = passwd        self.recv = recv        self.title = title        self.content = content        self.file = file        self.email_host = email_host        self.port = port    def send_mail(self):        msg = MIMEMultipart()        #發送內容的對象        if self.file:#處理附件的            att = MIMEText(open(self.file).read())            att["Content-Type"] = 'application/octet-stream'            att["Content-Disposition"] = 'attachment; filename="%s"'%self.file            msg.attach(att)        msg.attach(MIMEText(self.content))#郵件正文的內容        msg['Subject'] = self.title  # 郵件主題        msg['From'] = self.username  # 發送者帳號        msg['To'] = self.recv  # 接收者帳號列表        self.smtp = smtplib.SMTP(self.email_host,port=self.port)        #發送郵件服務器的對象        self.smtp.login(self.username,self.passwd)        try:            self.smtp.sendmail(self.username,self.recv,msg.as_string())        except Exception as e:            print('出錯了。。',e)        else:            print('發送成功!')    def __del__(self):        self.smtp.quit()if __name__ == '__main__':    m = SendMail(        username='XXX@163.com',passwd='XXX',recv='XXX@XXX.com',        title='新鞋的發送郵件',content='哈哈哈啊哈哈哈哈',file='like_report.txt'    )    m.send_mail()
相關文章
相關標籤/搜索