❶ 用python發送郵件,可以群發,帶有多個附件
'''''
函數說明:Send_email_text()函數實現發送帶有附件的郵件,可以群發,附件格式包括:xlsx,pdf,txt,jpg,mp3等
參數說明:
1.subject:郵件主題
2.content:郵件正文
3.filepath:附件的地址,輸入格式為["","",...]
4.receive_email:收件人地址,輸入格式為["","",...]
'''
defSend_email_text(subject,content,filepath,receive_email):
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.
sender="發送方郵箱"
passwd="填入發送方密碼"
receivers=receive_email#收件人郵箱
msgRoot=MIMEMultipart()
msgRoot['Subject']=subject
msgRoot['From']=sender
iflen(receivers)>1:
msgRoot['To']=','.join(receivers)#群發郵件
else:
msgRoot['To']=receivers[0]
part=MIMEText(content)
msgRoot.attach(part)
##添加附件部分
forpathinfilepath:
if".jpg"inpath:
#jpg類型附件
jpg_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=jpg_name)
msgRoot.attach(part)
if".pdf"inpath:
#pdf類型附件
pdf_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=pdf_name)
msgRoot.attach(part)
if".xlsx"inpath:
#xlsx類型附件
xlsx_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=xlsx_name)
msgRoot.attach(part)
if".txt"inpath:
#txt類型附件
txt_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=txt_name)
msgRoot.attach(part)
if".mp3"inpath:
#mp3類型附件
mp3_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=mp3_name)
msgRoot.attach(part)
try:
s=smtplib.SMTP()
s.connect("smtp.mail.aliyun.com")#這里我使用的是阿里雲郵箱,也可以使用163郵箱:smtp.163.com
s.login(sender,passwd)
s.sendmail(sender,receivers,msgRoot.as_string())
print("郵件發送成功")
exceptsmtplib.SMTPExceptionase:
print("Error,發送失敗")
finally:
s.quit()
❷ 如何用python自動發送郵件
注意如果使用qq的smtp轉發功能的話,需要在設置頁面中將該功能打開,然後設定密碼,改密碼就是賦值給下文password中的。
開啟smtp轉發功能文檔
[python]view plain
#fromemail.MIMETextimportMIMEText#
fromemail.MIMETextimportMIMEText
fromemail.HeaderimportHeader
msg=MIMEText('hello,sendbypython','plain','utf-8');
from_addr="*****@qq.com"#
password="******"#password
to_addr="****@qq.com"#targetemailaddress
smtp_server="smtp.qq.com"#smtpseverdomainforqqissmtp.qq.com
importsmtplib
server=smtplib.SMTP(smtp_server,25);
server.set_debuglevel(1)
server.login(from_addr,password);
server.sendmail(from_addr,[to_addr],msg.as_string())
server.quit()
上面這個是沒有主題的,有主題的話建議採用下面這個代碼
下面是連續發送有主題的文件10封
[python]view plain
fromemail.HeaderimportHeader
fromemail.MIMETextimportMIMEText
fromemailimportencoders
fromemail.utilsimportparseaddr,formataddr
importsmtplib
def_format_addr(s):
name,addr=parseaddr(s)
returnformataddr((Header(name,'utf-8').encode(),addr))
from_addr="*****@qq.com"#
password="******"#password
to_addr="****@qq.com"#targetemailaddress
smtp_server="smtp.qq.com"#smtpseverdomainforqqissmtp.qq.com
fornuminrange(1,11):
msg=MIMEText('hello,sendbyTom','plain','utf-8');
msg['From']=_format_addr('Tom<%s>'%from_addr)
msg['To']=_format_addr('addministrator<%s>'%to_addr)
msg['Subject']=Header('ThegreetingfromTom','utf-8').encode()
server=smtplib.SMTP(smtp_server,25)
server.set_debuglevel(1)
server.login(from_addr,password)
#server.sendmail(from_addr,[to_addr],msg.as_string())
server.sendmail(from_addr,[to_addr],msg.as_string())
server.quit()
❸ Python腳本也可以用來發送電子郵件
准備工作:安裝第三方包,yagmail和keyring
安裝完成後,打開命令提示符窗口(我用的是win)。輸入python回車,輸入import yagmail回車,輸入yagmail.register('你的郵箱地址','郵箱密碼或郵箱安全碼')回車。沒有報錯後,就可以開始編寫python腳本了。
直接上腳本:
import yagmail
smtp_server=yagmail.SMTP(user='[email protected]',host='smtp.qq.com')
#host 需要你到郵箱首頁幫助中心找一下,一般都是smtp.xxxx.com
contents=['郵件正文內容,可以逗號分開多寫幾行,也可以用轉行符號!']
smtp_server.send('[email protected]','郵件主題描述',contents)
# [email protected]收件郵箱地址,可以設置成一樣的,測試一下自己能收到不。
執行上面的腳本,基本上就可以收到郵件了。不要執行多次,太頻繁,容易被伺服器攔截,最後可能會被封號。
另外 contents 里可以寫html,也可以放附件,附件的話直接寫個本機文件路徑即可。
❹ Python向多人發送、抄送帶附件的郵件(含詳細代碼)
python要發送帶附件的郵件,首先要創建MIMEMultipart()實例,然後構造附件,如果有多個附件,可依次構造,最後使用smtplib.smtp發送。
步驟:
(1)設置伺服器所需信息(ps:部門郵箱密碼為授權碼,需自行登錄相應郵箱設置授權碼)
(2)設置email信息
(3)附件部分
(4)登錄郵箱並發送郵件
附上源碼:
❺ python批量發送郵件--包括批量不同附件
小豬在公司做出納,乾的活卻包括了出納、會計、結算專員等工作,周末都要被無奈在家加班,主要還沒有加班費,簡直是被公司嚴重壓榨。每個月初都要給每個工長發預付款賬單郵件,月中發結算款賬單。重復性機械工作。
一個及格線上的程序員,最起碼的覺悟就是將重復性的機械工作自動化,於是,在我花了一個多小時,幫她給一部分工長發了一次郵箱後,默默的回來寫了這個腳本。
所以,設計要點就是一個字—— 懶 。
恩,就醬。
經過我觀察,郵件內容分為兩種,這里先說第一種,「結算款」:
(1) 郵件內容(content)不變,為固定的txt文本
(2) 附件(attch)為每個工長的結算賬單(excel文件.xlsx),此文件命名為總賬單中自動分割出來的名字(暫時不懂怎麼分割出來的=.=),格式為:
(3) 郵件主題(Subject)為附件名(不帶後綴名)
(4) 郵件接收對象(工長)的名單及其郵箱地址基本不變,偶爾變動
(5)
(1) 將工長及其郵箱地址存為CSV文件的兩列,python中將其讀取為字典形式,存儲以供後續查詢郵箱地址。
(2) 遍歷文件夾中的附件(.xlsx類型文件),對其進行兩種操作,一方面將其名字(不帶路徑和後綴)提取出來,作為郵件主題(Subject),並對Subject進一步劃分,得到其中的人名(工長);另一方面,將其傳入MIMEbase模塊中轉為郵件附件對象。
(3) 由上述得到的人名(name),在字典形式的通訊錄中,查找相應的地址(value),即為收件人名稱和地址
(4) 利用python中的email模塊和smtp模塊,登錄自己的郵箱賬號,再對每個附件,得到的收件人名和地址,添加附件,發送郵件。done
在設計過程中有幾點需要注意
(1) 有時一個郵件地址對應兩個人名,此時應該在CSV文件中分為兩行存儲,而不是將兩個人名存為同一個鍵;
(2)有賬單.xlsx文件,通訊錄里卻沒存儲此人記錄,程序應該列印提示沒有通訊記錄的人名,且不能直接退出,要保證員工看到此提示,此第一版程序還有解決此問題;
(3)此程序發送的郵件內容為純文本,若要求郵件內容有不同格式(如部分加粗,部分紅色),還有小部分需要每次更改的地方(如郵件內容包含當前月份),如何解決?(這就是第二種郵件內容,「預算款」);
(4)重名的,暫時還沒碰到,程序中也沒給出解決方案。
第一版到此,20180830,待更新
第二版更新,20180904
第三版更新,20180909
轉戰CSDN博客,更多博客見傳送門《 xiaozhou的博客主頁 》
❻ python 使用zmail收發電子郵件
1、發送郵件:
import zmail
server = zmail.server(' [email protected] 』, 'yourpassword')
server.send_mail(' [email protected] ',{'subject':'Hello!','content_text':'By zmail.'})
server.send_mail([' [email protected] ',' [email protected] '],{'subject':'Hello!','content_text':'By zmail.'})
2、接收最後一封郵件:
import zmail
server = zmail.server(' [email protected] 』, 'yourpassword')
latest_mail = server.get_latest()
zmail.show(latest_mail)
3、發送帶附件的郵件:
import zmail
mail = {
'subject': 'Success!', # Anything you want.
'content_text': 'This message from zmail!', # Anything you want.
'attachments': ['/Users/zyh/Documents/example.zip','/root/1.jpg'], # Absolute path will be better.
}
server = zmail.server(' [email protected] 』, 'yourpassword')
server.send_mail(' [email protected] ', mail)
server.send_mail([' [email protected] ',' [email protected] '], mail)
4、發送html格式郵件:
with open('/Users/example.html','r') as f:
content_html = f.read()
mail = {
'subject': 'Success!', # Anything you want.
'content_html': content_html,
'attachments': '/Users/zyh/Documents/example.zip', # Absolute path will be better.
}
server.send_mail(' [email protected] ',mail)
5、使用抄送:
server.send_mail([' [email protected] ',' [email protected] '],mail,cc=[' [email protected] '])
6、自定義server
server = zmail.server('username','password',smtp_host='smtp.163.com',smtp_port=994,smtp_ssl=True,pop_host='pop.163.com',pop_port=995,pop_tls=True)
7、根據ID取回郵件:mail = server.get_mail(2)
根據日期、主題、發送人取回郵件:
mail = server.get_mails(subject='GitHub',after='2018-1-1',sender='github')
mail = server.get_mails(subject='GitHub',start_time='2018-1-1',sender='github',start_index=1,end_index=10)
8、查看郵箱統計
mailbox_info = server.stat() #結果為包含兩個整型的元組: (郵件的數量, 郵箱的大小).
9、刪除郵件:MailServer.delete(which)
10、保存附件:zmail.save_attachment(mail,target_path=None,overwrite=False)
11、保存郵件:zmail.save(mail,name=None,target_path=None,overwrite=False)
12、讀取郵件:zmail.read(file_path,SEP=b'\r\n')
支持的列表: