Python uses the email module to send mail

Introduce sending mail

import smtplib
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
# Common sense of username and password sent by email: third-party authorization
_ user='your [email protected]'
_ pwd='enter password'

now=time.strftime('%Y-%m-%d-%H-%M-%S')#Get timestamp

classsendEmail:
 def send_email(self,email_to,filepath):
  # email_to recipient
  # filepath the email address you want to send
  # As the name suggests, Multipart is divided into multiple parts
  msg=MIMEMultipart()
  msg['Subject']=now +'Qinghan test report'
  msg['From']=_user
  msg['To']=email_to

  #- - - This is the text part---
  part=MIMEText('This is an automated test result, please check!')
  msg.attach(part)

  #- - - - This is the attachment part-----
  # Send multiple attachments
  # path=['1','2','3']#Many paths
  # for item in path:
  #  part=MIMEApplication(open(item,'rb').read())
  #  part.add_header('Content-Disposition','attachment',filename=filepath)
  #  msg.attach(part)

# It can only read files but not folders, so you can put the files in the list and make a for loop below.

  # Send an attachment
  part=MIMEApplication(open(filepath,'rb').read())
  part.add_header('Content-Disposition','attachment',filename=filepath)
  msg.attach(part)
  s=smtplib.SMTP_SSL('smtp.163.com',timeout=30)#Connect to smtp mail server, the default port is 25
  s.login(_user,_pwd)#Login server
  s.sendmail(_user,email_to,msg.as_string())#send email

if __name__ =='__main__':sendEmail().send_email('your mailbox/Someone else's [email protected]',r'C:\Users\18210\Desktop\python\jiaoben\ningmenban\API_AUTO _1\test_result\html_report\test_api.html')

received e-mail


Welcome to scan the QR code!

Recommended Posts

Python uses the email module to send mail
Python uses the re module to verify dangerous characters
Python implements 126 mailboxes to send mail
How to view the python module
Python implements QQ mailbox to send mail
How to learn the Python time module
How to install the downloaded module in python
CentOS 6/7 configure sendEmail to send mail
How to save the python program
Python uses pdfkit to generate pdf [python]
Python3 module
python Douban replaces pip to install python module
Python string to judge the password strength
How does Python handle the json module
Python novice learns to use the library
Python uses pandas to process Excel data
How to use the round function in python
How to use the zip function in Python
An article to understand the yield in Python
How to use the format function in python
Detailed explanation of the usage of Python decimal module
How to enter python through the command line
Python uses PIL to cut and stitch pictures
How to switch the hosts file using python
Python how to move the code collectively right
The best way to judge the type in Python
How to send mail using sendemail on ubuntu
Use python to realize the aircraft war game
01. Introduction to Python
2.1 The Python Interpreter (python interpreter)
Introduction to Python
Python Lesson 37-Module
How to add the gzip module to Nginx on Ubuntu 14.04
How to practice after the python file is written
What are the methods for python to connect to mysql
How to understand the introduction of packages in Python
Using Python to analyze the Guangzhou real estate market
How does python judge the module installation is complete
The specific steps to upgrade python on Raspberry Pi
Python simulation to realize the distribution of playing cards
What are the ways to open files in python