1

This is in code-igniter. To send mail I have written a function using php mail(). The code is:

function mailsend()
{
    $to="mymail@gmail.com";
    $subject="test";
    $message="<table>
                    <tr>
                        <td>Hello</td>
                        <td>World</td>
                    </tr>
                </table>";
    echo $message;

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

When I send mail from server side, I get the html code in my mail, not the real table. But the $messageshows the real table in browser. Is there any way in mail() to get the real table in the mail?

codelearner
  • 45
  • 1
  • 8

3 Answers3

2

You can send the html content in email via setting the header portion. Below is the sample code you can use.

<?php
$to = 'maryjane@email.com';
$subject = 'Marriage Proposal';
$from = 'peterparker@email.com';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';

// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}
?>
piyush
  • 655
  • 4
  • 11
1

In order to send html content in your email with mail function you will need to add header options to it, like so:

$headers =  'From: test@yourdomain.com' . "\r\n" .
                    'Reply-To: test@yourdomain.com' . "\r\n" .
                    "Content-type:text/html;charset=UTF-8" . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();

mail($sendTo, $subject, $message, $headers);
Igor Ilic
  • 1,370
  • 1
  • 11
  • 21
1

Add an header to your mail function like this

$headers =  'From: test@yourdomain.com' . "\r\n" .
                    'Reply-To: hello@example.com' . "\r\n" .
                    "Content-type:text/html;charset=UTF-8" . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();
Omolewa Stephen
  • 444
  • 3
  • 19