0

I'm attempting to run an email form, which takes text from the user's input forms and send them to my desired email (seen as $myemail in the php below).

<form method="post" name="contact_form" action="Contact.php">
        Your Name: <input type="text" required name="name">
        Email Address: <input type="text" required name="email">
        Message: <textarea required name="message"></textarea>
        <input type="submit" value="Submit">
</form>

<?php
$errors = '';
$myemail = 'enterDesiredEmail';

if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
        $errors .= "\n Error: all fields are required";
}

$name = null;
$email_address = null;
$message = null;

$name = $_POST["name"];
$email_address = $_POST["email"];
$message = $_POST["message"];

if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",$email_address)) {
        $errors .= "\n Error: Invalid email address";
}

if( empty($errors)) {
    $to = $myemail;
    $email_subject = "Contact form submission: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n ".
    "Email: $email_address\n Message \n $message";
    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email_address";
    mail($to,$email_subject,$email_body,$headers);
}
?>

I spat the above code out onto a webpage and uploaded the file to my server. Upon accessing the webpage online, filling in content to the form, and clicking sbmit, the page refreshed with the inputs and textarea clear as expected. When I checked the email I had set, no message was received. What could I do to correct this code so that the desired email address actually receives a message?

Many thanks.

H3ll0
  • 259
  • 1
  • 3
  • 16
  • 2
    The mail command requires that sendmail or some other mail server is set up. – Jason K Dec 30 '16 at 23:52
  • Try and add type to your header so the server know how to handle the script. By type I mean if it's an html email or just text. Secondly, instantiate a variable for the mail function. It returns true if mail was actually sent and false otherwise. Eg $send = mail(....): if($sent) do stuff.... hope this helps – Mueyiwa Moses Ikomi Dec 30 '16 at 23:54
  • 1
    You need to put `\r\n` between the header fields, not just `\n`. – Barmar Dec 31 '16 at 00:00

3 Answers3

1

You need to interpolate the actual $email variable inside the double quote from :
$headers = "From: $myemail\n"; to $headers = "From: ${myemail}"."\r\n";
If you Specify additional headers, like From, Cc, and Bcc.
The additional headers should be separated with a CRLF (\r\n).

This is an example from php doc:

 `<?php
  $to      = 'nobody@example.com';
  $subject = 'the subject';
  $message = 'hello';
  $headers = 'From: webmaster@example.com' . "\r\n" .
  'Reply-To: webmaster@example.com' . "\r\n" .
  'X-Mailer: PHP/' . phpversion();
  mail($to, $subject, $message, $headers);
 ?>

Happy coding!

kourouma_coder
  • 1,078
  • 2
  • 13
  • 24
  • 1
    There's no difference between `"From: $myemail\n"` and `"From: ${myemail}\n"`. You only need the curly braces for complex variables. – Barmar Dec 31 '16 at 00:01
1

You should try to use a library such as PHPMailer. Themail() function is sometimes not flexible enough to achieve what you need in most cases. Also the mail() function requires a local mail server. In addition, PHPMailer offers a lot of addons such as the ability to set up attachments or the ability to send HTML emails to your users. Using a library like this also means that you emails will be sent out almost all the time since it it tested and used by a lot of people.

You can find a tutorial for using PHPMailer here: https://www.sitepoint.com/sending-emails-php-phpmailer/

Example code taken from the website using PHPMailer:

<?php

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer;

//From email address and name
$mail->From = "from@yourdomain.com";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("recepient1@example.com", "Recepient Name");
$mail->addAddress("recepient1@example.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("reply@yourdomain.com", "Reply");

//CC and BCC
$mail->addCC("cc@example.com");
$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

?>
Brian Moreno
  • 977
  • 4
  • 11
  • 39
1

Some pointers that may well solve your issue without specifically telling you what the cause of this issue actually is:

1) Use Error Logging.

2) Check that you have a valid mail server setup on your server. As stated in comments by Jason K.

3) Use a mailing library such as PHPMailer. It sidesteps a huge amount of the headache.

4) Check your emails are valid with the correct code, principly using filter_var rather than obtuse regexes.

Also check emails are Sanitized (FILTER_SANITIZE_EMAIL) as well.

5) If not using or setting up a library such as PHPMailer or SwiftMailer then you need to check your mail headers are exactly correct.

BONUS
There seems to be some conflicting accounts of what to use for email line endings [including headers], but I would suggest PHP_EOL or \r\n (the same thing on Linux servers). \n is not suitable. As stated in comments by Barmar.

Good luck

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132