In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Python email email how to send plain text, attachments, html format mail, in view of this question, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Learn to use smtplib to send email and record the learning process today. You are welcome to share with us.
Create a new python file named py3_email.py, and write the operation code in this file:
Import osimport smtplibfrom email.message import EmailMessageimport imghdr# here uses QQ Mail for testing # send plain text # get # email address and password from environment variables EMAIL_ADDRESS = os.environ.get ('EMAIL_ADDRESS') EMAIL_PASSWORD = os.environ.get (' EMAIL_PASSWORD') # get recipient's email address EMAIL_RECEVIER = os.environ.get ('EMAIL_RECEVIER')
With smtplib.SMTP ('smtp.qq.com',25) as smtp: # use ehlo to identify smtp server smtp.ehlo () # enable TLS smtp.starttls () # identify smtp smtp.ehlo again () # login smtp smtp.login (EMAIL_ADDRESS,EMAIL_PASSWORD) # smtp.set_debuglevel (1) debug using subject =' hello where are you?' Body ='Hi In China' Msg = f'{subject}\ n\ n {body} 'smtp.sendmail (EMAIL_ADDRESS,EMAIL_RECEVIER,msg) # # Note when testing here, the sender uses QQ Mail # # there are a lot of problems when using 163mailbox as the sender. # # the password here is the authorization code of the mailbox. You need to log in to the mailbox to set it.
# use EmailMessage object to set email subject content to send e_msg = EmailMessage () e_msg ['Subject'] =' Life is short! 'e_msg [' From'] = EMAIL_ADDRESSe_msg ['To'] = EMAIL_RECEVIERe_msg.set_content (' How about python?') with smtplib.SMTP ('smtp.qq.com',25) as smtp: smtp.login (EMAIL_ADDRESS,EMAIL_PASSWORD) smtp.send_message (e_msg)
# # sending an email with attachments e_msg = EmailMessage () e_msg ['Subject'] =' Image from my PCs with attachments to the email 'From'] = EMAIL_ADDRESSe_msg [' To'] = EMAIL_RECEVIERe_msg.set_content ('Image moto attached. # Open the picture and add the picture information to e_msg with open ('0.jpg) as f: file_data = f.read () file_type = imghdr.what (f.name) file_name = f.namee_msg.add_attachment (file_data,maintype='image',subtype=file_type,filename=file_name) # you can also add multiple pictures It can be implemented using for # if it is a pdf file, make the following adjustments # e_msg.add_attachment (file_data,maintype='application',subtype='octet-stream',filename=file_name) # other format files can refer to apiwith smtplib.SMTP ('smtp.qq.com',25) as smtp: smtp.login (EMAIL_ADDRESS,EMAIL_PASSWORD) smtp.send_message (e_msg)
# send an email in HTML format: e_msg = EmailMessage () e_msg ['Subject'] =' html email sendthrift email ('From'] = EMAIL_ADDRESSe_msg [' To'] = EMAIL_RECEVIERe_msg.set_content ('This is a plain text email') e_msg.add_alternative ("\ This is an HTML Email!", subtype='html') with smtplib.SMTP (' smtp.qq.com') 25) as smtp: smtp.login (EMAIL_ADDRESS,EMAIL_PASSWORD) smtp.send_message (e_msg) answers to questions about how python email emails are sent in plain text, attachments, and html format are shared here I hope the above content can help you to a certain extent, if you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.