How to create beautiful email templates and send them to users through the SMTP service of qq mailbox

Recently, I was writing a registration function that automatically sends an email to notify the verification code. Unfortunately, I have never learned the front-end. I only have the QSS basics of writing Qt, so I have to use the web page to design the email format I want. The final effect is as follows :

I also promote my own project ShaderLab, which can run most of the shader codes on ShaderToy. Friends who study graphics can pay attention to it, and friends who study front-end and are interested in developing WebGL can also pay attention:)

The recent development progress and results are at this link

How to create beautiful email formats through templates

Here we recommend using the mailbox design artifact CamppainMonitor (this website looks like a design website at first glance)

After registering and logging in, enter the OverView interface and click the Create button to create a template.

Click to create email design

After entering the plan theme, click the Design Email button and select a template for design.

Click on the theme to select the rich design templates provided by CamppainMonitor for design.

Choose one and start designing online

After the design is completed, return to the OverView interface and click the magnifying glass button

Click to print

Enter the pure HTML display interface, right-click -> Save as HTML, select the last web page, all

You can see that the download has been successful, the pure html code is 52KB

This conscientious website also helps you save the uploaded design images, so you don’t have to worry about the image becoming invalid when you open it after converting it to HTML! !

How to send an email to a specified mailbox through a script

First of all, you need to understand the relevant knowledge of the SMTP protocol. There are many blogs on the Internet, so I won’t go into details. Please refer to this blog for details.

First of all, we need to activate the SMTP service of QQ mailbox and let QQ mailbox serve as the “transfer station” for sending our emails. Please refer to this article.

In addition, you can register Foxmail, configure the port and sending protocol of the QQ mailbox STMP service, enable the SMTP service and obtain the authorization code, and then let the sending server forward our emails through socket communication.

We use python to implement socket communication as an email sending script. We originally wanted to use C++/libcurl, but found that the default attachment upload size of libcurl is limited to 16K. Later, we considered a pure socket implementation in C++, but there were a lot of pitfalls. I accidentally discovered a piece of python code that worked, and decided to join the python army.

Name the designed html email email_template.html and the python script in a directory, and run the following code,

Be sure to replace your username and authorization code with your own.

# SMTPClient.py
from socket import *
import base64

msg = open("email_template.html",'r',encoding='UTF-8').read()
endMsg = "\r\\
.\r\\
"
# Select an email service
mailServer = "smtp.qq.com"
# Sender address and receiver address, from and to
fromAddress = "[email protected]"
toAddress = "[email protected]"
# Sender, verification information. Since the email input information will use base64 encoding, it needs to be encoded.
username = str(base64.b64encode(b"xxxx"),encoding='UTF-8') # Enter the encoding corresponding to your username
password = str(base64.b64encode(b"xxxx"),encoding='UTF-8') # This is not your own password, but the corresponding authorization code when opening the SMTP service

#Create client socket and establish connection
serverPort = 587 # SMTP uses port 587
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailServer, serverPort)) # connect can only receive one parameter
# Receive information from client socket
recv = clientSocket.recv(1024).decode()
print(recv)
if '220' != recv[:3]:
    print('220 reply not received from server.')

# Send HELO command and print server reply
# Start interacting with the server. The server will return status code 250, indicating that the requested action is completed correctly.
heloCommand = 'HELO MSG\r\\
'
clientSocket.send(heloCommand.encode()) # Pay attention to encoding and decoding information at any time
recv1 = clientSocket.recv(1024).decode()
print(recv1)
if '250' != recv1[:3]:
    print('250 reply not received from server.')

#Send the "AUTH LOGIN" command to verify identity. The server will return status code 334 (the server is waiting for the user to enter verification information)
clientSocket.sendall('AUTH LOGIN\r\\
'.encode())
recv2 = clientSocket.recv(1024).decode()
print(recv2)
if '334' != recv2[:3]:
    print('334 reply not received from server.')

#Send verification information
clientSocket.sendall((username + '\r\\
').encode())
recvName = clientSocket.recv(1024).decode()
print(recvName)
if '334' != recvName[:3]:
    print('334 reply not received from server')

clientSocket.sendall((password + '\r\\
').encode())
recvPass = clientSocket.recv(1024).decode()
print(recvPass)
# If the user authentication is successful, the server will return status code 235
if '235' != recvPass[:3]:
    print('235 reply not received from server')

# After the TCP connection is established, you can start sending emails through user authentication. The transmission of mail begins with the MAIL command, which is followed by the sender's address.
#Send the MAIL FROM command and include the sender's email address
clientSocket.sendall(('MAIL FROM: <' + fromAddress + '>\r\\
').encode())
recvFrom = clientSocket.recv(1024).decode()
print(recvFrom)
if '250' != recvFrom[:3]:
    print('250 reply not received from server')

# Then the SMTP client sends one or more RCPT (abbreviation for recipient recipient) commands in the format of RCPT TO: <recipient address>.
#Send the RCPT TO command and include the recipient’s email address, returning status code 250
clientSocket.sendall(('RCPT TO: <' + toAddress + '>\r\\
').encode())
recvTo = clientSocket.recv(1024).decode() # Note that UDP uses sendto, recvfrom
print(recvTo)
if '250' != recvTo[:3]:
    print('250 reply not received from server')

#Send the DATA command to indicate that the email content is about to be sent. The server will return status code 354 (start email input, end with ".")
clientSocket.send('DATA\r\\
'.encode())
recvData = clientSocket.recv(1024).decode()
print(recvData)
if '354' != recvData[:3]:
    print('354 reply not received from server')

# Edit email information and send data
subject = "ShaderLab Regstration"
contentType = "text/html"

message = 'from:' + fromAddress + '\r\\
'
message + = 'to:' + toAddress + '\r\\
'
message + = 'subject:' + subject + '\r\\
'
message + = 'Content-Type:' + contentType + '\t\\
'
message + = '\r\\
' + msg
clientSocket.sendall(message.encode())

# End with ".". The request returns 250 successfully
clientSocket.sendall(endMsg.encode())
recvEnd = clientSocket.recv(1024).decode()
print(recvEnd)
if '250' != recvEnd[:3]:
    print('250 reply not received from server')

# Send the "QUIT" command to disconnect from the mail server
clientSocket.sendall('QUIT\r\\
'.encode())

clientSocket.close()

You can see that the email has been successfully forwarded: