#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    65
    Rep Power
    10

    Exclamation Create eml file with attachment


    Hi

    I am trying to create an eml file, but I am having difficulty constructing it correctly.

    I am using the following code which creates an .eml file but the .pdf attachment cannot be opened in the eml file
    and my body comes out as text and is not rendered as html.

    FYI the pdf file (application.pdf) is created prior to this code on the fly and is in the current directory

    PHP Code:
    //get the body using a template
    $Body file_get_contents('./templates/app-email.php');
     
    //Substitute Variable for placeholders in the email templates
    $VariableArray = array('{ContractName}','{ContractSurname}','{idClient}');
    $ValuesArray = array($ContractName,$ContractSurname$idClient);
                
    $Body str_replace($VariableArray,$ValuesArray,$Body);


    $EmailBuild='From: system@pinnacleapps.co.za
    Date:'
    .$EmailDate.'
    MIME-Version: 1.0
    To:'
    .$Email.'
    CC:'
    .$CC.'
    BCC:'
    .$BCC.'
    Subject:'
    .$Subject.'
    Content-Type: multipart/mixed;
        boundary="b1_10b9d4050312bdab3fa541e657aad966"

    This is a message with multiple parts in MIME format.
    --b1_10b9d4050312bdab3fa541e657aad966

    Content-Type: text/html'
    .$Body.'

    --b1_10b9d4050312bdab3fa541e657aad966
    Content-Type: application/pdf;name="application.pdf"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;filename="application.pdf";'
    ;

    $Handle=fopen('email.eml','w');
    fwrite($Handle$EmailBuild);
    fclose($Handle);


    header('Content-disposition: attachment; filename=email.eml');
    header('Content-type: message/rfc822');
    readfile('email.eml'); 
    I would really appreciate the assistance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    65
    Rep Power
    10
    Originally Posted by fabzster
    Hi

    I am trying to create an eml file, but I am having difficulty constructing it correctly.

    I am using the following code which creates an .eml file but the .pdf attachment cannot be opened in the eml file
    and my body comes out as text and is not rendered as html.

    FYI the pdf file (application.pdf) is created prior to this code on the fly and is in the current directory

    PHP Code:
    //get the body using a template
    $Body file_get_contents('./templates/app-email.php');
     
    //Substitute Variable for placeholders in the email templates
    $VariableArray = array('{ContractName}','{ContractSurname}','{idClient}');
    $ValuesArray = array($ContractName,$ContractSurname$idClient);
                
    $Body str_replace($VariableArray,$ValuesArray,$Body);


    $EmailBuild='From: system@pinnacleapps.co.za
    Date:'
    .$EmailDate.'
    MIME-Version: 1.0
    To:'
    .$Email.'
    CC:'
    .$CC.'
    BCC:'
    .$BCC.'
    Subject:'
    .$Subject.'
    Content-Type: multipart/mixed;
        boundary="b1_10b9d4050312bdab3fa541e657aad966"

    This is a message with multiple parts in MIME format.
    --b1_10b9d4050312bdab3fa541e657aad966

    Content-Type: text/html'
    .$Body.'

    --b1_10b9d4050312bdab3fa541e657aad966
    Content-Type: application/pdf;name="application.pdf"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;filename="application.pdf";'
    ;

    $Handle=fopen('email.eml','w');
    fwrite($Handle$EmailBuild);
    fclose($Handle);


    header('Content-disposition: attachment; filename=email.eml');
    header('Content-type: message/rfc822');
    readfile('email.eml'); 
    I would really appreciate the assistance
    I have figured out how to embed the pdf attachments, but need assistance for the body, see below updated code
    Currently the body is included and rendered as html, but the top portion of the body does not display when opening the
    eml file, the html formatting also is not 100%

    PHP Code:
    //get the body using a template
    $Body file_get_contents('./templates/app-email.php');

    //Substitute Variable for placeholders in the email templates
    $VariableArray = array('{ContractName}','{ContractSurname}','{idClient}');
    $ValuesArray = array($ContractName,$ContractSurname$idClient);
                
    $Body str_replace($VariableArray,$ValuesArray,$Body);


    $Application chunk_split(base64_encode(file_get_contents('application.pdf')));
    $Disclosure chunk_split(base64_encode(file_get_contents('disclosure.pdf')));
    $Termschunk_split(base64_encode(file_get_contents('terms.pdf')));

    $EmailBuild='From: system@pinnacleapps.co.za
    Date:'
    .$EmailDate.'
    MIME-Version: 1.0
    To:'
    .$Email.'
    CC:'
    .$CC.'
    BCC:'
    .$BCC.'
    Subject:'
    .$Subject.'
    Content-Type: multipart/mixed;
        boundary="b1_10b9d4050312bdab3fa541e657aad966"

    This is a message with multiple parts in MIME format.

    --b1_10b9d4050312bdab3fa541e657aad966
    Content-Type: text/html;charset=utf-8;'
    .$Body.'



    --b1_10b9d4050312bdab3fa541e657aad966
    Content-Type: application/pdf;name="application.pdf"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;filename="application.pdf";

    '
    .$Application.'


    --b1_10b9d4050312bdab3fa541e657aad966
    Content-Type: application/pdf;name="terms.pdf"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;filename="terms.pdf";

    '
    .$Terms.'


    --b1_10b9d4050312bdab3fa541e657aad966
    Content-Type: application/pdf;name="disclosure.pdf"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;filename="disclosure.pdf";

    '
    .$Disclosure.'


    '
    ;
    $Handle=fopen('email.eml','w');
    fwrite($Handle$EmailBuild);
    fclose($Handle);

    header('Content-disposition: attachment; filename=email.eml');
    header('Content-type: message/rfc822');
    readfile('email.eml'); 
    Last edited by fabzster; March 30th, 2017 at 04:02 AM.
  4. #3
  5. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,324
    Rep Power
    9645
    You have to embed the PDF into the eml. And you're not constructing the eml correctly, suggesting you really don't know what you're doing in the first place.

    PHP Code:
    $EmailBuild='From: system@pinnacleapps.co.za 
    Date:'
    .$EmailDate.
    MIME-Version: 1.0 
    To:'
    .$Email.
    CC:'
    .$CC.
    BCC:'
    .$BCC.
    Subject:'
    .$Subject.
    Content-Type: multipart/mixed; 
        boundary="b1_10b9d4050312bdab3fa541e657aad966" 

    This is a message with multiple parts in MIME format. 
    --b1_10b9d4050312bdab3fa541e657aad966 

    Content-Type: text/html'
    .$Body.

    --b1_10b9d4050312bdab3fa541e657aad966 
    Content-Type: application/pdf;name="application.pdf" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment;filename="application.pdf";

    '
    ;
    $EmailBuild .= wordwrap(base64_encode(/* contents of the PDF */));
    $EmailBuild .= "
    --b1_10b9d4050312bdab3fa541e657aad966--"

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    65
    Rep Power
    10
    Thanks,

    I have however figured out how to add the attachments, but now have only one issue (see above edited code).

    The Body of the email is in a template and is rendered now when opening the eml file, the problem is that the first 46 lines are missing and the formatting of the html body is not 100% correct because of the first lines being omitted
    Last edited by fabzster; March 30th, 2017 at 04:14 AM.
  8. #5
  9. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,324
    Rep Power
    9645
    If $Body is just the contents of the message and doesn't have any headers then
    PHP Code:
    '--b1_10b9d4050312bdab3fa541e657aad966 
    Content-Type: text/html;charset=utf-8;'
    .$Body
    is wrong: you need line breaks between the headers and the body, just like you did with the attachments.

    You also need to close the multipart message using another boundary with a trailing "--"
    PHP Code:
    .$Disclosure.

    --b1_10b9d4050312bdab3fa541e657aad966--
    '

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    65
    Rep Power
    10
    My apologies for being a pain, I have never tried generating an eml file and have very limited understanding on this.

    below is what's omitted, the rest is visible in the body of the generated .eml file. I can't figure out why it just leaves the first 46 lines out

    Code:
    <!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>
        <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
        <title></title>
        <meta name='viewport' content='width=device-width, initial-scale=1.0'/>
    </head>
        <body style='margin: 0; padding: 0; color:#313131;'>
            <table align='center' border='1' cellpadding='0' cellspacing='0' width='800' style='border-collapse: collapse; font-family:Verdana, Geneva, sans-serif;'>
                <tr>
                    <td align='center' bgcolor='#FFCC00'>
                    </td>
                </tr>
                <tr>
                <td bgcolor='#FFFFFF'>
                <table style='text-align:justify;' align='center' border='0' cellpadding='0' cellspacing='0' width='80%'>
                    <tr>
                        <td style='padding-top:10px; padding-bottom:10px; font-size:12px;'>
                            <font face='verdana'><font size='2'><br>Dear {ContractName} {ContractSurname}, <br><br>
                            
                            <!--TEXT has been removed-->
    
                                <br><br>
    Last edited by fabzster; March 30th, 2017 at 05:30 AM.
  12. #7
  13. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,324
    Rep Power
    9645
    I'm not sure either, but I did tell you what you need to do to fix it.

    Comments on this post

    • fabzster agrees : What A Legend!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Posts
    65
    Rep Power
    10
    Ok cool,

    I just added a Line break and as you said Viola!!!! it's sorted

    Thank you soo much!!!!!!!!
    Last edited by fabzster; March 30th, 2017 at 07:19 AM.

IMN logo majestic logo threadwatch logo seochat tools logo