1. 程式人生 > >Python 發電子郵件示例

Python 發電子郵件示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# python 3.5

import re
import os
import smtplib
from io import StringIO
from validate_email import validate_email
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

class PYMAIL:
	def __init__(self):
		self.from_mail = "[email protected]
" self.from_mail_pwd = "xxxxx" self.smtp_server = "smtp.139.com" self.smtp_port = 25 """ def send_mail(self, to_mail,subject,content): try: msg = MIMEText( content ,'plain','utf-8') msg['From'] = self.from_mail msg['To'] = to_mail msg['Subject'] = subject smtp = smtplib.SMTP(self.smtp_server, self.smtp_port) smtp.starttls() smtp.login(self.from_mail , self.from_mail_pwd) smtp.sendmail(self.from_mail, to_mail, msg.as_string()) smtp.quit() print("傳送成功!") except smtplib.SMTPException as err: print("傳送失敗!") print(str(err)) """ def check_mail(self ,maillist): to_maillist = [] if maillist: for mail in maillist: if re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', mail): if validate_email(mail,verify=True) != None: to_maillist.append(mail) else: raise Exception("Invalid email address: %s" % mail) else: raise Exception("Invalid email format: %s" % mail) else: raise Exception("Receive email can not be null!") return to_maillist #Can add attachment or not. def send_mail_with_attachment(self, to_mail,subject,content,filelist): to_maillist = self.check_mail(to_mail) try: msg = MIMEMultipart() msg['From'] = self.from_mail msg['To'] = ", ".join(to_maillist) msg['Subject'] = subject msg.attach(MIMEText( content ,'plain','utf-8')) #add attachment if filelist: for file in filelist: if os.path.isfile(file): path, filename = os.path.split(file) part = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8') part["Content-Disposition"] = 'attachment; filename="%s"' % os.path.basename(filename) msg.attach(part) else: raise Exception("The file [%s] is not exists!" % file) smtp = smtplib.SMTP(self.smtp_server, self.smtp_port) smtp.starttls() smtp.login(self.from_mail , self.from_mail_pwd) smtp.sendmail(self.from_mail, to_maillist, msg.as_string()) smtp.quit() print("Mail Success!") except smtplib.SMTPException as err: print("Mail failure!") print(str(err)) if __name__ == "__main__": to_mail = ["
[email protected]
","[email protected]"] subject = "test subject" content = "test mail!~" filelist = ["D:/Python35/mypy/test.py","D:/Python35/mypy/op.py"] mymail = PYMAIL() mymail.send_mail_with_attachment(to_mail,subject,content,filelist)

相關推薦

Python 電子郵件示例

#!/usr/bin/env python # -*- coding: utf-8 -*- # python 3.5 import re import os import smtplib from io import StringIO from validate_email

Python 電子郵件

使用的庫 這個程式涉及兩個庫:smtplib 和 email。 這兩個庫都是Python自帶的,所以不需要額外的下載安裝,此外,這次使用的Python版本為2.7。 思路步驟 總體思路很簡單,就像我們平常上網是通過HTTP協議一樣,我們傳送郵件是

python使用電子郵件模塊smtplib的方法(送圖片 附件)實用可行

文件的 main.c 發送郵件 open view 讀取圖片 alt 指令 main Smptp類定義:smtplib.SMTP(host[,port[,local_hostname[,,timeout]]]),作為SMTP的構造函數,功能是與smtp服務器建立連接,在連接

Python郵件

http ima 發送 images cnblogs 郵件 技術 讀取 logs 一、Python發送郵件,將文件中的內容讀取後放入郵件正文(不帶附件) 二、Python發送帶附件的郵件 Python發送郵件

python 郵件腳本

prepare env 郵件 如果 get sre esp pwd targe 一、該腳本適合在 linux 中做郵件發送測試用,只需要填寫好 發送賬號和密碼以及發送人即可,然後使用 python ./filename.py (當前目錄下)即可。如果發送出錯,會將錯誤詳情

python 郵件

utf-8 root str try finally tex pri con linu #!/usr/bin/env python#encoding=utf8#coding=utf-8import smtplib, sysreload(sys)sys.setdefaulte

使用Python郵件

加密 郵件 附件 不帶附件的方式發送郵件,代碼如下:#!/usr/bin/python #coding:utf8 from email.mime.text import MIMEText from email.header import Header from email.utils impor

Python郵件(常見四種郵件內容)

t對象 3.6 idt serve ttl dddd python tdi tls Python發送郵件(常見四種郵件內容) 轉載 2017年03月03日 17:17:04 轉自:http://lizhenliang.blog.51cto.com/7876557

python郵件腳本ssl 465端口

() test ring end ssl == python from port #coding:utf8 from smtplib import SMTP_SSL from email.header import Header from email.mime.tex

【轉】【PythonPython郵件(常見四種郵件內容)

.cn .com pytho html 常見 body gpo 詳細 tle 感謝:夢琪小生的《【轉】【Python】Python發送郵件(常見四種郵件內容)》 裏面詳細介紹了Python中發送郵件的方法,以供自己參考【轉】【Python】Python發送郵件(常見四種郵件

Java郵件示例

ise ble catch gbk 發送郵件 .get png content chan 利用Java發送郵件示例: 1、發送QQ郵件 1 import java.util.Properties; 2 import javax.mail.Message; 3 imp

Python郵件腳本

python本腳本使用的163郵箱:此腳本需要進入163郵箱設置客戶端授權密碼:mail.py腳本源碼:#!/usr/bin/env python #-*- coding: UTF-8 -*- import os,sys reload(sys) sys.setdefaultencoding('utf8

python 郵件及smtplib.SMTPAuthenticationError 503 錯誤處理

RR 不變 服務 cat 被拒絕 smtp服務 authent 使用 三方登錄 發送失敗錯誤1:smtplib.SMTPAuthenticationError: 我們使用python發送郵件時相當於自定義客戶端根據用戶名和密碼登錄,然後使用SMTP服務發送郵件,郵箱是

python 郵件(收到的郵件要有送方才能回復)

turn als password port 編碼格式 方法 n) multi 打開 Python使用SMTP(簡單郵件傳輸協議)發送郵件 普通文本郵件 普通文本郵件發送的實現,關鍵是要將MIMEText中_subtype設置為plain ## -*- coding: UT

使用python郵件的各種姿勢

import 服務 ocm text osi evel coder quit 編碼 SMTP是發送郵件的協議,Python內置對SMTP的支持,可以發送純文本郵件、HTML郵件以及帶附件的郵件。Python對SMTP支持有smtplib和email兩個模塊,email負責構

Python郵件:smtplib、sendmail

postfix prot 找到 serve qmail 問題 配置文件 什麽 分享圖片 本地Ubuntu 18.04,本地Python 3.6.5, 阿裏雲Ubuntu 16.04,阿裏雲Python 3.5.2, smtplib,sendmail 8.15.2, 今天

python郵件至多人

多個 授權 fin mtp lur code 收件人 import 郵箱 本文轉載自 https://www.cnblogs.com/lurenjia1994/p/9485997.html python2發送郵件,demo腳本使用的是qq郵箱發送,需要申請授權碼(並非密

python郵件方法

lose 接收郵件 jpg Language .com msg 顯示 end multi python發送郵件方法 1、普通文本郵件 #!/usr/bin/env python# -*- coding:utf-8 -*-import smtplibfrom email.mi

聰哥哥教你學Python電子郵件

聰哥哥今天主要講的是如何用Python實現電子郵件傳送。這個電子郵件傳送的應用場景有很多,比如註冊使用者發郵件驗證,監控伺服器郵件告警等等。 引用廖雪峰先生的說法: Email的歷史比Web還要久遠,直到現在,Email也是網際網路上應用非常廣泛的服務。 幾乎所有的程式語言都支援傳送和接

使用Python傳送電子郵件

SMTP傳送郵件: 我這裡使用的qq郵箱伺服器,所以執行之前應先把qq郵箱中的相應服務開啟; 1.傳送文字郵件 #傳送文字郵件 import smtplib from email.mime.text import MIMEText from email.header