python Realizes Timing Mail Sending

  • 2021-08-28 20:47:56
  • OfStack

This article example for everyone to share python to achieve the specific code of sending mail regularly, for your reference, the specific content is as follows

STEP 1 Send mail


import smtplib 
from email.mime.text import MIMEText
from email.header import Header
# Introduce smtplib , MIMETex And Header

mailhost='smtp.qq.com'
# Put qq The server address of the mailbox is assigned to a variable mailhost The address should be in string format 
qqmail = smtplib.SMTP()
# Instantiation 1 A smtplib In the module SMTP Class, so that you can call the SMTP Object's methods and properties 
qqmail.connect(mailhost,25)
# Connect to the server, the 1 The parameter is the server address, the 2 The parameters are SMTP Port number. 
# All of the above are connected servers. 

account = input(' Please enter your email address: ')
# Get the mailbox account in string format 
password = input(' Please enter your password: ')
# Gets the mailbox password in string format 
qqmail.login(account,password)
# Login mailbox, number 1 The parameters are email account number, and the 2 Parameters are mailbox passwords 
# All of the above are login mailboxes. 

receiver=input(' Please enter the recipient's mailbox: ')
# Gets the recipient's mailbox. 

content=input(' Please enter the message body: ')
# Enter the body of your message in string format 
message = MIMEText(content, 'plain', 'utf-8')
# Instantiation 1 A MIMEText Mail object, which needs to be written into 3 Parameters, which are message body, text format and encoding 
subject = input(' Please enter your email subject: ')
# Enter the subject of your email in string format 
message['Subject'] = Header(subject, 'utf-8')
# On the right side of the equal sign is instantiated 1 A Header Message header object, which needs to write two parameters, namely message subject and code, and then assign them to the variable to the left of the equal sign message['Subject'] . 
# Above, fill in the subject and text. 

try:
 qqmail.sendmail(account, receiver, message.as_string())
 print (' Mail sent successfully ')
except:
 print (' Message delivery failed ')
qqmail.quit()
# The above are sending mail and exiting mailbox. 

Step 2 Timing


import schedule
import time
# Introduce schedule And time

def job():
 print("I'm working...")
# Definition 1 A man named job The function of the function is to print 'I'm working...'

schedule.every(10).minutes.do(job)  # Deploy every 10 Execute in minutes 1 Times job() The task of the function 
schedule.every().hour.do(job)   # Deployment is performed every × hour 1 Times job() The task of the function 
schedule.every().day.at("10:30").do(job) # Deployed on a daily basis 10:30 Execute job() The task of the function 
schedule.every().monday.do(job)   # Deploy every week 1 Execute job() The task of the function 
schedule.every().wednesday.at("13:15").do(job)# Deployment weekly 3 Adj. 13 : 15 Execute the task of the function 

while True:
 schedule.run_pending()
 time.sleep(1) 

I saw a section of python to send mail regularly, and share it with you:


import schedule
import time
import smtplib
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header

host_server = 'smtp.qq.com' # smtp The address of the server 
sender_sina = 'xxxx@qq.com' #  Sender's mailbox 
pwd = 'xxxx' #  Authorization password 

sender_sina_mail = 'xxxx@qq.com' #  Sender's mailbox 
receiver = 'xxxx@qq.com' #  Recipient mailbox 

mail_title = ' This is the title '
mail_content = ' This is the text '
msg = MIMEMultipart()
msg['Subject'] = Header(mail_title, 'utf-8')
msg['From'] = sender_sina_mail
msg['To'] = Header(receiver, 'utf-8')
msg.attach(MIMEText(mail_content, 'html', 'utf-8'))

#  Add attachments 
attachment = MIMEApplication(open('xxxx.pptx', 'rb').read())
attachment.add_header('Content-Disposition', 'attachment', filename='xxxx.pptx')
msg.attach(attachment)


#  Introduce schedule And time Module 
def job():
  try:
    smtp = SMTP_SSL(host_server)
    smtp.set_debuglevel(0)
    smtp.ehlo(host_server)
    smtp.login(sender_sina, pwd)
    smtp.sendmail(sender_sina_mail, receiver, msg.as_string())
    smtp.quit()
    print('email send success')
  except smtplib.SMTPException:
    print('email send error')


#  Definition 1 A man named job Function of 
# schedule.every(2).seconds.do(job) #  Every 2s Execute 1 Times job() Function 
schedule.every().day.at("14:30").do(job)

while True:
  schedule.run_pending()
  time.sleep(1)

Related articles: