Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic email sending by Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how Python can automate email sending. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Using Python to automate email delivery allows you to get rid of tedious and repetitive business and save a lot of time.

Configuration before operation (take the more complex QQ Mail as an example, other mailbox operations are similar)

Click Settings-account, slide down the agreement below, and open the IMAP/SMTP protocol (IMAP, that is, Internet Message Access Protocol), through which you can obtain email information and download email from the mail server. IMAP is similar to POP in that it is a mail acquisition protocol. )

(ps. Verification is required to enable)

Remember the port number. You need to write code to send email later.

The authorization code is generated, and the previous configuration is completed.

Account number: 3203068752@qq.com

Authorization code: iwmha*ndcei (change it to your own, I'm afraid you'll blow up my mailbox)

Port number: 465

one hundred and twenty three

Let's start by sending a simple email.

SMTP.sendmail (from_addr, to_addrs, msg [, mail_options, rcpt_options]

Parameter description:

From_addr: address of the sender of the mail.

To_addrs: list of strings, email address.

Msg: sending messa

Note here that the third parameter, msg, is a string that represents the message. We know that an email is generally composed of title, sender, recipient, email content, attachment, etc. When sending an email, we should pay attention to the format of msg. This format is defined in the smtp protocol.

Import smtplibfrom email.mime.text import MIMEText## enter sender mailbox name email_name = 'your mailbox' # enter user authorization code passwd = 'your authorization code' # recipient mailbox msg_to = 'who do you want to send' # body content = "I want to eat steamed bread" # set email msg = MIMEText (content) msg ['subject'] =' what do you want to eat'# set sender msg ['From'] = 'Xiao Wang next door' # this parameter sets who to send to msg ['To'] = msg_to# to connect to the server s = smtplib.SMTP_SSL (' smtp.qq.com' 465) # Log in to my mailbox s.login (email_name,passwd) # send email s.sendmail (email_name,msg_to,msg.as_string ()) print ("sent successfully")

It was sent successfully and we received this email in the mailbox we received.

How to send an email with attachments

To send mail with attachments, first create an instance of MIMEMultipart (), then construct attachments, if there are multiple attachments, you can construct them in turn, and finally send them using smtplib.smtp.

Import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipart## enter sender mailbox name email_name ='*'# enter user authorization code passwd ='* # recipient mailbox msg_to ='* # body content = "I want to eat steamed bread" # set email content_part = MIMEText (content) # set attachment content object msg = MIMEMultipart () msg ['subject'] =' What do you want to eat?'# set the sender msg ['From'] =' Xiao Wang 'next door' # this parameter sets who to send to msg ['To'] = msg_to# add attachment content msg.attach (content_part) # * construct attachment * # attachment att1 = MIMEText (". / data/ Learning goal .txt" 'rb') .read (),' plain','utf-8') # add header information I told the server that I am now an attachment att1 ['Content-Type'] =' application/octet-stream'att1.add_header ("Content-Disposition", 'attachment',filename= (' gbk', ", 'learning goal .txt')) # add content to the email msg.attach (att1) # Image attachment att2 = MIMEText (" Piggy .gif ", 'rb'). Read (),' plain','utf-8') # add header message I told the server I am now an attachment att2 ['Content-Type'] =' application/octet-stream'att2.add_header ("Content-Disposition", 'attachment',filename= (' gbk', ", 'pig.gif')) # add content to email msg.attach (att2) # connect server s = smtplib.SMTP_SSL ('smtp.qq.com',465) # log in to my mailbox s.login (email_name,passwd) # send mailbox s.sendmail (email_name) Msg_to,msg.as_string () print ("sent successfully")

We can see that attachments are sent successfully, but we send emails in bulk in our work, and it also involves CC, so what should we do?

# set acc_to = ['* *] # this parameter sets who to send msg ['To'] ='; '.join (msg_to) # sets the caster msg [' Cc'] ='; '.join (acc_to)

All we need is to set up the CC.

Is it realized in batch at once?

Sending mail with pictures

# email body content = "" Today's report

imageid

Ha ha ha

imageid2

This is the end "image_path = {" imageid ":" pig.png "," imageid2 ":" * *. Png "} # this is the sender's mailbox name email_name ='* *'# this is the user authorization code, QQ Mail has. Other mailboxes usually write your password directly passwd ='*'# recipient mailbox multiple people send msg_to = [*] # CC _ to = [* *] from_name ='* 'subject =' automation office begins' att_file= [. / data/ learning goal .txt', 'comment word cloud picture .png', 'automation office .ipynb', 'pig.gif' '* * .jpg'] send_email (email_name = email_name, passwd=passwd, msg_to=msg_to, acc_to=acc_to, content=content, image_html=True, image_path = image_path, subject=subject, from_name = from_name, att_file=att_file)

After reading the above, do you have any further understanding of how Python automates email delivery? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report