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.