【问题标题】:how to send mail using phpmailer (laravel); [closed]如何使用 phpmailer (laravel) 发送邮件; [关闭]
【发布时间】:2021-03-13 16:33:53
【问题描述】:

我想在使用 phpmailer(laravel) 注册后向用户发送电子邮件验证链接。几天前一切正常,但现在已经停止工作。我真的不知道出了什么问题。它总是给我电子邮件错误('抱歉出了点问题,我们无法向您发送电子邮件验证链接')

mail_handler.php

 <?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;


// Load Composer's autoloader
require 'vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);


require_once 'config.php';
$user = new Auth();

if (isset($_POST['action'])&& $_POST['action']=='register'){
    $name = $user->test_input($_POST['name']);
    $email = $user->test_input($_POST['email']);
    $pass = $user->test_input($_POST['password']);



    $hpass = password_hash($pass, PASSWORD_DEFAULT);

    if ($user->user_exist($email)) {
        echo $user->showMessage('warning', 'This Index number is already registered!');
    }
    else{
        if ($user->register($name,$email,$hpass,$email)) {
            echo'';
            $SESSION['user']= $email;
            try{
                $mail->isSMTP();
                $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
                $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
                $mail->Username   = Database::USERNAME;                     // SMTP username
                $mail->Password   = Database::PASSWORD;                               // SMTP password
                $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
                $mail->Port       = 587; 
        
                //Recipients
                $mail->setFrom(Database::USERNAME,'ENSapp');
                $mail->addAddress($email); 
        
                 // Content
                $mail->isHTML(true);  
                $mail->Subject = 'E-mail Verification';
                $mail->Body    = '<h3>Click the below link to verify your E-Mail.<br><a
                href="http://localhost/admin/verify-email.php?email='.$email.'">
                href="http://localhost/admin/verify-email.php?email='.$email.'
                </a><br>Regards<br> Executives!</h3>'; 
                
                $mail->send();
                echo $user->showMessage('success','Registration succefull we have sent you an E-Mail verification link. <strong>Please Sign In and check your e-mail to verify now!</strong>');
               
            }
            catch(Exception $e){
                echo $user->showMessage('info','Sorry something went wrong we could not send you an email verification link');
             
            }
        
          
        }
        else{
            echo $user->showMessage('danger','Something went wrong! Please try again later! ');
        }
    }
}

  
?>

config.php

<?php
class Database {
    const USERNAME = 'example@gmail.com';
    const PASSWORD = '*******';
    

    // Checking Input 
    public function test_input($data){
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    // Error success message alert
    public function showMessage($type,$message){
        return '<div class="alert alert-'.$type.' alert-dismissible "> 
                    <button type="button" class="close" 
                    data-dismiss="alert">&times;</button>
                    <strong class="text-center"> '.$message.' </strong>
                    
                     </div>';
    }

    

?>

【问题讨论】:

  • 我希望$e 可能会给你一些关于这方面的细节,不是吗?
  • 为什么不打印$e-&gt;getMessage()毕竟这就是为什么会有例外让你知道哪里出了问题。你自己弥补的东西通常没那么有用
  • 这真的不像 Laravel 代码。 Laravel 有 its own mailer interface 工作正常,所以在 Laravel 上下文中我建议坚持使用它。

标签: php laravel phpmailer


【解决方案1】:

切勿在电子邮件中发送 html 表单。 相反,您可以生成一个 6 位随机数并将其发送到该电子邮件中,并将此数字保存在会话中。 当用户输入此代码验证他的帐户时,您可以将他输入的代码与会话中存储的值进行比较。

另外:您是否开启了“不太安全的应用访问”?

【讨论】:

  • 是的,我已经打开了与生成 6 位随机数相关的“不太安全的应用程序访问”,并发送到电子邮件我该怎么做
  • 我不太明白你在说什么。
  • 如果你能清楚地告诉我你想要什么吗?因为我不明白你想要什么
猜你喜欢
  • 2013-11-29
  • 2020-04-05
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 2022-10-17
  • 2018-02-24
  • 1970-01-01
相关资源
最近更新 更多