HTML emails using PHP
  • Hello. I'm kinda have to develop my sister's website and currently she asks me to send a message to registered emails (from database). But the problem is that I can't manage to send HTML code to my email...

    First I tried the PHP mail() function but it didn't really work out. Then I downloaded PHPMailer v5.1 and tried using it. Same.

    The GMAIL gets everything it's supposed to get but it doesn't display as it should.

    Here is the code:

    // s[] - subject array
    // m[] - messages array
    // keys of each array mean language (like 'en')
    $mail = new PHPMailer();
    $mail->AddReplyTo("info@web.com","web.com");
    $mail->SetFrom('info@web.com', 'web.com');
    $mail->IsHTML(true);

    foreach($m as $key => $value){
    $mail->Subject = $s[$key];
    $result = mysql_query("SELECT id, email FROM emails WHERE status = '2' AND language = '{$key}'", $con);
    while($user = mysql_fetch_array($result, MYSQL_NUM)){
    $l = '...';
    $mail->AddAddress($user[1], '');
    $mail->MsgHTML($l);
    $mail->Send();
    }
    }


    The headers GMAIL gets say that content type is multipart/alternative and then 2 other - plain and html. It's normal so far.
    When I open the mail, it prints the HTML version but with tags/attributes removed.


    Is there something I should send in the header to get HTML fully working?
  • The IsHTML should do the trick. Have you tried sending from localhost using PHPMailer to see if gmail is messing with the html?
    Hello World!
  • May I ask what's the point of trying sending from localhost? I've got a form on the server I copy the HTML code to and then it does what the script does... (the one I posted) It surely does mess with the HTML. It gets what is has to get but when it has to echo it, it does it after some kind of filter.

    I would understand why it would disable styles but I hoped there is anything to add to the header (if it was included in protocol).

    P.S. Is there any chance to achieve a different result if used SMTP or something? Maybe it filters PHPMailer as a potential danger or something... I'll try this out after waking up.
  • The point of sending from localhost would be it's using a different server. I think I had a retarded moment and thought you were sending from gmail.

    I try using SMTP just for kicks. PHPMailer is a pretty good class and I can't say I have run into trouble using it in the past. Very odd it's not passing the correct content type.

    Have you tried sending the body using $mail->Body = $l; ?
    Hello World!
  • Actually MsgHTML does everything it should do. Here are 2 lines from it:

    ...
    $this->IsHTML(true);
    $this->Body = $message;
    ...

    so even my call to IsHTML() is kinda useless.


    --------------------- edited -----------------

    OK, now I'm pretty sure that GMAIL doesn't want to show the HTMLed emails and that's pretty much all about that. Although, I accidentally noticed that when I submit <a> tags, my PHP script receives it width " added after attribute= and \ added before " I added. So... it looks like this:
    I copy: <a href="http://web.com/" target="_blank">www.web.com</a>
    PHP receives: <a href="\"http://web.com/"\" target="\"_blank"\">www.web.com</a>

    If I copy without " - it adds it and everything works just fine. And that's before sending. Like this:
    $m['...'] = $_POST['msg_...'];
    foreach($m as $key=>$value)
    echo $value;

    WTF?! Does it mean that the server, the website is hosted in, uses some sucky security scripts?
  • Looks like magic_quotes are turned on. Not sure why the extra " though.
    Hello World!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Poll

No poll attached to this discussion.

In this Discussion