Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts

    Sending multipart/alternative emails, whats wrong with this code?

    I'm trying to send a multipart/alternative email with both the plain text version of the email and the html version in the same send to support older email clients as well as more modern ones.

    The code is sending fine however the received email isn't coming through correctly my servers AV is detecting a bad mime error which I believe is causing the problem but I can't see anything wrong with the code at all.
    In the final received email it appears to be breaking after the first text/plain content boundry and then just dumping out the rest of the email (including the other mixed headers) as plain text within the body.

    This is the code:
    PHP Code:
    $random_hash md5(date('r'time()));
            
    //add boundary string and mime type specification
            
    $headers "Return-path: <".$_SESSION['from'].">\r\n";
            
    $headers .= "Reply-to: <".$_SESSION['from'].">\r\n";
            
    $headers .= "From: <".$_SESSION['from'].">\r\n";
            
    $headers .= "X-Priority: 3\r\n";
            
    $headers .= "X-Mailer: Red Limes Sender\r\n";
            
    $headers .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n";
            
    $headers .= "MIME-Version: 1.0\r\n";
            
    $headers .= "\r\n\r\n";
            
    $message "--PHP-alt-$random_hash\r\n
            Content-Type: text/plain; charset=\"iso-8859-1\"\r\n
            Content-Transfer-Encoding: 7bit\r\n\r\n
            "
    .$_SESSION['plain']."\r\n\r\n
            --PHP-alt-$random_hash\r\n
            Content-Type: text/html; charset=\"iso-8859-1\"\r\n
            Content-Transfer-Encoding: 7bit\r\n\r\n
            <html><body>"
    .$_SESSION['html']."</body></html>\r\n\r\n
            --PHP-alt-$random_hash\r\n\r\n"
    ;

            
    //send the email
            
    mail($to$subject$message$headers); 
    This is the message details coming through including headers received by server:
    PHP Code:
    RFC822 Message body
    Return-Path: <www-data@furious-angels.com>
    X-Original-Tohidden@fcsoftware.co.uk
    Delivered
    -Tohidden@fcsoftware.co.uk
    Received
    from localhost (localhost [127.0.0.1])
    by mail.furious-angels.com (Postfixwith ESMTP id 923B4B2F29
    for <hidden@fcsoftware.co.uk>; Mon1 Feb 2010 12:33:44 +0000 (GMT)
    X-Quarantine-ID: <blQXIGAyHfe7>
    X-Virus-ScannedDebian amavisd-new at furious-angels.com
    X
    -Amavis-AlertBAD HEADER MIME errorerrorpart did not end with expected
    boundary
    Received
    from mail.furious-angels.com ([127.0.0.1])
    by localhost (beta.furious-angels.co.uk [127.0.0.1]) (amavisd-new, port 10024)
    with ESMTP id blQXIGAyHfe7 for <hidden@fcsoftware.co.uk>;
    Mon1 Feb 2010 12:33:34 +0000 (GMT)
    Receivedby mail.furious-angels.com (Postfixfrom userid 33)
    id 39F29B2F22Mon1 Feb 2010 12:33:34 +0000 (GMT)
    Tohidden@fcsoftware.co.uk
    Subject
    test template subject
    Reply
    -to: <hidden@furious-angels.com>
    From: <hidden@furious-angels.com>
    X-Priority3
    X
    -MailerRed Limes Sender
    Content
    -Typemultipart/alternativeboundary="PHP-alt-4be183c0dcc161fce6756cbfb9466c46"
    MIME-Version1.0
    Message
    -Id: <20100201123334.39F29B2F22@mail.furious-angels.com>
    DateMon1 Feb 2010 12:33:34 +0000 (GMT)





    --
    PHP-alt-4be183c0dcc161fce6756cbfb9466c46

    Content
    -Typetext/plaincharset="iso-8859-1"

    Content-Transfer-Encoding7bit


    asdasd
    asd
    asd
    asd


    --PHP-alt-4be183c0dcc161fce6756cbfb9466c46

    Content
    -Typetext/htmlcharset="iso-8859-1"

    Content-Transfer-Encoding7bit


    <html><body><p>this is a template test</p>
    <
    p> </p>
    <
    p>template test</p>
    <
    p> </p>
    <
    p>template test</p></body></html>


    --
    PHP-alt-4be183c0dcc161fce6756cbfb9466c46 
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    here's a multi part email function that I built a few years back, this may give you some ideas:

    PHP Code:
        // Format and Sends HTML & TXT Email at the same time
        
    function SendHTMLTxTEmail($From$FromName$To$ToName$Subject$Text$Html) {
            
    $OB="----=_OuterBoundary_000";
            
    $IB="----=_InnerBoundery_001";
            
    $Html=$Html?str_replace(array("\r","\n","\t"), ""$Html):preg_replace("/\n/","<br>",$Text
            or die(
    "neither text nor html part present.");
            
    $Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
            
    $From or die("sender address missing");
            
    $To or die("recipient address missing");

            
    $headers ="MIME-Version: 1.0\n"
            
    $headers.="From: ".$FromName." <".$From.">\n"
            
    // $headers.="To: ".$ToName." <".$To.">\n"; 
            
    $headers.="Reply-To: ".$FromName." <".$From.">\n"
            
    // $headers.="X-Priority: 1\n"; 
            // $headers.="X-MSMail-Priority: High\n"; 
            // $headers.="X-Mailer: My PHP Mailer\n"; 
            
    $headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";

            
    //Messages start with text/html alternatives in OB
            
    $Msg ="This is a multi-part message in MIME format.\n";
            
    $Msg.="\n--".$OB."\n";
            
    $Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";

            
    //plaintext section
            
    $Msg.="\n--".$IB."\n";
            
    $Msg.="Content-Type: text/plain;\n\tcharset=\"Windows-1252\"\n";
            
    $Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
            
    // plaintext goes here
            
    $Msg.=$Text."\n\n";

            
    // html section
            
    $Msg.="\n--".$IB."\n";
            
    $Msg.="Content-Type: text/html;\n\tcharset=\"Windows-1252\"\n";
            
    $Msg.="Content-Transfer-Encoding: base64\n\n";
            
    // html goes here 
            
    $Msg.=chunk_split(base64_encode($Html))."\n\n";

            
    // end of IB
            
    $Msg.="\n--".$IB."--\n";

            
    //message ends
            
    $Msg.="\n--".$OB."--\n";

            
    mail($To,$Subject,$Msg,$headers);
        } 

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Why not just use PHPMailer. It's much easier
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    maybe because some people like to know how things actually work instead of just using pre-built scripts.

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Of course. I know how to send multipart emails and the whole process behind doing so, I just also like tried and tested classes that save time. If the OP is using your function it's no different to using a class so the argument of wanting to know how it works is a bit of a nonsense argument since he/she can read the source of the class as well as the function
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    it's not nonsense at all, the class is bloated, where as my function is much more raw making it simpler for him to see what exactly is being done.

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Well that's your opinion. It doesn't make your function any better than the class
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    wow, you've got issues.

    I'm really not interested in arguing with you about it. the poster can use whatever he likes, end of discussion.




    edit: and for the record, I just checked, PHPMailer is roughly 3500 lines of code. my code is 46 lines. so being bloated is clearly not just an opinion is it? this is easier to go through, specially for someone just learning.
    Last edited by angst; 02-03-2010 at 03:48 AM.

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    ha that's hilarious. I express my opinion and you don't agree so I have issues Next I'll be told I'm crazy for indenting my code a certain way.

    I could pull your function apart and come up with reasons why it shouldn't be used.
    I wasn't saying your function wasn't something to use, I was merely stating that wheels don't need re-inventing every time someone creates a script

    Edit:
    And yes the main class is pretty big, theres also a lite version, which is still well documented
    Last edited by JAY6390; 02-03-2010 at 03:51 AM.
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #10
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    yes, but you continued to argue needlessly and I'm really not interested.

    and with lines like: "Well that's your opinion. It doesn't make your function any better than the class". right.. and I never said it did, only that it was simpler to read through. but it would appear that you have nothing better to know then argue about nonsense. so yes, I would say that you have some issues if this is what you like to spend your free time doing. I on the other hand have better things to do, so I'll leave it at that.

  • #11
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    whatever, I don't give a f***.

    Use the function hinch it's amazing
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #12
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Not to start the argument over but I would recommend using the pair modules as stated in the php manual for sending complex emails...

    Yes I'm the type of person who loves knowing how things work and avoid using others' programming when I don't have to. I recently built a multipart email form with an attachment that sends a pdf. I built it originally using php's mail() function but I discovered it only worked in Microsoft Outlook. When I tested with yahoo and gmail the body of the email ended up being blank and even though it said it had an attachment you couldn't open it. I then read on php's website to use the pear modules and it made life sooo much easy. It was a quick fixed once I got them installed.

    http://php.net/manual/en/function.mail.php

    It's in the notes below the examples. So that said for simple emails use php's mail function complex ones use pair... My two cents ;-)

  • #13
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    thanks for the entertaining topic guys.

    I refuse to use the phpmailer as on previous unit tests its considerably slower in through put and mails per second sending than just manually writing out using the mail() function thats why I've written this out by hand rather than using pre-built functions/classes etc I need to be a light weight and as little load causing on the server as possible.

    I worked out what was happening in the end anyway I'd wrapped $message in just single " " around start and end point the email client was interperating the in code carridge returns as \r\n so was forming the headers and email wrong I've dumped it all down into one line now and it works like a charm.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •