PHP邮件未到达Amazon EC2上的目标

PHP邮件未到达Amazon EC2上的目标,php,email,amazon-ec2,smtp,phpmailer,Php,Email,Amazon Ec2,Smtp,Phpmailer,我正在尝试使用PHP的mail()函数从AmazonEC2上的服务器发送电子邮件。函数正在返回true,但电子邮件未到达目的地。我研究了这个话题,试图设置sendmail和sendmail cf,但没有解决这个问题。代码如下: $EmailTo = ...; //destination email $Subject = "New Message Received."; $Body = ""; $Body .= "Name: "; $Body .= $name; $Body .= "\n"; $B

我正在尝试使用PHP的
mail()
函数从
AmazonEC2
上的服务器发送电子邮件。函数正在返回
true
,但电子邮件未到达目的地。我研究了这个话题,试图设置
sendmail
sendmail cf
,但没有解决这个问题。代码如下:

$EmailTo = ...; //destination email
$Subject = "New Message Received.";
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $msg_subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

try{
     $success = mail($EmailTo, $Subject, $Body, "From:".$email);
     echo $success;
}catch(Exception $e){
     echo "Something went wrong. Please try again.";
}
所有变量均取自表单的
$\u POST
请求,并经过验证


欢迎提供任何提示。

您可以使用PHPMailer类,而不是使用php mail()函数,这可能是最好的选择,因为它也可以在没有本地邮件服务器(集成SMTP支持)的情况下运行

您可以通过
composer require phpmailer/phpmailer
安装或从此处下载。您可以在同一链接上阅读更多有关它的信息

下面是一个简单的脚本示例

require 'vendor\autoload.php'; // For composer

require 'path/to/PHPMailer/src/Exception.php';  // If you are 
require 'path/to/PHPMailer/src/PHPMailer.php';  // downloading it without
require 'path/to/PHPMailer/src/SMTP.php';       // composer then include these.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

try 
{
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp1.example.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    // Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} 
catch (Exception $e) 
{
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
require'vendor\autoload.php';//作曲家
需要'path/to/PHPMailer/src/Exception.php';//如果你是
需要'path/to/PHPMailer/src/PHPMailer.php';//下载时不需要
需要'path/to/PHPMailer/src/SMTP.php';//作曲家然后包括这些。
使用PHPMailer\PHPMailer\PHPMailer;
使用PHPMailer\PHPMailer\Exception;
使用PHPMailer\PHPMailer\SMTP;
尝试
{
//服务器设置
$mail->SMTPDebug=SMTP::DEBUG_SERVER;//启用详细调试输出
$mail->isSMTP();//使用SMTP发送
$mail->Host='smtp1.example.com';//设置要发送的SMTP服务器
$mail->SMTPAuth=true;//启用SMTP身份验证
$mail->Username=user@example.com“;//SMTP用户名
$mail->Password='secret';//SMTP密码
$mail->SMTPSecure=PHPMailer::ENCRYPTION\u STARTTLS;//启用TLS加密;`PHPMailer::ENCRYPTION\u SMTPS`
$mail->Port=587;//要连接的TCP端口,请将465用于上面的'PHPMailer::ENCRYPTION_SMTPS'
//接受者
$mail->setFrom('from@example.com","梅勒",;
$mail->addAddress('joe@example.net“,”Joe User');//添加收件人
$mail->addAddress('ellen@example.com');//名称是可选的
$mail->addReplyTo('info@example.com","信息",;
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//附件
$mail->addAttachment('/var/tmp/file.tar.gz');//添加附件
$mail->addAttachment('/tmp/image.jpg','new.jpg');//可选名称
//内容
$mail->isHTML(true);//将电子邮件格式设置为HTML
$mail->Subject='主题在这里';
$mail->Body='这是以粗体显示的HTML邮件正文!';
$mail->AltBody='这是非HTML邮件客户端的纯文本正文';
$mail->send();
回音“消息已发送”;
} 
捕获(例外$e)
{
echo“无法发送邮件。邮件程序错误:{$mail->ErrorInfo}”;
}

这是一个复杂的话题。许多邮件服务器不再接受来自IP的电子邮件,这些IP未标记为电子邮件服务器,并且满足一系列保护自己免受垃圾邮件攻击的条件。@MaxMuster我试图编辑邮件系统文件以添加邮件服务器,但没有起作用。在这种情况下,你有什么建议?你检查过邮箱中的垃圾邮件文件夹吗?@TufailAhmad是的,我检查过。没什么,我也有过同样的问题。将发件人(发送邮件地址)放入邮件()修复了我的问题。你可以试试。你能解释一下这将如何解决他们的问题吗?