SoFunction
Updated on 2024-11-12

python sends mail using the smtplib module

Use the smtplib module to send emails, for your reference, as follows

1) Use smtplib module to send simple emails

Steps:

1. Connect to the SMTP server and log in to the server with your username and password
2. Create an EmailMessage object, which represents the message itself.
3. Call sendmail() method to send mail

Example:

  • I use my own QQ email (English address) to send an email to myself (original address) (QQ email requires an authorization code (see details))
  • () Normal SMTP connection for delegates (default port 21)
  • smtplib.SMTP_SSL() represents SSL-based SMTP connections (default port 456, secure)
import smtplib
import 

fromaddr = 'wk_helloworld@' # Account number
password = '****************'  # QQ Authorization Code

conn = smtplib.SMTP_SSL('', 465) # Create SMTP connections
(fromaddr, password)    # Log in to the mail server
msg = ()   # Create mail objects
msg.set_content('Hello, Python Mail')   # Setting up mail content (regular mail)
(fromaddr, ['929667257@'], msg.as_string())  # Send mail

() # Disconnect

2) Send a full content email

  • Setting the subject, sender name, and recipient name for the email (setting the corresponding properties of the EmailMessage object)
  • Setting the second parameter of the set_content() method of EmailMessage to html changes the content of the message to HTML format.
import smtplib
import 

fromaddr = 'wk_helloworld@'
password = '****************'
 
conn = smtplib.SMTP_SSL('', 465)
(fromaddr, password) 
msg = ()
msg.set_content('<h2>HTML mail<h2>' + '<div style="border:1px:solid red">HTMLE-mail content</div>', 'html', 'UTF-8')
msg['subject'] = 'HTML Mail'
msg['from'] = 'Obsessed<%s>' % fromaddr
msg['to'] = 'Bashful<%s>' % '929667257@'
(fromaddr, ['929667257@'], msg.as_string())

()

3) Send graphic emails

To insert an image in an email, you need to add an attachment by first calling the add_attachment() method of EmailMessage, which takes the following parameters:

  • maintype: specifies the main type of the attachment
  • subtype: specifies the subtype of the attachment
  • filename: Specifies the filename of the attachment.
  • cid=img: Specifies the resource ID of the attachment.

Insert the attached image via the <img.../> element (referencing the attachment's cid attribute)

import smtplib
import 
import 

fromaddr = 'wk_helloworld@'
password = '****************'
toaddr = '929667257@'
 
conn = smtplib.SMTP_SSL('', 465)
(fromaddr, password) 
msg = ()
first_id = .make_msgid()
msg.set_content('<h2>HTML mail<h2>' 
    + '<div style="border:1px:solid red">htmlE-mail content</div>' 
    + '<img src="cid:' + first_id[1:-1] + '">', 'html', 'UTF-8')
msg['subject'] = 'HTML Mail'
msg['from'] = 'wk<%s>' % fromaddr
msg['to'] = 'k<%s>' % toaddr

# Add attachments
with open('Figure', 'rb') as f:
 # When a cid is specified for an attachment, the body of the email can refer to the image by that cid.
 msg.add_attachment((), maintype='image', subtype='jepg', filename='', cid=first_id)

with open('Figure', 'rb') as f:
 msg.add_attachment((), maintype='image', subtype='jepg', filename='')
# with open('graph', 'rb') as f.
#  msg.add_attachement((), maintype='image', subtype='gif', filename='')

(fromaddr, [toaddr], msg.as_string())

()

This is the whole content of this article.