0

I am having trouble with sending emails through PHP.

So I have a form which submits to a PHP script which sends an email:

<?php

    $to  = "myemail@email.com"; 
    $subject = "[Contact Form]";
    $name = $_POST["name"];
    $contactNumber = $_POST["contactNumber"];
    $email = $_POST["email"] ;
    $message = $_POST["message"];
    $body    = "Someone has sent a new message from the contact form. \n \n  Message from: " . $name . "\n Contact Number: ". $contactNumber ."\n Email: ". $email ."\n \n Message: ". $message; 


    if (mail($to, $subject, $body)) {
        echo ("<p>Email successfully sent!</p>");
    } else {
        echo ("<p>Email delivery failed…</p>");
    }


?>

And the email is sent fine when for example the message is one line such as:

"Hi there how is it going?"

And fine if it is multiple lines such as

"Hi there

how is it going?"

But when I try and type a message with a comma such as

Hello there,

how is it going?

It fails?

Is there a way I can just treat the whole thing as a string possibly? Would this also fail on any other characters or is this issue just because of the way I am writing the PHP script?

This might be an obvious fix but I am new to PHP so apologies! I have tried looking around for an answer but nothing seems to fix what I am looking for.

Thanks for any help!

Community
  • 1
  • 1
user3195250
  • 127
  • 2
  • 13
  • 1
    assign your POST array for the message to a variable first. same for all your other POST arrays, it's easier. – Funk Forty Niner Jul 28 '16 at 15:44
  • 1
    Email failing because of a comma in the string? Pretty mysterious. You'll have to do some more digging for someone to be able to help you. First try would be to wrap the string in `utf8_encode` to avoid any weird characters. – Dan Jul 28 '16 at 15:49
  • Try to use htmlentities: http://php.net/manual/de/function.htmlentities.php – Fabio Widmer Jul 28 '16 at 15:50
  • Try using `
    ` in the email for a line break instead of the `\n`. You will have to specify that you are using `HTML` in your message. [Checkout Example 3](http://www.w3schools.com/php/func_mail_mail.asp)
    – Gary Holiday Jul 28 '16 at 15:50

1 Answers1

0

Try using headers in your mail function, like this:

$headers  = 'MIME-Version: 1.0\r\n';
$headers .= 'Content-type: text/html; charset=UTF-8\r\n';
mail($to, $subject, $body, $headers) 
rboeg
  • 51
  • 6