Python 3 realizes the function of sending email and SMS verification code

Time:2021-7-21

Python3 realizes sending email:

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
my_ sender = ' [email protected] '# sender email account
my_ Pass = '* * * *' ා authorization code of the sender's mailbox
my_ user = ' [email protected] The recipient's email account number, I'll send it to myself
def mail():
 ret= True
 try:
  MSG = mimetext ('captcha: 123456 ','plain','utf-8 ')
  msg['From'] = formataddr(["From nicead.top", my_ Sender] # the corresponding sender's email nickname and sender's email account in brackets
  msg['To'] = formataddr(["FK", my_ User] # the corresponding recipient's email nickname and recipient's email account number in brackets
  MSG ['subject '] ='verification code' # the subject of the e-mail, or the title
  server = smtplib.SMTP_ SSL ("SMTP. QQ. Com", 465) # SMTP server in sender's mailbox, port 25
  server.login(my_ sender, my_ The corresponding in brackets are the sender's email account and password
  server.sendmail(my_ sender, [my_ user, ], msg.as_ String ()) # the corresponding in brackets are the sender's email account, the recipient's email account and the sending email account
  Server. Quit() # close connection
 Except exception as e: # if the statement in try is not executed, the following RET = false will be executed
  ret = False
 return ret
ret = mail()
if ret:
 Print ("mail sent successfully")
else:
 Print ("mail send failed")

Note the authorization code: it needs to be obtained from the settings in the email, such as QQ mailbox

  Implementation of SMS verification code in Python 3  

#Interface type: mutual billion wireless trigger SMS interface, support sending verification code SMS, order notification SMS, etc.
#Account registration: please open an account through this address http://sms.ihuyi.com/register.html
#Note:
#(1) During debugging, please use the system default SMS content: your verification code is: [variable]. Please don't disclose the verification code to others.;
#(2) Please use apiid and apikey to call the interface;
#(3) The code is only for the reference of accessing mutual billion wireless SMS interface, and customers can write it according to their actual needs;
#Send SMS
#APIID:C11345804
#APIKEY:735d183ae02189f678c26800ac19b03a
# !/usr/local/bin/python
# -*- coding:utf-8 -*-
import http.client
import urllib
host = "106.ihuyi.com"
sms_send_uri = "/webservice/sms.php?method=Submit"
#The user name is login user center > verification code SMS > Product Overview > apiid
account = "************"
#Password to view the password, please log in to user center > verification code SMS > Product Overview > apikey
password = "**********************"
def send_sms(text, mobile):
 params = urllib.parse.urlencode(
  {'account': account, 'password': password, 'content': text, 'mobile': mobile, 'format': 'json'})
 headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
 conn = http.client.HTTPConnection(host, port=80, timeout=30)
 conn.request("POST", sms_send_uri, params, headers)
 response = conn.getresponse()
 response_str = response.read()
 conn.close()
 return response_str
if __name__ == '__main__':
 #Target mobile phone number to receive SMS
 Mobile = mobile number
  #SMS content
 Text = "your verification code is: 121254. Please don't leak the CAPTCHA to others“
 ret = send_sms(text, mobile).decode('utf-8')
 import json
 ret = json.loads(ret)
 print(ret)

be careful:

#The user name is login user center > verification code SMS > Product Overview > apiid
account = "************"
#Password to view the password, please log in to user center > verification code SMS > Product Overview > apikey
password = "**********************"

You need to register an account on the official website of Huyi wireless trigger SMS interface, which will provide ten free SMS verification codes. http://sms.ihuyi.com/register.html

summary

The above is the introduction of python3 to realize the function of sending email and SMS verification code. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!
If you think this article is helpful to you, please reprint, please indicate the source, thank you!

Recommended Today

Development tips: Unicode sorting and regular matching

brief introduction We know that computers first emerged in foreign countries. Considering the computer performance and common foreign characters at that time, ASCII was used in computers at the beginning. After all, the characters that ASCII coding can represent are limited. With the development of computers and the popularity all over the world, more coding […]