1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Converting php variables to XML attachment

Discussion in 'PHP' started by k9webdesign, Jun 22, 2011.

  1. #1
    Hi all,

    I have made my own script to generate an xml using the following format:

    $xmlsm = "<srs>\n" .
    	   "	<affiliate_code>MyAffCode</affiliate_code>\n" .
    	   "	<batch_id>".$batchid."</batch_id>\n" .
    	   "	<sr>\n" .
    	   "	<data>\n" .
    Code (markup):
    etc etc which is used to then send this using mailto command.

    However, the company has requested that it be sent as an attachment, is there any way to send the output as attachment without having to physically create the xml file on disk?

    If not, what is the simplest solution here?

    Thanks very much.

    Andy
     
    k9webdesign, Jun 22, 2011 IP
  2. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I think this code can do the job:

    
        $filetype = “text/xml”;
    
        $filename = “whatever.xml”;
    
        $rand = md5(time());
    
        $mime_boundary = “==Multipart_Boundary_x{$rand}x”;
    
        $headers .= “\nMIME-Version: 1.0\n” .
    
                          “Content-Type: multipart/mixed;\n” .
    
                          ” boundary=\”{$mime_boundary}\”";
    
        $message = “This message contains an XML attachement\n\n” .
    
                           “-{$mime_boundary}\n” .
    
                           “Content-Type: text/plain; charset=\”iso-8859-1\n” .
    
                            “Content-Transfer-Encoding: 7bit\n\n” .
    
        $message .= “\n\n”;
    
       $data = chunk_split(base64_encode($xmlsm));
    
        $message .= “–{$mime_boundary}\n” .
    
                            “Content-Type: {$filetype};\n” .
    
                           ” name=\”{$filename}\”\n” .
    
                           “Content-Disposition: attachment;\n” .
    
                           ” filename=\”{$filename}\”\n” .
    
                           “Content-Transfer-Encoding: base64\n\n” .
    
                            $data . “\n\n” .
    
                            “-{$mime_boundary}-\n”;
    
       if(mail($to, $subject, $message, $headers)) {
              echo ”The email was sent.“;
        }
        else {
              echo ”There was an error sending the mail.“;
        }
    
    PHP:
     
    The Webby, Jun 22, 2011 IP
  3. k9webdesign

    k9webdesign Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi there, thanks for the response, after implementing it, I get the email but with no attachment, this is what I got:

    MIME-Version: 1.0
    Content-Type: multipart/mixed;
     boundary="==Multipart_Boundary_x105ca9252e8254315c3e737477e2e259x"
    
    This message contains the XML attachement for lead id 23201311172
    
    -==Multipart_Boundary_x105ca9252e8254315c3e737477e2e259x
    Content-Type: text/plain; charset="iso-8859-1
    Content-Transfer-Encoding: 7bit
    
    
    
    –==Multipart_Boundary_x105ca9252e8254315c3e737477e2e259x
    Content-Type: text/xml;
     name="23201311172.xml"
    Content-Disposition: attachment;
     filename="23201311172.xml"
    Content-Transfer-Encoding: base64
    
    PHNycz4KCTxhZmZpbGlhdGVfY29kZT5LOVdlYkRlc2lnbjwvYWZmaWxpYXRlX2NvZGU+Cgk8YmF0
    Y2hfaWQ+MjMyMDEzMTExNzI8L2JhdGNoX2lkPgoJPHNyPgoJPGRhdGE+Cgk8aWQ+MjMyMDEzMTEx
    NzI8L2lkPgoJPGNhdGVnb3J5PgoJPGlkPjwvaWQ+Cgk8bGFiZWw+Q29uc2VydmF0b3J5PC9sYWJl
    bD4KCTwvY2F0ZWdvcnk+Cgk8ZGF0ZT4yMi0wNi0yMDExPC9kYXRlPgoJPC9kYXRhPgoJPGN1c3Rv
    bWVyPgoJPHR5cGU+UGVyc29uYWw8L3R5cGU+Cgk8dGl0bGU+TXI8L3RpdGxlPgoJPG5hbWU+VGVz
    dDwvbmFtZT4KCTxmb3JuYW1lPkE8L2Zvcm5hbWU+Cgk8YWRyZXNzXzE+VGVzdGluZyAxMjM8L2Fk
    cmVzc18xPgoJPGFkcmVzc18yPkRlbGV0ZSBtZSBwbGVhc2U8L2FkcmVzc18yPgoJPHBvc3Rjb2Rl
    PkNXMzRUWTwvcG9zdGNvZGU+Cgk8dG93bj5UZ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lcmlhPgoJPGNyaXRlcmlhPgoJPGlkPjwvaWQ+
    Cgk8bGFiZWw+RG9vciBQb3NpdGlvbjwvbGFiZWw+Cgk8dmFsdWU+TGVmdDwvdmFsdWU+Cgk8L2Ny
    aXRlcmlhPgoJPGNyaXRlcmlhPgoJPGlkPjwvaWQ+Cgk8bGFiZWw+RGVzaWduPC9sYWJlbD4KCTx2
    YWx1ZT5MZWFuIFRvPC92YWx1ZT4KCTwvY3JpdGVyaWE+Cgk8Y3JpdGVyaWE+Cgk8aWQ+PC9pZD4K
    CTxsYWJlbD5DdXN0b21lciBOb3RlczwvbGFiZWw+Cgk8dmFsdWU+PCFbQ0RBVEFbXV0+PC92YWx1
    ZT4KCTwvY3JpdGVyaWE+Cgk8L2Zvcm0+Cgk8YWZmaWxpYXRlX2N1c3RvbV9kYXRhPgoJPC9hZmZp
    bGlhdGVfY3VzdG9tX2RhdGE+Cgk8L3NyPgoJPC9zcnM+
    
    
    -==Multipart_Boundary_x105ca9252e8254315c3e737477e2e259x-
    
    Code (markup):
    Not sure if I did something wrong when I integrated your code but it looked fine.

    The code I used was:

    $filetype = "text/xml";
    
        $filename = $leadid.".xml";
    
        $rand = md5(time());
    
        $mime_boundary = "==Multipart_Boundary_x{$rand}x";
    
        $headers .= "From: ME (Andy) testing@mydomain.com>\r\n" .
    	
    	"\nMIME-Version: 1.0\n" .
    
                          "Content-Type: multipart/mixed;\n" .
    
                          " boundary=\"{$mime_boundary}\"";
    
        $message = "This message contains the XML attachment for lead id ".$leadid."\n\n" .
    
                           "-{$mime_boundary}\n" .
    
                           "Content-Type: text/plain; charset=\"iso-8859-1\n" .
    
                            "Content-Transfer-Encoding: 7bit\n\n" .
    
        $message .= "\n\n";
    
       $data = chunk_split(base64_encode($xmlsm));
    
        $message .= "–{$mime_boundary}\n" .
    
                            "Content-Type: {$filetype};\n" .
    
                           " name=\"{$filename}\"\n" .
    
                           "Content-Disposition: attachment;\n" .
    
                           " filename=\"{$filename}\"\n" .
    
                           "Content-Transfer-Encoding: base64\n\n" .
    
                            $data . "\n\n" .
    
                            "-{$mime_boundary}-\n";
    
    $emailSubjectx = FilterCChars("New Lead Just In");
    
    $emailTox = "testing@mydomain.com";
    
    mail($emailTox, $emailSubjectx, $message, $headers); 
    Code (markup):
     
    k9webdesign, Jun 22, 2011 IP
  4. k9webdesign

    k9webdesign Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi again, that script you posted was very close so thanks very much for helping me on the right track.

    For anyone with the same problem please try this:

    <?php
    //define the receiver of the email
    $to = 'youraddress@example.com';
    //define the subject of the email
    $subject = 'Test email with attachment';
    //create a boundary string. It must be unique
    //so we use the MD5 algorithm to generate a random hash
    $random_hash = md5(date('r', time()));
    //define the headers we want passed. Note that they are separated with \r\n
    $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
    //add boundary string and mime type specification
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
    //read the atachment file contents into a string,
    //encode it with MIME base64,
    //and split it into smaller chunks
    $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
    //define the body of the message.
    ob_start(); //Turn on output buffering
    ?>
    --PHP-mixed-<?php echo $random_hash; ?> 
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
    
    --PHP-alt-<?php echo $random_hash; ?> 
    Content-Type: text/plain; charset="iso-8859-1"
    Content-Transfer-Encoding: 7bit
    
    Hello World!!!
    This is simple text email message.
    
    --PHP-alt-<?php echo $random_hash; ?> 
    Content-Type: text/html; charset="iso-8859-1"
    Content-Transfer-Encoding: 7bit
    
    <h2>Hello World!</h2>
    <p>This is something with <b>HTML</b> formatting.</p>
    
    --PHP-alt-<?php echo $random_hash; ?>--
    
    --PHP-mixed-<?php echo $random_hash; ?> 
    Content-Type: application/zip; name="attachment.zip" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment 
    
    <?php echo $attachment; ?>
    --PHP-mixed-<?php echo $random_hash; ?>--
    
    <?php
    //copy current buffer contents into $message variable and delete current output buffer
    $message = ob_get_clean();
    //send the email
    $mail_sent = @mail( $to, $subject, $message, $headers );
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
    echo $mail_sent ? "Mail sent" : "Mail failed";
    ?> 
    Code (markup):
     
    k9webdesign, Jun 22, 2011 IP
  5. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Glad you got it working..
    I hadn't tested the script, just wrote and posted it. Should have tested to see if it was working properly..
    if I remember, I had this script working in outlook express, I did this for a project long ago..

    Good luck with the project.
     
    The Webby, Jun 23, 2011 IP
  6. k9webdesign

    k9webdesign Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks Webby, one way or another you solved my problem. Thanks
     
    k9webdesign, Jun 23, 2011 IP