Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Sending HTML email using PHP [header problems]

3stripe3stripe ✭✭
edited May 2006 in Vanilla 1.0 Help
Hi folks,

I'm having problems generating an html email... something's not right in the code below but I can't figure out what... when I receive the email in gmail it's all wrong.

Any ideas as usual on fixing my php idiocy are much appreciated

Cheers!

James

//Get email address of a friend, and sends them an html email // Get the email address from flash $Email1 = $HTTP_POST_VARS['email1'] ; // Email starts here $ToEmail = $Email1; $ToName = $Email1; $Yourname = "Bob Smith"; $Youremail = "email@email.com"; $ToSubject = "EMAIL SUBJECT HERE"; $html_content = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Document</title> </head> <h1>Test email only</h1> <p>Blah blah blah</p> </body> </html>'; // 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"; $Headers .= 'From: '.$Yourname.' <".$Youremail.">' . "\r\n"; $Message = $html_content; // Send email mail($ToName." <".$ToEmail.">",$ToSubject, $Message, $Headers); echo "status=ok"; ?>

Comments

  • Options
    3stripe3stripe ✭✭
    PS. the email1 variable is coming from a Flash file, but that's working fine
  • Options
    NickENickE New
    edited May 2006
    To qoute php.net:
    As such, the to parameter should not be an address in the form of "Something <someone@example.com>". The mail command may not parse this properly while talking with the MTA.
    But I don't think that's the problem... You could also try replacing your Windows newlines with instances of \n:
    Note: If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with RFC 2822.
    My only other thoughts are that the newline before the doctype is messing it up, or the doctype itself.
  • Options
    3stripe3stripe ✭✭
    Hey thanks for the pointers (too many late nights starting to scramble my brain). I'm going to try both of those suggestions.... if Something <someone@example.com> is not correct, what is then, just the email address? Cheers SirNot.
  • Options
    NickENickE New
    edited May 2006
    what is then, just the email address?

    Well actually, now that I read over that section again, it seems only to apply to Windows servers, so if you know you're running a *nix server than you shouldn't need to worry about it. However, if not, then I'd assume you'd want to use just the mail address.
  • Options
    3stripe3stripe ✭✭
    edited May 2006
    Ok, yeah, user@example.com is better than having them not matching, that makes sense.... let's see what happens now... Oops, beat me to it (still reckon that a "see who's typing" extension would be good)
  • Options
    3stripe3stripe ✭✭
    edited May 2006
    Ok... the html formatting is working... thanks dude :)

    One problem left - something in the from email address is not right - it's appearing in the email like this:

    From: Joe Bloggs <".$Youremail."@z1lnx007.web.vi.net>

    So somethings not quite right here... can't work out where it is picking up the host domain from either!
  • Options
    You seem to swap which kind of ' or " you're using half way through the line...?
    $Headers .= 'From: '.$Yourname.' <".$Youremail.">' . "\r\n";
    Perhaps either use
    $Headers .= "From: $Yourname <$YourEmail>\r\n";
    or
    $Headers .= 'From: '.$Yourname.'<'.$Youremail.'>'."\r\n";

    (i think)
  • Options
    3stripe3stripe ✭✭
    Hot dog!

    The second one worked (I've switched to just "\n"s for now)

    Cheers folks. Now onto the fun part of prettifying the html...
  • Options
    3stripe3stripe ✭✭
    Hmm so how can I include a text-only version as well, for the less well equipped email readers..... ?

    Not sure about this one - having just tested on my pipex webmail, I don't see anything at all in this email which isn't good!
  • Options
    3stripe3stripe ✭✭
    edited May 2006
    Hmm, multi-part email php looks a bit complex for this time o' night...
  • Options
    haha. I was always under the impression text only emails had to be made seperately and then mailed to people depending whether they wanted html or text? I dont think you can send an email which picks whether it's to be html or text when it's get there? I could be wrong though.

    But yeah i'm definitely hitting that night time barrier.
  • Options
    you can send them both in one email. that's the mhtml standard. the client displays the html part if it supports html, otherwise it displays the plaintext part.

    some example code that i just picked up off the net:
    $boundary = "nextPart"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "From: Me <sales@mysite.com>\r\n"; $headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n"; //text version $headers .= "\n--$boundary\n"; // beginning \n added to separate previous content $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"; $headers .= "This is the plain version"; //html version $headers .= "\n--$boundary\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "This is the <b>HTML</b> version"; mail("me@mymail@mac.com", "An HTML Message", "", $headers);
  • Options
    3stripe3stripe ✭✭
    edited May 2006
    Cheers ithcy, that's a cleaner chunk of code than I managed to find anywhere :) And it works, as well. Super.
  • Options
    *Learns something new every day*
  • Options
    3stripe3stripe ✭✭
    Got it all sorted - live at http://tinyurl.com/huv57 (the game sends out an email to a friend after you've played it)
This discussion has been closed.