Python3 enables timed email sending with attachments and python3 sends emails

Source: Internet
Author: User

Python3 enables timed email sending with attachments and python3 sends emails

This example shares the code of the Python3 timed email sending function for your reference. The details are as follows:

1. Import Module

Import osimport datetime # timed sending, and date import shutil # file operation import smtplib # mail module from email. mime. text import MIMETextfrom email. mime. multipart import MIMEMultipartfrom email. header import Headerimport timeimport xlwt # Write Data in excel

2. Write Data to EXCEL

Def eWrite (fLocate, file_sheet, file_subject, style0): try: if OS. path. exists (fLocate): OS. remove (fLocate) # If the file exists, delete f = xlwt. workbook (encoding = 'utf-8') # Open the excel file fs = f. add_sheet (file_sheet) # sheet name subject = list (file_subject) # list-based for I in range (len (subject )): # Find the date column if 'date' in subject [I]: col_num = I for I in range (len (subject): # sheet title fs. write (0, I, subject [I]) for I in range (10): # The cell width is fs. col (I ). width = 3333 print ("write finished") f. save (fLocate) failed t: print ("write failed ")

3. Send an email

Def eSend (sender, explorer, username, password, smtpserver, subject, e_content, file_path, file_name): try: # mail header message = MIMEMultipart () message ['from'] = sender # Send message ['to'] = ",". join (receiver) # receiver message ['subobject'] = Header (Subject, 'utf-8') message. attach (MIMEText (e_content, 'plain ', 'utf-8') # Body of the email # construct the attachment att1 = MIMEText (open (file_path + file_name, 'rb '). read (), 'base64', 'utf-8 ') att1 ["Content-Type"] = 'application/octet-stream' att1 ["Content-Disposition"] = "attachment; filename =" + file_name message. attach (att1) # execute smtp = smtplib. SMTP () smtp. connect (smtpserver) # connect to the smtp server. login (username, password) # log on to smtp. sendmail (sender, receiver, message. as_string () # send smtp. quit () print ("SEND") failed t: print ("send failed ")

4. Configuration and execution

While True: # configuration #__ time _____ ehour = 11 # scheduled hour emin = 49 # scheduled minute esec = 50 # scheduled second current_time = time. localtime (time. time () # current time date cur_time = time. strftime ('% H % m', time. localtime (time. time () # current time str #__ mysql _____ #__ email _____ sender = ''# sender email receiver = ['2017 @ qq.com '] # recipient email, username = ''# sender name password ='' # smtp password. qq assigns you a string, 163 set smtpserver = ''# mailbox server subject =" Hey, here's something interesting "# mail title e_content = '{0: ^ 27} \ n {1: ^ 27} \ n {2: ^ 25} \ n {3: ^ 25 }'. format ('I', '/\', '(-----)', '(--------)') # mail body #__ file _____ file_path = "D: /"# File Location file_name =" shit.xls "# file name fLocate = file_path + file_name # file path file_subject = 'I', 'Miss ', 'U' # sheet title file_sheet = 'OK' # sheet Name style0 = xlwt. XFStyle () style0.num _ format_str = 'yyyy-MM-DD '# operation if (current_time.tm_hour = ehour) and (current_time.tm_min = emin) and (current_time.tm_sec = esec )): print ("START") eWrite (fLocate, file_sheet, file_subject, style0) eSend (sender, Cycler, username, password, smtpserver, subject, e_content, file_path, file_name) print (cur_time) time. sleep (1)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.