Php 邮件程序类无法在 Ubuntu 数字海洋服务器上运行

问题描述 投票:0回答:1

Php 邮件程序类的状态为 true,但未在 Ubuntu 数字海洋服务器上发送电子邮件。 我正在使用 php 版本 7.4 和 php mailer 类版本 5.1

$email_body = "please verify your account by clicking on the link in the message!";
$sender_name = "Test";
$user_name = "User Name";
$sender_email_address = "[email protected]";
require_once('../php_mailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->Host = $host; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->setfrom($sender_email_address);
$mail->FromName = $sender_name;
$mail->Subject = "Registration Email";
$mail->From = $sender_email_address;
$mail->AltBody = $email_body; // optional, comment out and test
$mail->MsgHTML($email_body);
$mail->AddAddress($email, $user_name);


if (!$mail->Send()) {
    throw new Exception("Email could not be sent. Mailer Error: {$mail->ErrorInfo}");
} else {
    echo "Email was sent successfully!";
}

由于我的旧服务器是 Hostgator,它运行良好,因此我也需要它在数字海洋上运行。

php class phpmailer
1个回答
-2
投票

我希望这对你有帮助!!

您可以通过在 Composer 中运行以下命令来在 PHP 项目中安装 PHPMailer 库。

composer require phpmailer/phpmailer

您可以使用下面的代码从本地网络服务器发送电子邮件。


<?php 
    require_once "vendor/autoload.php"; //PHPMailer Object 
    $mail = new PHPMailer; //From email address and name 
    $mail->From = "[email protected]"; 
    $mail->FromName = "Full Name"; //To address and name 
    $mail->addAddress("[email protected]", "Recepient Name");//Recipient name is optional
    $mail->addAddress("[email protected]"); //Address to which recipient will reply 
    $mail->addReplyTo("[email protected]", "Reply"); //CC and BCC 
    $mail->addCC("[email protected]"); 
    $mail->addBCC("[email protected]"); //Send HTML or Plain Text email 
    $mail->isHTML(true); 
    $mail->Subject = "Subject Text"; 
    $mail->Body = "<i>Mail body in HTML</i>";
    $mail->AltBody = "This is the plain text version of the email content"; 
    if(!$mail->send()) 
        echo "Mailer Error: " . $mail->ErrorInfo; 
    else
        echo "Message has been sent successfully"; 
    if(!$mail->send()) 
        echo "Mailer Error: " . $mail->ErrorInfo; 
    else 
        echo "Message has been sent successfully"; 
?>
© www.soinside.com 2019 - 2024. All rights reserved.