1. 程式人生 > >python傳送郵件

python傳送郵件

轉載自後知者的部落格 使用email模組和smtplib模組,內容比較固定,配好了即可實現,程式碼如下:

一、普通郵件傳送 import smtplib from email.mime.text import MIMEText email_host = ‘smtp.163.com’ #郵箱地址 email_user = ‘[email protected]’ # 傳送者賬號 email_pwd = ‘XXX’ # 傳送者的密碼 maillist =‘[email protected]’ #收件人郵箱,多個賬號的話,用逗號隔開 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: 郵箱賬號 [email protected] :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 = ‘[email protected]’ # 傳送者賬號 email_pwd = ‘xxxxx’ # 傳送者密碼 maillist = ‘[email protected]’ title = ‘測試郵件標題’ content = ‘這裡是郵件內容’ send_mail(email_user, email_pwd, maillist, title, content)

二、髮帶附件的郵件

import smtplib #smtplib這個模組是管發郵件 from email.mime.text import MIMEText #構造郵件內容 from email.mime.multipart import MIMEMultipart #髮帶附件的郵件用的 email_host = ‘smtp.163.com’ #郵箱伺服器地址 email_user = ‘[email protected]’ # 傳送者賬號 email_pwd = ‘XXX’

傳送者密碼是郵箱的授權碼,不是登入的密碼

maillist = ‘[email protected]’ #收件人郵箱,多個賬號的話,用逗號隔開 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.’)

三、封裝傳送郵件的類並驗證

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=‘[email protected]’,passwd=‘XXX’,recv=‘[email protected]’, title=‘新鞋的傳送郵件’,content=‘哈哈哈啊哈哈哈哈’,file=‘like_report.txt’ ) m.send_mail()