SoFunction
Updated on 2024-10-30

A brief look at using the python mail module

We are in the development of the program, sometimes need to develop some automated tasks, after the implementation of the results will automatically send a copy of the mail, python send mail using the smtplib module, is a standard package, direct import import can be used, the code is as follows:

import smtplib    
from  import MIMEText
email_host = 'smtp.'   #E-mail address
email_user = 'xxxx@' # Sender account number
email_pwd = 'xxxx'    # Sender's password
maillist ='511402865@'
#Recipient's email address, if there are multiple accounts, separate them with a comma.
me = email_user
msg = MIMEText('Email test content')  # Contents of the e-mail
msg['Subject'] = 'Email Test Subject'  # Subject of the e-mail
msg['From'] = me  # Sender account number
msg['To'] = maillist  # Recipient account list
smtp = (email_host,port=25) # Connect to the mailbox, pass in the mailbox address, and port number, the port number for smtp is 25
(email_user, email_pwd)  # Sender's e-mail account, password
(me, maillist, msg.as_string())
# The parameters are the sender, the receiver, and the third one is to turn the content of the above sent email into a string.
() # Exit smtp after sending
print ('email send success.')

Here's how to send an email with an attachment

import smtplib
from  import MIMEText
from  import MIMEMultipart
username='xxx@'
email_host = 'smtp.'
passwd='123456'
recv=['511402865@',]
title='Email Title'
content='Send mail test'
msg = MIMEMultipart()
file=''
att = MIMEText(open(file,encoding='utf-8').read())
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="%s"'%file
(att)
(MIMEText(content))# The content of the body of the email
msg['Subject'] = title # Subject of the e-mail
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
(username,passwd)
(username,recv,msg.as_string())
()

Of course, we can encapsulate it into a function, and when you use it, you can directly call the function and pass in the email account password, recipient, sender, title and content.

  import smtplib      
  from  import MIMEText
  def send_mail(username,passwd,recv,title,content,mail_host='smtp.',port=25):
    '''
    Send mail function, default use 163smtp
    :param username: mailbox account xx@
    :param passwd: password of the mailbox
    :param recv: Recipient's address, multiple accounts separated by commas.
    :param title: title of the message
    :param content: content of the message
    :param mail_host: Mailbox server
    :param port: port number
    :return.
    '''
    msg = MIMEText(content)  # Contents of the e-mail
    msg['Subject'] = title  # Subject of the e-mail
    msg['From'] = username  # Sender account number
    msg['To'] = recv  # Recipient account list
    smtp = (mail_host,port=port) # Connect to the mailbox, pass in the mailbox address, and port number, the port number for smtp is 25
    (username, passwd)  # Sender's e-mail account, password
    (username, recv, msg.as_string())
    # The parameters are the sender, the receiver, and the third one is to turn the content of the above sent email into a string.
    () # Exit smtp after sending
    print ('email send success.')
     
email_user = 'xxxx@' # Sender account number
email_pwd = 'xxxxx'    # Sender's password
maillist ='511402865@'
title = 'Test Email Title'
content = 'Here are the contents of the email'
send_mail(email_user,email_pwd,maillist,title,content)

This is the whole content of this article.