【发布时间】:2018-06-01 17:43:26
【问题描述】:
我现在是第一次使用 PHPmailer,因为我以后需要使用附件,而 mail 中的构建不是那么好。
我正在使用示例代码,尽管我没有收到任何错误 + Message has been sent 没有消息到达目标电子邮件。
这是我使用的代码。可以忽略未注释的内容(我需要它们作为表单,提交时应发送电子邮件)
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require '../vendor/autoload.php';
// Fetching Values from URL.
//$phone = $_POST['phone1'];
//$email_var = $_POST['email1'];
$email_var = 'sender.email.info@gmx.net';
$email_var = filter_var($email_var, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email_var, FILTER_VALIDATE_EMAIL)) {
$mail = new PHPMailer(); // Passing `true` enables exceptions
try {
//$mail->SMTPDebug = 2; // Enable verbose debug output
//$mail->isSMTP(); // Set mailer to use SMTP
//$mail->Host = 'SERVER'; // Specify main and backup SMTP servers
//$mail->SMTPAuth = true; // Enable SMTP authentication
//$mail->Username = 'USER'; // SMTP username
//$mail->Password = 'PW'; // SMTP password
//$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
//$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('receiver.email@gmx.net');
//$mail->addAddress('receiver.email@gmx.net'); // Add a recipient
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = '<div style="padding:50px; color:white;">'
. '<br/> Kautionsrückzahlung Interesse von: <br/><br/>'
//. 'Telefon: ' . $phone . '<br/>'
. 'Email: ' . $email_var . '<br/>';
$mail->AltBody = '<div style="padding:50px; color:white;">'
. '<br/> Kautionsrückzahlung Interesse von: <br/><br/>'
//. 'Telefon: ' . $phone . '<br/>'
. 'Email: ' . $email_var . '<br/>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
我现在尝试了不同的东西,似乎我是否使用服务器 PW 等都没有关系。我假设它只适用于 localhost。
编辑:
澄清一下,我不在本地主机上。我有我的服务器的数据,但显然我没有在问题中包含数据。我将它们更改为USER、SERVER 和PW。
但不管它们是否存在或未注释我都得到Message has been sent在这两种情况下。
【问题讨论】:
-
1.检查垃圾邮件文件夹。 2. 检查服务器日志。 3.检查端口是否开放。