【发布时间】: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">×</button>
<strong class="text-center"> '.$message.' </strong>
</div>';
}
?>
【问题讨论】:
-
我希望
$e可能会给你一些关于这方面的细节,不是吗? -
为什么不打印
$e->getMessage()毕竟这就是为什么会有例外让你知道哪里出了问题。你自己弥补的东西通常没那么有用 -
这真的不像 Laravel 代码。 Laravel 有 its own mailer interface 工作正常,所以在 Laravel 上下文中我建议坚持使用它。