0

So I just recently created my first Ec2 instance through AWS. I decided to go with the Ubuntu Server 18.04 LTS (HVM), SSD Volume Type. The first things I did when setting up the server was:

1. sudo apt-get update - updating the machine.
2. sudo apt-get -y upgrade - upgrade the machine to the newest software.
3. sudo apt-get -y install tasksel - installs software called tasksel 
4. sudo tasksel install lamp-server - installs apache, php and mysql.
5. ln -s /var/www/html www  - creates a www link in your home folder. 
6. sudo chown -R ubuntu /var/www/html/ - take ownership of the html folder for easier access.

I then uploaded my html files to the html folder. I am able to access my website using my servers IP address however when I try to send an email it fails. Here is my PHP code:

 <?php
            $to = "email@gmail.com";  //<--------put email here

            $subject = "subject";

            $email = $_POST['email'];
            $headers = "From: ".$email."\r\n";
            $headers .= "Reply-To: ".$email."\r\n";
            $headers .= "CC: \r\n";

            $name = $_POST['realname'];
            $number = $_POST['Phone'];
            $comment = $_POST['Comments'];
            $message = "Name: ".$name."\n"."Phone number: ".$number."\n"."Comment: ".$comment;
            
            $didItSend = null;

            if ($email == "Email" || $name == "Name" || $number == "Phone" || $comment == "Comments" || $email == "" || $name == "" || $number == "" || $comment == ""){
                echo "<p>all fields must be filled with your information and the comment must be filled out!</p>";
            } else {
                $didItSend = mail($to, $subject, $message, $headers);
                if ($didItSend === TRUE){
                    echo "<p>Email sent!</p>";
                    echo "<p> $to , $subject ,  $message , $headers</p>";  // all vars are correct
                } else {
                    echo "<p>Failed to send Email. try entering a vaild email or try a different email account!</p>";
                    echo "<p> $to , $subject ,  $message , $headers</p>"; // all vars are correct
                    
                }
            }
        ?>

at first $didItSend was false when I was trying to send an email then I did:

sudo apt-get install sendmail

and now when I try to send mail $didItSend is true, however the email never gets sent. Is there something I am missing here? What else do I have to do to get this to work? By the way my php code worked for another website I created that was on my schools server and it worked perfectly. So im thinking that I didn't set something up correctly.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Kyle Lynch
  • 37
  • 4
  • You need a fully configured SMTP server, up to now, you don't have more than a mailsystem that can send messages from one user to the other inside your server. Check this page on [how to configure `mail()`](https://www.php.net/manual/en/mail.configuration.php). The best bet would be to use a dedicated mailbox/smtp to send mails with an external mail handler. – Daniel W. Aug 28 '20 at 18:44
  • I dont believe there is one for email. Would I simply add a new security group with a SMTPS rule and set the destination to anywhere? – Kyle Lynch Aug 28 '20 at 18:47
  • "email" and "simply" don't go well together. To properly send trusted mails that don't end up in junk filters, you need to setup several things, including SPF/DKIM DNS records, authentication and handling of transport encryption. The easiest way is to use a provider's smtp relay server. Maybe you want [Amazon Simple Email Service](https://aws.amazon.com/ses/) ? – Daniel W. Aug 28 '20 at 18:51
  • The easiest way for you would be to use something like [`PHPMailer()`](https://github.com/PHPMailer/PHPMailer) and your [gmail or similiar account](https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps). – Daniel W. Aug 28 '20 at 18:53

0 Answers0