0

I am attempting to send the collected value's of a html form as an email to myself via PHP & Ajax. For some reason, I am able to update the UI with a success alert, however there's no actual email sent when I check my inbox. I am under the impression that my PHP script may be ill-structured, because when I log the results of my js function, all of the form values have been correctly captured.

Here is the JS:

function _(id){ return document.getElementById(id); };
function submitForm(){
    var formdata = new FormData();
    formdata.append( "first-name", _("first-name").value );
    formdata.append( "last-name", _("last-name").value );
    formdata.append( "email", _("email").value );
  formdata.append( "subject", _("subject").value );
  formdata.append( "message", _("message").value );
    var ajax = new XMLHttpRequest();
    ajax.open( "POST", "email_me.php" );
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            if(ajax.responseText == "success"){
                alert("Hey! It Worked!!");
            } else {
            // display error
            }
        }
    }
    ajax.send( formdata );
  // Display the key/value pairs
  for (var pair of formdata.entries()) {
      console.log(pair[0]+ ', ' + pair[1]);
  }
}

And Here is the php script (email_me.php file)

<?php
  if(isset($_POST['first-name'], $_POST['last-name'], $_POST['email'], $_POST['subject'], $_POST['message'])){
    $name = $_POST['first-name'];
    $email = $_POST['email'];
    $m = nl2br($_POST['message']);
    $to = "me@gmail.com";
    $from = $email;
    $subject = $_POST['subject'];
    $message = '<p>'.$m.'</p>';
    $headers = "From: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    if( mail($to, $subject, $message, $headers) ){
        echo "success";
    } else {
        echo "The server failed to send the message. Please try again later.";
    }
  }
?>

What seem's to be the issue? Im running the current version of Apache with a localhost, using MAMP Pro btw.

Here are the server logs:

Marker - Aug 23, 2016, 12:34:32 PM

Aug 23 12:35:24 MacBookAir postfix/master[7884]: daemon started -- version 2.11.0, configuration /etc/postfix Aug 23 12:36:24 MacBookAir postfix/master[7884]: master exit time has arrived

Aug 23 12:36:24 MacBookAir postfix/master[7885]: daemon started -- version 2.11.0, configuration /etc/postfix Aug 23 12:37:24 MacBookAir postfix/master[7885]: master exit time has arrived Aug 23 12:37:24 MacBookAir r postfix/master[7886]: daemon started -- version 2.11.0, configuration /etc/postfix

John Jackson
  • 900
  • 2
  • 12
  • 28
  • issue is in from u r sending email from user's email, send email from a constant email and keep user's email in message. $header = "From: noreply@something.com\r\n"; – AmmyTech Aug 23 '16 at 07:09
  • can you possibly edit my code to show an example of this? @Kool-Mind – John Jackson Aug 23 '16 at 07:12
  • check the below code in answer – AmmyTech Aug 23 '16 at 07:19
  • Have setup your host to send email using mail function? – Ahmed Khan Aug 23 '16 at 07:22
  • I am using MAMP Pro, I was under the understanding that it was just a configuration under the Postfix tab in the settings to set up a mailing service @AhmedKhan – John Jackson Aug 23 '16 at 07:32
  • which mail service are you using? – Ahmed Khan Aug 23 '16 at 07:41
  • if you are using gmail you need to set gmail in order to send email using smtp – Ahmed Khan Aug 23 '16 at 07:45
  • mail service to receive email's? well i been trying to use either gmail or yahoo, just a pseudo. If you mean server? I am using apache with a MAMP stack. @AhmedKhan – John Jackson Aug 23 '16 at 07:45
  • well its extremely ambiguous on just how to do that on this server @AhmedKhan – John Jackson Aug 23 '16 at 07:45
  • gmail has change its settings alot to send mail via smtp. if you are using 2 factor verfication it will be difficult. You to set settings. Check the last part of this article to know how to send email using less secure app in gmail. – Ahmed Khan Aug 23 '16 at 07:50
  • http://www.wpbeginner.com/plugins/how-to-send-email-in-wordpress-using-the-gmail-smtp-server/ – Ahmed Khan Aug 23 '16 at 07:50
  • most of that demonstration is of no use to me, as its almost totally for wordpress users. Im not using wp @AhmedKhan – John Jackson Aug 23 '16 at 07:57
  • Well I know you need to read the last part in order to send email with mail() using gmail. Follow the following step then Go to My Account in Google then to Sign & Security and in the last of page turn on *less secure apps* – Ahmed Khan Aug 23 '16 at 08:01

1 Answers1

0

update you php code and check it will work

 if(isset($_POST['first-name'], $_POST['last-name'], $_POST['email'], $_POST['subject'], $_POST['message'])){
    $name = $_POST['first-name'];
    $email = $_POST['email'];
    $m = nl2br($_POST['message']);
    $message = '<p>Name => '.$name.' <br/> Email =>'.$email.'<br /> Message =>'.$m.'</p>';
    $to = "me@gmail.com";

    $subject = $_POST['subject'];
    $headers = "From: noreply@something.com\r\n"; // use \r\n
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";  // use \r\n
    $headers.= "X-Priority: 1\r\n";
    if( mail($to, $subject, $message, $headers) ){
        echo "success";
    } else {
        echo "The server failed to send the message. Please try again later.";
    }
  }
AmmyTech
  • 738
  • 4
  • 10
  • No luck, I'm getting a lot of "operation timed out" errors in my mail.log of my server – John Jackson Aug 23 '16 at 07:40
  • what's that error? and please update $headers.= "X-Priority: 1\r\n"; it was $header previously – AmmyTech Aug 23 '16 at 07:41
  • `Aug 23 03:43:55 MacBookAir postfix/smtp[6896]: connect to mta5.am0.yahoodns.net[66.196.118.35]:25: Operation timed out Aug 23 03:43:55 MacBookAir postfix/smtp[6896]: warning: 0C36E1E3DBA9: defer service failure` Aug 23 03:43:55 MacBookAir postfix/smtp[6896]: 0C36E1E3DBA9: to=, relay=none, delay=38342, delays=38192/0.05/150/0, dsn=4.4.1, status=deferred (connect to mta5.am0.yahoodns.net[66.196.118.35]:25: Operation timed out) – John Jackson Aug 23 '16 at 07:49
  • is me@yahoo.com valid email? please use proper email id – AmmyTech Aug 23 '16 at 07:51
  • yes its totally valid & I'm getting the same errors in addition to a new one claiming `No route to host` – John Jackson Aug 23 '16 at 08:00
  • Looks like the problem is your connection to the Yahoo mailservers or the configuration of your local mailserver and not your php script. – Frederick Behrends Aug 23 '16 at 09:20
  • check the updated question for more server logs @FrederickBehrends – John Jackson Aug 23 '16 at 16:46