PHP的邮件没有收到,看来是成功的 - 托管在AWS [英] PHP mail not being received, appears to be successful -- hosted on AWS

查看:185
本文介绍了PHP的邮件没有收到,看来是成功的 - 托管在AWS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上建立一个标准联系人窗体被托管在亚马逊网络服务。我认为这是给我找麻烦的部分如下:

I'm building a standard contact form on a website being hosted on Amazon Web Services. The part that I believe is giving me trouble is as follows:

        $to = "theCoolestGuy@gmail.com";

        $name = trim($_POST['name']);
        $email = trim($_POST['email']);
        $message = trim($_POST['message']);

        $subject = "The best email subject of all time.";

        $message =  "Name: $name \r\n Email: $email \r\n Message: $message";
        $headers = "From:" . "veryCoolPerson@gmail.com";
        $mailsent = mail($to, $subject, $message, $headers);

        if($mailsent) {
            if ( $js === 'no-js' ) {
                header('Location: thankyoupage.php');
            }
            else {
                $response = array('status' => 'success');

                echo json_encode($response);
            }
        }

在上面的code,邮件似乎已成功发送。 $ mailsent 等于 1 和code返回 {状态:成功} 。我遇到一些困难,弄清楚为什么我没有收到邮件时, $ mailsent 的结果是

In the above code, the mail appears to have successfully sent. $mailsent equals 1 and the code returns { status: 'success' }. I'm having some difficulty figuring out why I'm not receiving the mail when the result of $mailsent is true.

任何帮助,非常感谢。 ^ _ ^

Any help is much obliged. ^_^

编辑:如果这是我的措辞问题的一个不错的方法,只是让我知道。我或多或少试图弄清楚为什么 $ mailsent 变量返回truthy'和我没有收到邮件。

If this is a bad way of phrasing my question, just let me know. I'm more or less trying to figure out why the $mailsent variable is returning 'truthy' and I'm not receiving mail.

推荐答案

在Amazon Web Services托管时,标准的PHP邮件的方法不能正常工作;看来不少人都遇到了这一点。有几个步骤来建立亚马逊SES服务,并得到这个才能正常工作。

The standard PHP mail method doesn't work properly when hosted on Amazon Web Services; it appears quite a few others have ran into this as well. There are a couple steps to setting up the Amazon SES service and getting this to work correctly.

  1. 添加您的从电子邮件到亚马逊SES的经过验证的电子邮件地址。登录到您的Amazon Web Services账户。在应用服务头去的 SES 。导航到验证发件人 - 电子邮件地址的左侧导航。点击按钮验证新的电子邮件地址,并按照通过邮件验证过程(这很简单!)。

  1. Add your FROM email to the verified email addresses in Amazon SES. Login to your Amazon Web Services account. Under the App Services header go SES. Navigate to Verified Senders - Email Addresses on the left navigation. Click the button 'Verify a New Email Address' and follow through the email verification process (it's simple!).

获取您的SMTP凭据。确认您的电子邮件进入 SMTP服务在亚马逊网络服务后> SES的管理页面,我们之前导航到。点击创建我的SMTP凭证按钮。它会给你您的凭据一次,并且只有一次!所以保存这些地方供以后使用。

Get your SMTP credentials. After verifying your email go to the SMTP Services within the Amazon Web Services > SES management page we navigated to before. Click the 'Create my SMTP Credentials' button. It will give you your credentials once, and once only! So save these somewhere for later use.

创建表单处理程序。创建一个PHP文件,将处理表单提交。我选择使用 PHPMailer的库来简化事情。下面是得到这个东西为我工作的code:

Create the form handler. Create a php file that will handle form submissions. I chose to use the PHPMailer library to simplify things. Here's the code that got this thing to work for me:

    // get your submitted fields
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    // include phpmailer
    require_once('class.phpmailer.php');

    try {
        // smtp settings
        // set this to true to throw exceptions
        // if you're running into issues
        $mail = new PHPMailer();

        $mail - > IsSMTP();
        $mail - > SMTPAuth = true;
        $mail - > SMTPSecure = "tls";
        $mail - > Host = "email-smtp.us-east-1.amazonaws.com"; // be sure this matches your server! can be found in the smtp settings from step 2
        $mail - > Username = "XXXXXXXXXXXXXXXXXXXXXX"; // your SMTP username from step 2!
        $mail - > Password = "XXXXXXXXXXXXXXXXXXXXXX"; // your SMTP password from step 2!

        $mail - > SetFrom('verfiedEmail@domain.com', 'FromName'); // from email - verified email address in step 1!
        $mail - > Subject = "Your Email Subject!"; //subject
        $body = "<strong>The body of your message!</strong>"; // Body of your message
        $mail - > MsgHTML($body);

        // recipient
        $mail - > AddAddress("email@domain.com", "RecepientName"); // this is where the email will be sent

        // success
        if ($mail - > Send()) {
                // woohoo! the mail sent! do your success things here.
            }
        }

    // errors :(
    } catch (phpmailerException $e) {
        echo $e - > errorMessage();
    } catch (Exception $e) {
        echo $e - > getMessage();
    }

执行这些步骤后,你应该有它在任何时候工作。玩得开心。

After following these steps you should have it working in no time. Have fun.

这篇关于PHP的邮件没有收到,看来是成功的 - 托管在AWS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆