Python sample code for Batch Sending of mail, python sample code

Source: Internet
Author: User
Tags starttls

Python sample code for Batch Sending of mail, python sample code

1. Send text information

'''Encrypted text mail''' def sendEmail (from_addr, password, to_addr, smtp_server): try: msg = MIMEText ('hello, greetings from the Information Engineering Institute... ', 'plain', 'utf-8 ') # text mail # msg = MIMEText ('

2. send an email with an Image Attachment

'''Send email with image attachments ''' def sendFileEmail (from_addr, password, to_addr, smtp_server): try: msg = MIMEMultipart () msg ['from'] = _ format_addr ('information Engineering Institute <% s> '% from_addr) msg ['to'] = _ format_addr ('recipient <% s> '% to_addr) msg ['subobject'] = Header ('mail Subject: greeting ', 'utf-8 '). encode () # The Mail body is MIMEText: msg. attach (MIMEText ('send with file... ', 'plain', 'utf-8') # msg. attach (MIMEText ('

3. Send an email with an Image Attachment

'''Send an email with an Image Attachment ''' def sendFilesEmail (from_addr, password, to_addr, smtp_server): try: msg = MIMEMultipart () msg ['from'] = _ format_addr ('information Engineering Institute <% s> '% from_addr) msg ['to'] = _ format_addr ('recipient <% s> '% to_addr) msg ['subobject'] = Header ('mail Subject: greeting ', 'utf-8 '). encode () # The Mail body is MIMEText: msg. attach (MIMEText ('send multi-attachment mail... ', 'plain', 'utf-8') # --- this is the attachment part --- # xlsx type attachment part = MIMEApplication (open (R '. /file/foo.xlsx ', 'r B '). read () part. add_header ('content-disposition', 'attachment', filename = "foo.xlsx") msg. attach (part) # jpg type attachment part = MIMEApplication (open (R '. /file/image .png ', 'rb '). read () part. add_header ('content-disposition', 'attachment ', filename = ('gbk', '', 'img .png') msg. attach (part) # pdf attachment part = MIMEApplication (open (R '. /file/foodies ', 'rb '). read () part. add_header ('content-disposition', 'attachment', filen Ame = "foodies") msg. attach (part) # mp3 type attachment # part = mimeapplication(open('fooket', 'rb '). read () # part. add_header ('content-disposition', 'attachment', filename = "footings") # msg. attach (part) server = smtplib. SMTP (smtp_server, 25, timeout = 30) # server. set_debuglevel (1) # record detailed information server. login (from_addr, password) # log on to the email server. sendmail (from_addr, to_addr, msg. as_string () # Send Message server. quit () print ("The email with image is sent successfully! ") Failed t Exception as e: print (" failed to send: "+ e)

4 complete code

From email import encodersfrom email. header import Headerfrom email. mime. text import MIMETextfrom email. mime. multipart import MIMEMultipartfrom email. mime. base import MIMEBasefrom email. mime. application import MIMEApplicationfrom email. utils import parseaddr, formataddrimport smtplibdef _ format_addr (s): name, addr = parseaddr (s) return formataddr (Header (name, 'utf-8 '). encode (), addr) '''encrypt sent text mail'' 'Def sendEmail (from_addr, password, to_addr, smtp_server): try: msg = MIMEText ('Hi, greetings from the Information Engineering Institute... ', 'plain', 'utf-8 ') # text mail # msg = MIMEText ('

The sample code for Batch Sending of emails in the above Python is all the content shared by the editor. I hope you can give us a reference and support the help house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.