-1

I tried sending HTML mails through PHP but nothing is working. I tried two options.

One with file_get_contents:

<?php
$to = 'teas@gmail.com';
$subject = 'Marriage Proposal';
$from = 'support@blabla.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();

$message = file_get_contents("email_template.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.';
}
?>

And one with HTML in a PHP string:

<?php
$to = 'anthony@gmail.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.';
}
?>

The response for both functions is:

Unable to send email. Please try again. Unable to send email. Please try again.

Can anyone tell me whats wrong?

TFennis
  • 1,393
  • 1
  • 14
  • 23
DrNawaf
  • 1
  • 1
  • Did you set your SMTP server correctly in `php.ini`? – TFennis Feb 22 '17 at 11:58
  • everything is working fine.i have sent a total of 10k emails till now in plain text.but when i implement HTML it gets error. if those plain emails are getting delivered then it mean that everything is fine.idk still – DrNawaf Feb 22 '17 at 12:00
  • To clarify the title, this is using the mail() function in php, not "PHPMailer". Big difference. – mickmackusa Feb 22 '17 at 12:07
  • Additional header lines provided here: http://stackoverflow.com/questions/566182/complete-mail-header these may or may not be helpful, just worth a read/try. – mickmackusa Feb 22 '17 at 12:10
  • If you cannot resolve your issue, I highly, highly recommend using PHPMailer. – mickmackusa Feb 22 '17 at 12:18
  • @DrNawaf the second email is working fine on my side, I just copied and paste your code and change my email address. the email arrived as expected with headings styled – Masivuye Cokile Feb 22 '17 at 13:25
  • can you post the contents of email_template.html – Masivuye Cokile Feb 22 '17 at 13:26

1 Answers1

-2

Just enable the HTML in php mailer

$mail->isHTML(true);

For refrence please see here the example in github

Pranav MS
  • 2,235
  • 2
  • 23
  • 50