How to send SMS verification code and email notification when user logs in in PHP

王林
Release: 2023-09-26 20:42:02
Original
1397 people have browsed it

How to send SMS verification code and email notification when user logs in in PHP

How to send SMS verification codes and email notifications when users log in in PHP

With the rapid development of the Internet, more and more applications require users to Sign-in functionality to ensure security and personalize your experience. In addition to basic account and password verification, in order to improve user experience and security, many applications will also send mobile phone SMS verification codes and email notifications when users log in. This article will describe how to implement this functionality in PHP and provide corresponding code examples.

1. Send SMS verification code

1. First, you need a service provider that can send SMS. These service providers usually provide some API interfaces through which we can send text messages. Commonly used SMS service providers include Alibaba Cloud, Tencent Cloud, etc.

2. Next, you need to use the SMS service provider’s API in your application to send SMS messages. The following is a sample code for using Alibaba Cloud SMS service:

require_once 'vendor/autoload.php';

use AlibabaCloudClientAlibabaCloud;
use AlibabaCloudClientExceptionClientException;
use AlibabaCloudClientExceptionServerException;

// 配置阿里云的AccessKey
AlibabaCloud::accessKeyClient('your-access-key-id', 'your-access-key-secret')
    ->regionId('cn-hangzhou')
    ->asDefaultClient();

// 发送短信
try {
    $result = AlibabaCloud::rpcRequest()
        ->product('Dysmsapi')
        ->version('2017-05-25')
        ->action('SendSms')
        ->method('POST')
        ->host('dysmsapi.aliyuncs.com')
        ->options([
            'query' => [
                'RegionId' => "cn-hangzhou",
                'PhoneNumbers' => "your-phone-number",
                'SignName' => "your-sign-name",
                'TemplateCode' => "your-template-code",
                'TemplateParam' => json_encode([
                    'code' => '123456'
                ])
            ],
        ])
        ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}
Copy after login

Please note that your-access-key-id, your-access-key-secret# in the code Replace ##, your-phone-number, your-sign-name, and your-template-code with your actual information.

2. Send email notification

1. You need an SMTP server that can send emails. Common SMTP servers include QQ mailbox, 163 mailbox, etc. You need to configure the SMTP server related information in the application.

2. Next, you can use PHP’s

mail function to send emails. The following is a sample code that uses the QQ mailbox SMTP server to send emails:

<?php
$smtpserver = "smtp.qq.com"; // SMTP服务器
$smtpserverport = 465; // SMTP服务器端口
$smtpusermail = "your-email@qq.com"; // SMTP服务器的用户邮箱
$smtpemailto = "recipient-email@qq.com"; // 发送给谁
$smtpuser = "your-email@qq.com"; // SMTP服务器的用户帐号
$smtppass = "your-password"; // SMTP服务器的用户密码
$mailsubject = "Login Notification"; // 邮件主题
$mailbody = "Someone has logged into your account."; // 邮件内容
$mailtype = "HTML"; // 邮件格式(HTML/TXT)

$smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass);
$smtp->debug = FALSE; // 调试模式

$result = $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

if ($result == true) {
    echo "邮件发送成功!";
} else {
    echo "邮件发送失败!";
}

class Smtp
{
    ...

    function sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype)
    {
        ...
    }
}
Copy after login
Please note that

smtp.qq.com, your-email@qq.com# in the code Replace ## and your-password with your actual information. Summary

The above are methods and code examples on how to send SMS verification codes and email notifications when users log in in PHP. Through these features, users can log in to applications more securely and receive important notifications in a timely manner. Hope this article is helpful to you!

The above is the detailed content of How to send SMS verification code and email notification when user logs in in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template