Brief understanding of how to use python mail module

  • 2021-07-26 08:05:15
  • OfStack

When we are developing programs, sometimes we need to develop some automated tasks. After execution, we will automatically send a mail. python sends mail using smtplib module, which is a standard package. import can be imported and used directly. The code is as follows:


import smtplib    
from email.mime.text import MIMEText
email_host = 'smtp.163.com'   # Email address 
email_user = 'xxxx@163.com' #  Sender account number 
email_pwd = 'xxxx'    #  Sender password 
maillist ='511402865@qq.com'
# Recipient mailbox, if there are multiple accounts, separate them with commas 
me = email_user
msg = MIMEText(' Mail sending test content ')  #  Mail content 
msg['Subject'] = ' Mail Test Subject '  #  Mail Subject 
msg['From'] = me  #  Sender account number 
msg['To'] = maillist  #  Recipient account list 
smtp = smtplib.SMTP(email_host,port=25) #  Connection mailbox, incoming mailbox address, and port number, smtp The port number of is 25
smtp.login(email_user, email_pwd)  #  Sender's email account number and password 
smtp.sendmail(me, maillist, msg.as_string())
#  The parameters are sender, receiver, and number 3 One is to change the content of the above sent email into a string 
smtp.quit() #  Exit after sending smtp
print ('email send success.')

The following is a message with attachments


import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
username='xxx@xx.com'
email_host = 'smtp.163.com'
passwd='123456'
recv=['511402865@qq.com',]
title=' Message header '
content=' Send mail test '
msg = MIMEMultipart()
file='a.txt'
att = MIMEText(open(file,encoding='utf-8').read())
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="%s"'%file
msg.attach(att)
msg.attach(MIMEText(content))# Content of the message body 
msg['Subject'] = title #  Mail Subject 
msg['From'] = username #  Sender account number 
msg['To'] = recv #  Recipient account list 
#smtp = smtplib.SMTP_SSL(eail_host,port=456)#qq Mailbox 
smtp = smtplib.SMTP_SSL(eail_host,port=25)# Other mailboxes 
smtp.login(username,passwd)
smtp.sendmail(username,recv,msg.as_string())
smtp.quit()

Of course, we can package it into a function. When using it, we can call the function directly and pass in the email account password, recipient, sender, title and content.


  import smtplib      
  from email.mime.text import MIMEText
  def send_mail(username,passwd,recv,title,content,mail_host='smtp.163.com',port=25):
    '''
     Send mail function, which is used by default 163smtp
    :param username:  Email account number  xx@163.com
    :param passwd:  Mailbox password 
    :param recv:  Mailbox recipient address, multiple accounts separated by commas 
    :param title:  Message header 
    :param content:  Mail content 
    :param mail_host:  Mailbox server 
    :param port:  Port number 
    :return:
    '''
    msg = MIMEText(content)  #  Mail content 
    msg['Subject'] = title  #  Mail Subject 
    msg['From'] = username  #  Sender account number 
    msg['To'] = recv  #  Recipient account list 
    smtp = smtplib.SMTP(mail_host,port=port) #  Connection mailbox, incoming mailbox address, and port number, smtp The port number of is 25
    smtp.login(username, passwd)  #  Sender's email account number and password 
    smtp.sendmail(username, recv, msg.as_string())
    #  The parameters are sender, receiver, and number 3 One is to change the content of the above sent email into a string 
    smtp.quit() #  Exit after sending smtp
    print ('email send success.')
     
email_user = 'xxxx@163.com' #  Sender account number 
email_pwd = 'xxxxx'    #  Sender password 
maillist ='511402865@qq.com'
title = ' Test message headers '
content = ' Here is the content of the email '
send_mail(email_user,email_pwd,maillist,title,content)

Related articles: