In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to automatically send mail to multiple people, groups, and attachments by python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "python how to automatically send mail to send multiple people, groups, multiple attachments"!
The main problems that will be encountered are:
1 、 smtplib . Smtpauthenticationerror: (550) "user does not have permission")
2 、 smtplib . Smtpauthenticationerror: (535 million error: verification failed')
Both of these errors are caused by not using the authorization code in your password, which is generated by Baidu.
3 、 554 DT:SPM 163 smtp5
This is caused by a return detection mechanism of NetEase mailbox, which seems to be related to frequent sending. I met when I sent it again. I didn't send it successfully until I added a NetEase mailbox as my inbox. I don't know if this is the reason, but the specific reason is unknown. If anyone knows, please let me know under private messages or comments. Thank you!
Prepare to check the python version number on the system:
As shown in the picture, it has been installed.
two。 The implementation code is as follows (this is to edit the test code under IDEL on Windows and only change the attachment path on linux)
#-*-coding:utf-8-*-
Fromemailimportencoders
Importos
Importtraceback
Fromemail.headerimportHeader
From email. Mime. Textimport mime text
Fromemail.utilsimportparseaddr,formataddr
Importsmtplib
From email. Mime. MultipimportMimePart
From email. Mime. BaseIMPORTMimeBase
Fromemailimportencoders
# Chinese processing
Def _ format _ addr:
Name, addr=parseaddr (s)
Returnformataddr ((header (name, 'utf-8'). Encode (), addr))
Defsend_email (to_addr_in,filepath_in):
# email sender and recipient configuration
From_addr='*@163.com'
Smtp_server='smtp.163.com'
Password ='* this is the password of the third-party authorized client in your mailbox, not your login password
To_addr=to_addr_in
To_addrs=to_addr.split (',')
Msg=MIMEMultipart ()
The sender is sent by message ['from'] = _ format _ addr (' python enthusiast% s'%from_addr) #
# msg ['To'] = _ format_addr (' administrator% s'%to_addr) # single displayed recipient
Message ['to'] =','. Join (to_addrs) # more than displayed recipients
Message ['subject'] = title ('greetings from SMTP.', 'utf-8'). Encode () # display headers
# the path to be passed
# filepath=r'D:\ test'
Filepath=filepath_in
R=os.path.exists (filepath)
IfrisFalse:
Msg.attach (MIMEText ('nofile.), "ordinary", "utf-8"))
Else:
# message body is MIMEText:
Msg. Attach (MiMeText ('sendwithfile.), "ordinary", "utf-8"))
# iterate through the specified directory and display all file names in that directory
PathDir=os.listdir (filepath)
ForallDirinpathDir:
Child=os.path.join (filepath,allDir)
Printchild . Decode ('gbk') # Decode ('gbk') is to solve the problem of garbled code in Chinese display.
# to add an attachment is to add a MIMEBase and read a file locally
Withopen (child,' rb') asf:
# set the MIME and file name of the attachment. Here is the txt type:
Mime=MIMEBase ('file', 'xls', filename=allDir)
# add the necessary title information:
Mime. Add _ header ('Content-Disposition', 'attachment', filename=allDir)
Add a title ('content identification','0')
Mime. Add _ header ('X-attachment-Id','0')
# Reading: the contents of the attachment in
Mime.set_payload (f.read ())
# Encoding with Base64:
Encoders.encode_base64 (mime)
# add to MIMEMultipart:
Msg.attach (mime)
Try:
Server=smtplib . SMTP (smtp_server,25)
# server.starttls ()
Server. Set _ debugle level (1) # is used to display the execution steps for sending messages
Server.login (from_addr,password)
# printto_addrs
Server.sendmail (from_addr,to _ addrs,msg.as_string ())
Server.quit ()
Exception,e:
Print "error: unable to send email"
Printtraceback.format _ exc ()
If__name__=='__main__':
Send _ email (* @ qq.com,* * @ 163.com','d:\\ test') 3. The execution result in PyCharm is as follows
It shows that the message has been sent successfully and can be viewed in the corresponding mailbox. There is attached text under the corresponding folder.
If there is no attachment text (this is the screenshot of NetEase's mailbox, the above QQ Mail indicates that both mailboxes have received emails):
At this point, I believe you have a deeper understanding of "python how to automatically send mail to multiple people, send in groups, and multiple attachments". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.