python郵件群發
Ⅰ 為什麼用python.csv模塊群發郵件時給兩個人時,只有第二個人能收到
可能是郵件系統有問題或者網路原因造成郵件丟失。
能發郵件卻收不到郵件的情況(用的vip郵箱),直接找官方排查問題,自己沒辦法解決。
Ⅱ 如何用python發送email
python發送email還是比較簡單的,可以通過登錄郵件服務來發送,linux下也可以使用調用sendmail命令來發送,還可以使用本地或者是遠程的smtp服務來發送郵件,不管是單個,群發,還是抄送都比較容易實現。
先把幾個最簡單的發送郵件方式記錄下,像html郵件,附件等也是支持的,需要時查文檔即可
1 登錄郵件服務
[python]view plain
#!/usr/bin/envpython
#-*-coding:utf-8-*-
#python2.7x
#send_simple_email_by_account.py@2014-07-30
#author:orangleliu
'''''
使用python寫郵件simple
使用126的郵箱服務
'''
importsmtplib
fromemail.mime.textimportMIMEText
SMTPserver='smtp.126.com'
sender='[email protected]'
password="xxxx"
message='IsendamessagebyPython.你好'
msg=MIMEText(message)
msg['Subject']='TestEmailbyPython'
msg['From']=sender
msg['To']=destination
mailserver=smtplib.SMTP(SMTPserver,25)
mailserver.login(sender,password)
mailserver.sendmail(sender,[sender],msg.as_string())
mailserver.quit()
print'sendemailsuccess'
#-*-coding:utf-8-*-
#python2.7x
#send_email_by_.py
#author:orangleliu
#date:2014-08-15
'''''
用的是sendmail命令的方式
這個時候郵件還不定可以發出來,hostname配置可能需要更改
'''
fromemail.mime.textimportMIMEText
fromsubprocessimportPopen,PIPE
defget_sh_res():
p=Popen(['/Application/2.0/nirvana/logs/log.sh'],stdout=PIPE)
returnstr(p.communicate()[0])
defmail_send(sender,recevier):
print"getemailinfo..."
msg=MIMEText(get_sh_res())
msg["From"]=sender
msg["To"]=recevier
msg["Subject"]="Yestodayinterfacelogresults"
p=Popen(["/usr/sbin/sendmail","-t"],stdin=PIPE)
res=p.communicate(msg.as_string())
print'mailsended...'
if__name__=="__main__":
mail_send(s,r)
#!/usr/bin/envpython
#-*-coding:utf-8-*-
#python2.7x
#send_email_by_smtp.py
#author:orangleliu
#date:2014-08-15
'''''
linux下使用本地的smtp服務來發送郵件
前提要開啟smtp服務,檢查的方法
#ps-ef|grepsendmail
#telnetlocalhost25
這個時候郵件還不定可以發出來,hostname配置可能需要更改
'''
importsmtplib
fromemail.mime.textimportMIMEText
fromsubprocessimportPopen,PIPE
defget_sh_res():
p=Popen(['/Application/2.0/nirvana/logs/log.sh'],stdout=PIPE)
returnstr(p.communicate()[0])
defmail_send(sender,recevier):
msg=MIMEText(get_sh_res())
msg["From"]=sender
msg["To"]=recevier
msg["Subject"]="Yestodayinterfacelogresults"
s=smtplib.SMTP('localhost')
s.sendmail(sender,[recevier],msg.as_string())
s.quit()
print'sendmailfinished...'
if__name__=="__main__":
r=s
mail_send(s,r)
2調用sendmail命令 (linux)
[python]view plain
3 使用smtp服務來發送(本地或者是遠程伺服器)
[python]view plain
Ⅲ 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')
支持的列表:
Ⅳ Python怎麼群發郵件
要群發郵件直接找u-mail郵件營銷平台啊,操作方便簡單,成功率高,效果有保障,還有詳細的報表統計發送效果,免費試用
Ⅳ 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自動發送郵件多個人收件人代碼更改
msg['To'] = "[email protected];[email protected]" # 多個郵件接收者,中間用;隔開
msg['Cc'] = "[email protected];[email protected]" # 多個郵件抄送者,中間用;隔開
Ⅷ 用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()