PHPMailer 无法在网页中工作

PHPMailer not working in web page

我正在尝试使用 PHPMailer 发送电子邮件,但到目前为止它对我不起作用。

在我的 FTP 上,我放了 2 个文件,class.phpmailer.phpsendEMail.php(这个文件是我创建的),内容如下:

<?php
require_once('/var/www/vhosts/MYWEBPAGE/httpdocs/class.phpmailer.php');

$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = "mail.dom.com"; 
$mail->Username = "smpt@mail.com"; 
$mail->Password = "passwd"; 
$mail->Port = 25; 

$mail->setFrom("my@mail.com", "me", 0);

$mail->addAddress("to@mail.com"); 

$mail->Subject = "test"; 
$body = "Hello World!";
$mail->Body = $body; 

if(!$mail->send()) {
echo ("Invoice could not be send. Mailer Error: " . $mail->ErrorInfo);
} else {
echo "Invoice sent!";
}

?>

我错过了什么?当我执行这个文件时,它什么也没显示,我的意思是在 if(!$mail->send()) {... ]

这是因为您没有阅读 the readme 告诉您需要哪些文件,也没有根据提供的示例编写代码。您还没有加载 SMTP class,也没有加载它的自动加载器,因此一旦您尝试发送,它将无法找到 SMTP class。这将像任何其他 PHP 致命错误一样记录在您的网络服务器日志中。

而不是这个:

require_once('/var/www/vhosts/MYWEBPAGE/httpdocs/class.phpmailer.php');

这样做:

require '/var/www/vhosts/MYWEBPAGE/httpdocs/PHPMailerAutoload.php';