0

I've launch an AWS LightSail Bitnami LAMP instance. client side simple html form

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Form</title>
</head>

<body>

<form action="contact2.php" method="post">
<input name="firstName" placeholder="firstName" type="text" size="30" />
<input name="lastName" placeholder="lastName" type="text" size="30" />
<input name="email" placeholder="email" type="text" size="30" />
<input name="phone" placeholder="phone" type="text" size="30" />
<input name="subject" placeholder="subject" type="text" size="30" />
<textarea name="message"  placeholder="message" type="text" size="30"></textarea>

<input name="submit" type="submit" value="Send This">
</form>

</body>
</html>

backend php code

<?php
if (!empty($_POST)) {
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $toEmail = 'mymail@mydomain.com';
    $emailSubject = 'test';
    $headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=iso-8859-1'];
    
    $body = "Name: $firstName $lastName .\n".
            "Phone: $phone.\n".
            "Message: $message.\n";
    
    mail($toEmail, $emailSubject, $body, $headers);
}
?>

Calling the contact.php returns with status 200 but the email does not get sent.

The same exact code on Heruku works perfect and the email is received

Kukula Mula
  • 1,431
  • 3
  • 13
  • 26
  • How have you made sure that your email got sent? You're not doing any error checking. If you're going by whether it is received, then the sending server may be of influence (for instance, if the sending IP has a bad reputation). – Ro Achterberg Nov 15 '20 at 20:57

2 Answers2

0

returns with status 200

Of course it does. There is nothing in your code to tell it to do otherwise....

if (!mail($toEmail, $emailSubject, $body, $headers)) {
   header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
   print "Failed to send email";
   exit;
}

But a successful return value from the mail() function only means that PHP has handed off the message on the first step of its journey (there will be at least 3 and often more hops before it arrives in someone's inbox).

BTW - are you aware that the code you have shown us allows anyone to send any email anywhere? You might want to google for PHP mail header injection.

symcbean
  • 45,607
  • 5
  • 49
  • 83
  • As the code works perfectly on Heruku I believe the problem isn't there, having said that, you are right in production/ after the basic functionality works (this is a simple test to see everything is configured correctly) there should be even more validation checks to the one you suggested. – Kukula Mula Nov 16 '20 at 06:53
  • "There should be even more validation checks" - not in the PHP code which sends the email. – symcbean Nov 16 '20 at 12:42
0

The page returns 200 as the mail() function doesn't throw errors. If you really want to check if the mail has been sent or not, you need check it with a if condition like this:

if(mail($to, $subject, $body, $headers)

In PHP, your page returns 200 most of the time, until unless you are using a framework like Codeigniter or Laravel.

Mail Issue

mail() function in php uses sendmail program from linux to send emails. If AWS instance is not able to send the mail and heroku instance is able to send it, it simply means that heroku instance has sendmail installed.

To resolve this issue here, you need to SSH into your AWS Lightsail instance and run this command depending on your Linux flavour

sudo apt-get install sendmail

If that doesn't work, try this

sudo yum install sendmail

Debugging Further

If installing doesn't solve your issue, check for this.

Check if the service has been started or not

service sendmail status

Note: Output of above command should be something - Active: active (running)

If it's not started, start it by running the following command

service sendmail start

Recommendation

This will work if you running a small project. But if you use this to send a lot of emails, your instance might get blocked by AWS for sending Emails via this method. Also, most of the emails sent via this method end up going to spam folder.

It is recommended to use one of the following 3rd party services to send emails

  1. PHP Mailer (Send mail using Gmail credentials)
  2. AWS SES (Sending cost is $1/10000 emails)
  3. Mailgun, Sendgrid, Elasticemail etc.
Ashutosh Kumar
  • 345
  • 2
  • 10
  • Thanks, I've installed sudo apt-get install sendmail and the status is Active: active (running) the mail function still isn't sending. Configuring the php.ini file adding SMTP=localhost smtp_port=25 didn't help either – Kukula Mula Nov 16 '20 at 09:20
  • Try running this: echo "Subject: sendmail test" | sendmail -v my@email.com – Ashutosh Kumar Nov 16 '20 at 15:14
  • If that doesn't work either than there must be two possibilities: 1. Firewall blocks port 25 (Check by running : sudo ufw status), 2. Blocked Port from AWS security rules. (Check the outbound security role for your Instance). – Ashutosh Kumar Nov 16 '20 at 15:16