Jump to content

PDF attached to email - code help


kellyalan

Recommended Posts

Hi - we're trying to attach a .pdf to an email and mail it to a client - not working -- here's the code - l'll highlight what I think I understand...  =)   I've inherited this php project and I'm in no way a PHP person though I can usually do minor fixes -- this is beyond me however. The owner of the site said that up until a year ago, this code was working. 

 

ps - I've tried incorporating swiftmailer and phpmailer but don't know how to configure those using this password verifying code and an "include" that's not a .pdf

 

question - is this code fixable and if so, how?

 

<?php

 
orange = goes to database, verifies passwords, that the report was paid for, etc - oks access and sets the "to" email address as $i_email
 
$i_get_sid = isset($_GET["sid"]) ? (int)$_GET["sid"] : $i_sid;
$i_get_pass = isset($_GET["a"]) ? $_GET["a"] : $_SESSION['r_pass'];
$i_get_pass = addslashes($i_get_pass);
 
$i_qry1 = db_qry("SELECT * FROM reports WHERE sid={$i_get_sid} AND ispurchased=1 AND isenabled=1 AND passhash='$i_get_pass'")
 or die("Error: report-4, SQL request error #1 ".mysql_error());
if($i_qry1) {
if($i_rec1=mysql_fetch_array($i_qry1)) {
$i_fname = $i_rec1["fname"];
$i_lname = $i_rec1["lname"];
$i_email = $i_rec1["email"];
}
db_free_result($i_qry1);
}
 
blue - imports the file where the pdf is generated using FPDF, gives it the variable $data and attaches(or embeds? - not sure) to the email
 
if(!empty($i_email)) {
ob_start();
include('inc/pages/report-3.inc.php');
$data = ob_get_contents();
ob_end_clean();
$data = wordwrap(base64_encode($data), 72, "\n", true);
$i_boundary = '----------'.bin2hex(mhash(MHASH_MD5, time()));
$i_headers = "From: oursite.com <info@oursite.com>
 
green - sets the content of the email message
 
X-Mailer: oursite.com PHP Mail (v1.0)
Reply-To: oursite.com <info@oursite.com>
X-Priority: 3 (Normal)
MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary=\"$i_boundary\"";
$i_message = "--$i_boundary
Content-Type: text/plain;
charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
 
Dear ".$i_fname." ".$i_lname.",
 
Here is the report you asked for, attached as an Adobe Acrobat document.
 
If you do not already have one, an Adobe Acrobat reader can be downloaded
for free at http://www.adobe.com/.
 
----
Best regards,
Sports Team
mailto:info@oursite.com
 
--$i_boundary
Content-Type: APPLICATION/PDF; name=\"report.pdf\"
Content-transfer-encoding: base64
Content-Disposition: attachment; filename=\"report.pdf\"
 
$data
 
{$i_boundary}--
";
 
 
shows a new message in the browser to the user saying the emails been sent
 
@mail($i_email, ' Sports - Your Report', $i_message, $i_headers);
@mail('null@sight2k.com', 'Sports - Your Report', "ID - $i_get_sid", $i_headers);
}
 
header('Content-type: text/html');
$m_header = '<link href="shared/report.css" rel=stylesheet type="text/css">';
require_once($DOCUMENT_ROOT."inc/top-2.inc.php");
$i_get_sid = isset($_GET["sid"]) ? (int)$_GET["sid"] : $i_sid;
 
$i_get_pass = isset($_GET["a"]) ? $_GET["a"] : $_SESSION['r_pass'];
$i_get_pass = addslashes($i_get_pass);
 
echo '<table cellpadding=0 cellspacing=0 border=0 width="100%">';
echo '<tr vAlign=top><td height=7><img src="images/1x1.gif" width=1 height=7></td></tr>';
echo '<tr height=28 style="background: url(images/bookm-bg.gif) repeat-x"><td width="100%"><nobr>';
echo '<img src="images/1x1.gif" width=5 height=1><a href="report.php?sid='.$i_get_sid.'&a='.urlencode($i_get_pass).'"><img src="images/bookm-42.gif" width=67 height=28 border=0></a><a href="report.php?sid='.$i_get_sid.'&a='.urlencode($i_get_pass).'&b=pdf"><img src="images/bookm-s3.gif" width=10 height=28 border=0><img src="images/bookm-52.gif" width=65 height=28 border=0></a> <a href="report.php?sid='.$i_get_sid.'&a='.urlencode($i_get_pass).'&b=/report.pdf"><img src="images/button-downloadpdf.gif" width=80 height=28 border=0></a>  <a href="report.php?sid='.$i_get_sid.'&a='.urlencode($i_get_pass).'&b=email"><img src="images/button-emailpdf.gif" width=80 height=28 border=0></a>';
echo '</nobr></td><td><nobr><font style="font-size: 10px;"><a href="javascript:window.close();">Close Window</a> </font></nobr></td></tr></table>';
echo "<p> Thank you.";
echo "<p> Your report has been successfully sent.";
require_once($DOCUMENT_ROOT."inc/btm-2.inc.php");
 
// header("location: report.php?sid=$i_get_sid&a=".urlencode($i_get_pass));
 
?>
 
 

>>>>>The output is this instead of an email with attachment:

 

------------fd755da9fdf74bb9c832b8a95272fd4b
Content-Type: text/plain;
        charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Dear "client",

Here is the report you asked for, attached as an Adobe Acrobat document.

If you do not already have one, an Adobe Acrobat reader can be downloaded
for free at http://www.adobe.com/.

----
Best regards,
Sports Team
mailto:info@oursite.com

------------fd755da9fdf74bb9c832b8a95272fd4b
Content-Type: APPLICATION/PDF; name="report.pdf"
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="report.pdf"

 JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNl
cyAyIDAgUgovQ29udGVudHMgNCAwIFI+PgplbmRvYmoKNCAwIG9iago8PC9GaWx0ZXIgL0Zs
YXRlRGVjb2RlIC9MZW5ndGggMzE1Pj4Kc3RyZWFtCnicdVLLTsMwELznK+YIUnHtXTuxb0BL
JbghIu6hpKgoaUqSgvr3rBNCAQlbstfemdnxg3CXaOUyfCTXOeYrA2OU1sg3uMmTN1jn4jL2 .....(goes on forever)

 
Link to comment
Share on other sites

For whatever help it provides, I use the PEAR Mail modules to send pdf attachments. They are out of date, but here's my code (without the actual longwinded message):

include 'Mail.php';
include 'Mail/mime.php' ;

$text = "Your message here";
$html = "<html><body><p>Your message here</p></body></html>";
$crlf = "\n";
$hdrs = array(
              'From'    => 'david@yourdomain.com',
              'Subject' => 'Your Subject'
              );

$mime = new Mail_mime(array('eol' => $crlf));

$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'application/pdf','valuation.pdf',FALSE);

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$recipients=$recipient_email;
$mail->send($recipients, $hdrs, $body);
Link to comment
Share on other sites

do you mean this?

 

--$i_boundary

Content-Type: APPLICATION/PDF; name=\"report.pdf\"
Content-transfer-encoding: base64
Content-Disposition: attachment; filename=\"report.pdf\"
 
$data
 
{$i_boundary}--
 
not sure - I didn't write the code, just trying to fix our issue =) -- should it be like this:   --{$i_boundary   and the ending be   $i_boundary}--
 
as far as using PEAR, I'll look into it but I always seem to run into an issue since I'm including a .php file where the data is converted to the .pdf -- I'm not actually including a .pdf
Link to comment
Share on other sites

A few things: You can use swiftmailer. Your pdf should be in $data. The password stuff at the top of the script could stay the same.

 

Try removing the @ that is in front of the mail. The @ suppresses any error messages that the mail produces.

 

The $i_boundary is used to tell the mail client where one part of the email (the message) stops and another (the pdf) starts. Looking at the output it looks right to me but looks like the email is not being sent with the headers for each section and the boundaries recognized. I'm not sure why. Any idea what changes were made when it stopped working?

Link to comment
Share on other sites

at some point, there must have been edits or changes to the code that resulted in it not working correctly but unfortunately, I don't know what those changes were.

 

I have the feeling that it's a small correction but just don't know how to narrow the problem down

Link to comment
Share on other sites

Can you format your code using our code

 tags. So, you are saying, you are able to send a pdf file and the file has been sent to the mail server successfully, but without attachment view, only as binary data, right?  Why are you calling the mail function twice?
Link to comment
Share on other sites

Why are you calling the mail function twice?

 

It looks to me like the first call is to send the email to the actual user and the second has a hardcoded address to send a notification that a report was sent to the developer/site owner. I'm assuming that was for debugging. I'm guessing that is not the source of his problem.

Link to comment
Share on other sites

regarding the mail calls/function:

 

@mail($i_email, ' Sports - Your Report', $i_message, $i_headers);  >>> sends to client
@mail('null@sight2k.com', 'Sports - Your Report', "ID - $i_get_sid", $i_headers);  >>> sends to us to confirm 
 
and yes, the email shows up but the PDF, instead of arriving as an attachment, it arrives in binary 
 
so far I've taken out the @ symbol / decided to change Content-Type: APPLICATION/PDF; name=\"report.pdf\"  to  Content-Type: application/octet-stream; name=\"report.pdf\"  (thought it might help but no luck ),  still arrives as binary
Link to comment
Share on other sites

I was found the following code few years ago somewhere in the web and still works. It should be easy to understand and re-write your script to it.

 

Here it is,

<?php

$to = "nobody@example.com";

$from = "jazzman@centos-box.com";

$subject = "Here is your attachment";
$mess = "test pdf message";
$fileatt = "SQL.pdf";
$fileatt_type = "application/pdf";

$fileatt_name = "mypdf.pdf";

$headers = "From: $from";

$file = fopen($fileatt, "rb");

$data = fread($file, filesize($fileatt));

fclose($file);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\r\n" .
          "Content-Type: multipart/mixed;\r\n" .
          " boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\r\n\r\n" .
         "--{$mime_boundary}\r\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" .
         "Content-Transfer-Encoding: 7bit\r\n\r\n" .
         $mess . "\r\n\r\n";

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}\r\n" .
          "Content-Type: {$fileatt_type};\r\n" .
          " name=\"{$fileatt_name}\"\r\n" .
          "Content-Transfer-Encoding: base64\r\n\r\n" .
          $data . "\r\n\r\n" .
          "--{$mime_boundary}--\r\n";

if (mail($to, $subject, $message, $headers))
{
    echo "<p>The email was sent.</p>";
}
else
{
    echo "<p>There was an error sending the mail.</p>";
}
Link to comment
Share on other sites

Thanks for the code -- I have the PDF being created correctly but still not getting the email sent and the attachment working

 

  1. if (mail($to, $subject, $message, $headers))
  2. {
  3. echo "<p>The email was sent.</p>";

 

-- the message does show up in my browser that the email was sent but it never shows up in my email box 

 

This is the code for grabbing the .pdf file from the folder and converting it into a variable right?  

  1. $fileatt = "SQL.pdf";
Link to comment
Share on other sites

 

This is the code for grabbing the .pdf file from the folder and converting it into a variable right?

  1. $fileatt = "SQL.pdf";

 

Yes. Make sure you're using a correct path to the file.  You'd post your updated script to check what happens at this moment. Use [ code ] tags when providing code. 

Link to comment
Share on other sites

follow up question: 

 

I'm using this code to go into the database to grab the email of our client
 
if($i_qry1) {
if($i_rec1=mysql_fetch_array($i_qry1)) {
 
$i_email = $i_rec1["email"];
 
 
then I'm changing the email address to $to variable
 
$to = $i_email;
 
everything seems to work(message says sent successfully) except that the email never arrives
 
if I replace $i_email with my personal email to test, it works -- am I writing this code incorrectly? 
 
 
Link to comment
Share on other sites

  • 3 weeks later...

on a follow up to this -- I've set up an AOL acct and a Gmail acct -- the email and .pdf attachment comes in perfectly through AOL but on Gmail, the .pdf get stripped into a MIME/text format -- is there a way to code the php to prevent this?

 

I'm assuming the pdf is getting caught as spam (??)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.