Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

$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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top