PHP mail not being received, appears to be successful — hosted on AWS

女生的网名这么多〃 提交于 2019-12-08 08:08:07

问题


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);
            }
        }

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. ^_^

Edit: 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.


回答1:


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. 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!).

  2. 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.

  3. 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.




回答2:


$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Coolest guy" . "\r\n";

ini_set("sendmail_from","webmaster@".$_SERVER["SERVER_NAME"]);
mail($to,$subject,$message,$headers);


来源:https://stackoverflow.com/questions/22927929/php-mail-not-being-received-appears-to-be-successful-hosted-on-aws

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!