PHP邮件未到达Amazon EC2上的目的地[重复]

问题描述 投票:1回答:1
我正在尝试使用PHP的mail()函数从Amazon EC2上的服务器发送电子邮件。该函数返回true,但电子邮件未到达目的地。我对该主题进行了研究,并尝试设置sendmailsendmail-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."; }

所有变量均从表单的$_POST请求中获取并经过验证。 

任何提示都值得赞赏。

php email amazon-ec2 smtp phpmailer
1个回答
1
投票
代替使用php mail()函数,您可以使用PHPMailer类,这可能是最好的选择,因为它也可以在没有本地邮件服务器的情况下运行(集成SMTP支持)。

您可以通过composer require phpmailer/phpmailer安装它,或从https://github.com/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 = '[email protected]'; // 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('[email protected]', 'Mailer'); $mail->addAddress('[email protected]', 'Joe User'); // Add a recipient $mail->addAddress('[email protected]'); // Name is optional $mail->addReplyTo('[email protected]', 'Information'); $mail->addCC('[email protected]'); $mail->addBCC('[email protected]'); // 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}"; }

© www.soinside.com 2019 - 2024. All rights reserved.