2

I was trying to send mail in PHP with mail() function. The email sent successfull and it's exist in the inbox. The problem is the headers printed in the email header. I've tried the various code for the headers. 1.The first goes like this:

$headers = "From: My Example Email".'\r\n'.
"MIME-Version: 1.0".'\r\n'.
"Content-Type: text/html; charset=ISO-8859-1".'\r\n'.
'X-Mailer: PHP/' . phpversion();

Result: My Example EmailrnMIME-Version

2.Second headers code:

$headers = "From: My Example Email"."\r\n".
"MIME-Version: 1.0"."r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
'X-Mailer: PHP/' . phpversion();

Result: The email didn't sent

3.Third headers code:

$headers = "From: My Example Email".'"\r\n"'.
"MIME-Version: 1.0".'"\r\n"'.
"Content-Type: text/html; charset=ISO-8859-1".'"\r\n"'.
'X-Mailer: PHP/' . phpversion();

Result: My Example Emailrn

I use PHP 5.4.19. Any answer will really help.

UPDATE

This is my whole code:

class User{
 function callname(){
     $user = $_SESSION['id'];
     $query = ("SELECT * FROM user WHERE user.id='$user'");
     while ($result=mysql_fetch_array($query)){
          echo ($result['username']);}}}

$user = new User;

if($_SERVER["REQUEST_METHOD"] == "POST"){
   $username = mysql_real_escape_string(trim($_POST['username']));
   $check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
 if ($check==TRUE){
   $name = $user->callname();
   $to = "myemail@domain.com";
   $subject = "Example Subject";
   $headers = "From: My Example Email".'"\r\n"'.
   "MIME-Version: 1.0".'"\r\n"'.
   "Content-Type: text/html; charset=ISO-8859-1".'"\r\n"'.
   'X-Mailer: PHP/' . phpversion();
   $message = "Hai $name, this is the new message.";

        mail($to, $subject, $message, $headers);
} else {
?>
    <script type="text/javascript">
        alert("Sorry, username not exist !");
    </script>
    <?php }}

New UPDATE:

After a long trial and help with everyone here, finally found the solution. But maybe it's unusual.

$headers = 'From: My Example Email'.'""'.
'MIME-Version: 1.0'.'""'.
'Content-Type: text/html; charset=ISO-8859-1'.'""'.
'X-Mailer: PHP/' . phpversion();

but I'm not understand yet. Some literature said that every part shoul be glue by "\r\n", but that's not work in my code. Thanks for every help. Thanks a lot. Thats all really helpful.

merli
  • 25
  • 9

4 Answers4

2

You could try my function for php mailing. This function will generete RFC compatible body and header part for your email.

function buildMime($msg){
$num = md5(time());
$num = "_001_".$num."_";

$headers = "From: SenderName<senderEmail@adress.com>\n";
$headers .= "Return-Path: <senderEmail@adress.com>\n";
$headers .= "Reply-To: <senderEmail@adress.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative;\n";
$headers .= "   boundary=\"".$num."\"\n";
$headers .= "X-Mailer: PHP v".phpversion()."\n";

$body = "This is a multi-part message in MIME format.\n\n";

$body1 = "--".$num."\n";
$body1 .= "Content-Type: text/plain; charset=utf-8\n";
$body1 .= "Content-Transfer-Encoding: 8bit\n\n";

$body1 .= trim(strip_tags($msg))."\n";
$body1 .= "\n";

$body1 .= "--".$num."\n";
$body1 .= "Content-Type: text/html; charset=utf-8\n";
$body1 .= "Content-Transfer-Encoding: 8bit\n\n";

$body1 .= $msg;
$body1 .= "\n";



$bodyx = "--".$num."--\n";
return array('body' => $body.$body1.$bodyx, 'headers' => $headers);
}
$mime = buildMime("<h1>Hello</h1><p>this is my firs test message</p>");
mail('whereToSend@email.com', 'Your subject', $mime[body], $mime[headers]);
?>
Adam Fischer
  • 1,075
  • 11
  • 23
  • thanks man, but so how to use that? Can you give me an example? Sorry 'cause I'm a newbie – merli Mar 28 '14 at 13:30
0

You are not properly encapsulating the quotes.

The right way to do...

$headers = 'From: My Example Email'."\r\n".
    'MIME-Version: 1.0'."\r\n".
    'Content-Type: text/html; charset=ISO-8859-1'."\r\n".
    'X-Mailer: PHP/' . phpversion();

EDITED CODE

<?php
class User{
    function callname(){
        $user = $_SESSION['id'];
        $query = ("SELECT * FROM user WHERE user.id='$user'");
        while ($result=mysql_fetch_array($query)){
            return $result['username'];}}}

$user = new User;

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = mysql_real_escape_string(trim($_POST['username']));
    $check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
    if ($check==TRUE){
        $name = $user->callname();
        $to = "myemail@domain.com";
        $subject = "Example Subject";
        $headers = "From: My Example Email"."\r\n".
            "MIME-Version: 1.0"."\r\n".
            "Content-Type: text/html; charset=ISO-8859-1"."\r\n".
            "X-Mailer: PHP/" . phpversion();
        $message = "Hai $name, this is the new message.";

        mail($to, $subject, $message, $headers);
    } else {
        ?>
        <script type="text/javascript">
            alert("Sorry, username not exist !");
        </script>
    <?php }}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • Not work man. The email don't wanna send if I use double qoute `"\r\n"`. – merli Mar 28 '14 at 12:46
  • The email don't wanna send man. My update code resulting the sent email and header From: My Example Emailrn. So confuse with this. Thanks for your help. – merli Apr 01 '14 at 04:41
0
$headers = "From: My Example Email"."\r\n".
"MIME-Version: 1.0"."\r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
'X-Mailer: PHP/' . phpversion();

Try this

0

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";


$headers .= 'From: ' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);

rajppari
  • 54
  • 6