2

I tried questions already answered over here (this, this, this, and others...), but nothing get worked... i have this function to send an email:

function send_email($para, $assunto, $msg){
    $to      = $para;
    $subject = $assunto;

    $message = '<html><body>';
    $message .= str_replace("\\r\\n","<br/>",$msg);
    $message .= '</body></html>';

    $headers = 'From: email@realemail.com' . "\r\n";
    $headers .= 'Reply-To: email@realemail.com' . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";

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

And i'm calling this with something like that:

$var = "<p><strong>...blablabla...</strong></p>";

send_email("email@email", "lalala", $var);

I can send the email, but it ins't html formatted (and it contains the html tags, except for HTML and BODY). What am i doing wrong?

EDIT 1: The email is displayed like this (exactly the same content sent, except for HTML and BODY TAG):

BEGIN
<p><strong>...blablabla</strong></p>
END

When i get the full email (like this tutorial), shows up that the email was sent with TAGS html, body, etc... A piece of the email:

Delivered-To: myemail@gmail.com
...
To: myemail@gmail.com
Subject: aaaaaaaaaaaaaaaaaaaaa
From: sender@email.com
Reply-To: sender@email.com
X-Mailer: PHP/5.6.19
Content-type: text/html; charset=iso-8859-1
MIME-Version: 1.0
...
Date: Tue, 29 Nov 2016 15:51:48 -0200
...
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - svr1.ravehost.com.br
X-AntiAbuse: Original Domain - gmail.com
X-AntiAbuse: Originator/Caller UID/GID - [99 497] / [47 12]
X-AntiAbuse: Sender Address Domain - svr1.ravehost.com.br

<html><body>...blablabla...</body></html>
Community
  • 1
  • 1
TroniPM
  • 329
  • 3
  • 13
  • could you eventually also copy/paste here the resulting e-mail message with headers and all? – Dragos Nov 29 '16 at 19:03
  • See the edit1 @Dragos. – TroniPM Nov 29 '16 at 21:35
  • Other then the headers being in incorrect order your code looks fine. Maybe there is a problem on the client side, which mail client are you using? – erikvimz Nov 29 '16 at 21:46
  • 2
    Try changing `Content-type` to `Content-Type` (upper case the T). – Ross Nov 29 '16 at 21:49
  • @PMateus E-mail, proper MIME encoding, and multi-part messages are an utter nightmare and you would be crazy to have to figure out all the quirks yourself. :-) Use an existing mail library that does this for you, such as PHPMailer or similar. – Brad Nov 29 '16 at 21:51
  • I tried this on my local PHP/windows env. and see the headers/body as: `Date: Tue, 29 Nov 2016 14:22:01 -0800 Subject: lalala To: From: X-Mailer: PHP/5.6.10 Content-Type: text/html; charset="iso-8859-1" MIME-Version: 1.0

    Prepared do an dissuade be.... 

    Society excited by cottage private an it esteems. ... 

    Whole every miles as tiled at seven or. . 

    `
    – Sᴀᴍ Onᴇᴌᴀ Nov 29 '16 at 22:32
  • @Ross still not working... – TroniPM Nov 30 '16 at 00:47
  • @Brad, yeah... i don't wanna use this lib, but until now i couldn't make it run properly, so i'm gonna try. – TroniPM Nov 30 '16 at 00:59
  • Try changing the order of the headers. First MIME-Version and Content-type and only after that the From, To, Reply-To and so on – Dragos Nov 30 '16 at 16:53

1 Answers1

-2

Not sure if this is an exact solution, but this solved html email problems I was having with Microsoft Outlook.

Replace all of the \r\n in the headers with PHP_EOL. I did this when I was having issues with sending html emails in PHP, and it seemed to solve my particular problems.

aCarella
  • 2,369
  • 11
  • 52
  • 85
  • 2
    `PHP_EOL` changes its value depending on the platform you're using. If you run PHP on Windows, you'll get `\r\n`. If you run it on Linux, you'll get `\n`. – Brad Nov 29 '16 at 21:51