dada
dada

Reputation: 25

unable to get line breaks when sending mail via php mail() function

Here is my code:

        $body = "Name:{$name}".PHP_EOL;
    $body .= "Phone:".$phone.PHP_EOL;
    $body .= "Email:".$email."\n\n";
    $body .= "Comments:".$comments."\n\n";
    $headers = 'From: MAILER <'.$emailTo.'>' . "\r\n" . 'Reply-To: '. $email."\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8"; 
    mail($emailTo, "New message", $body, $headers);

as you can see I've tried to use, \r\n\, \n\n, PHP_EOL etc... the only way it works is when i removing my "content-type" header. anyone have an idea for why is it like that?!

Upvotes: 1

Views: 3846

Answers (2)

Doon
Doon

Reputation: 20232

Since you are sending HTML email you need to use <br> to set the line breaks. Well that or wrap everything in <pre></pre> tags to honor the \n. IF you don't need HTML in the output just set your content to 'text/plain', and the line breaks would show up.

Since it isn't multi-part try adding MIME-Version: 1.0 to the headers. That might help with the content type.

$headers .= "MIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8";

Upvotes: 3

sourabh kasliwal
sourabh kasliwal

Reputation: 977

use as explain in example

$toemail=$emailid;
                $subject="You have registered successfully , please activate your account.";
                $msgbody.='<table width="100%" align="center" cellpadding="5" cellspacing="0" border="0">';
                $msgbody.='<tr><td>Admin Approved your profile and the details are : </td></tr>';
                $msgbody.='<tr><td>Username : '.$username.'</td></tr>';
                $msgbody.='<tr><td>Email : '.$emailid.'</td></tr>';
                $msgbody.='<tr><td>Password : '.$_SESSION['fan_user_password'].'</td></tr>';
                $msgbody.='<tr><td><a href="'.BASE_URL.'activate/fan/'.$lastid.'/'.$activation_code.'">';
                $msgbody.='Click here </a>to activate your account.</td></tr>';
                $msgbody.='</table>';
                mail($toemail,$subject,$msgbody,$from='');

Upvotes: 1

Related Questions