This topic is locked
[SOLVED]

 How to add attchment to email

9/29/2010 5:10:04 PM
PHPRunner General questions
D
dovercomputers author

Hi all,
Using the following code (which works perfectly by the way), how can I add an attachment to this email? One of the fields has a policy number which in turn has a pdf with the same name stored on the server.



if(@$_REQUEST["a"]=="email")

{
if(@$_REQUEST["selection"])

{

$body="";

foreach(@$_REQUEST["selection"] as $keyblock)

{

$arr=split("&",refine($keyblock));

if(count($arr)<1)

continue;

$keys=array();

$keys["q_id"]=urldecode(@$arr[0]);

$where = KeyWhere($keys);
$rs = CustomQuery("select * from quotes where $where");

$data=db_fetch_array($rs);

$body .= "Policy No: " . $data['q_policyno'] . "\n";

$body .= "Quote No: " . $data['q_quotenumber'] . "\n";

$body .= "Name: " . $data['q_name'] . "\n";

$body .= "Please find attached your policy documents";

$body .= "-------------------\n\n";

}
// send the email

$email=$data['q_email'];

$subject="Your New Policy Documents";

$arr = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $body));
// if error happened print a message on the web page

if (!$arr["mailed"])

{

echo "Error happened:
";

echo "File: " . $arr["errors"][0]["file"] . "
";

echo "Line: " . $arr["errors"][0]["line"] . "
";

echo "Description: " . $arr["errors"][0]["description"] . "
";

}

?><script> alert('Email Sent Successfully!'); </script><?php

}

}


Hope you can help
Jim

Sergey Kornilov admin 9/29/2010

PHP mail() function doesn't provide a convenient way to attach files. It will require a little work.
Here is an example of how this can be done:

http://www.texelate.co.uk/blog/send-email-attachment-with-php/

D
dovercomputers author 9/29/2010



PHP mail() function doesn't provide a convenient way to attach files. It will require a little work.
Here is an example of how this can be done:

http://www.texelate.co.uk/blog/send-email-attachment-with-php/


Thanks for the reply. I have got so far as the email is sent and the pdf is encoded but the code is displayed within the email body.

One thing I am not sure of is the email headers. In the example there are no headers so I have made them up like 'headers' => $headers. Is this correct?
Here is the code I have so far



if(@$_REQUEST["a"]=="email")

{
if(@$_REQUEST["selection"])

{

$body="";

foreach(@$_REQUEST["selection"] as $keyblock)

{

$arr=split("&",refine($keyblock));

if(count($arr)<1)

continue;

$keys=array();

$keys["q_id"]=urldecode(@$arr[0]);

$where = KeyWhere($keys);
$rs = CustomQuery("select * from quotes where $where");

$data=db_fetch_array($rs);

$body .= "Policy No: " . $data['q_policyno'] . "\n";

$body .= "Quote No: " . $data['q_quotenumber'] . "\n";

$body .= "Name: " . $data['q_name'] . "\n";

$body .= "Please find attached your policy documents \n";

$body .= "-------------------\n\n";

}

$email = $data['q_email'];

$from = "donotreply@quote-sys.co.uk";
//create and attach pdf

$fileatt = "/home/quotesys/public_html/courier/pdfs/".$data['q_policyno'].".pdf"; // Path to the file

$fileatt_type = "application/pdf"; // File Type

$fileatt_name = $data['q_policyno'].".pdf"; // Filename that will be used for the file as the attachment
$headers = "From: ".$from;
$file = fopen($fileatt,'rb');

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

fclose($file);
$semi_rand = md5(time());

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .

"Content-Type: multipart/mixed;\n" .

" boundary=\"{$mime_boundary}\"";
$body .= "This is a multi-part message in MIME format.\n\n" .

"--{$mime_boundary}\n" .

"Content-Type:text/html; charset=\"iso-8859-1\"\n" .

"Content-Transfer-Encoding: 7bit\n\n" .

$body .= "\n\n";
$dataf = chunk_split(base64_encode($dataf));
$body .= "--{$mime_boundary}\n" .

"Content-Type: {$fileatt_type};\n" .

" name=\"{$fileatt_name}\"\n" .

"Content-Disposition: attachment;\n" .

" filename=\"{$fileatt_name}\"\n" .

"Content-Transfer-Encoding: base64\n\n" .

$dataf .= "\n\n" .

"--{$mime_boundary}--\n";

// send the email



$subject="Your New Policy Documents";

$arr = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $body, 'headers' => $headers));
// if error happened print a message on the web page

if (!$arr["mailed"])

{

echo "Error happened:
";

echo "File: " . $arr["errors"][0]["file"] . "
";

echo "Line: " . $arr["errors"][0]["line"] . "
";

echo "Description: " . $arr["errors"][0]["description"] . "
";

}

?><script> alert('Email Sent Successfully!'); </script><?php

}

}


This is what appears in the email



Policy No: POL01100110

Quote No: 20100827ADA092212

Name: Test 1

Please find attached your policy documents

-------------------
This is a multi-part message in MIME format.
--==Multipart_Boundary_x6d9bd2ed1bcd9c03082a01cd946ba1a6x

Content-Type:text/html; charset="iso-8859-1"

Content-Transfer-Encoding: 7bit
Policy No: POL01100110

Quote No: 20100827ADA092212

Name: Test 1

Please find attached your policy documents

-------------------
--==Multipart_Boundary_x6d9bd2ed1bcd9c03082a01cd946ba1a6x

Content-Type: application/pdf;

name="POL01100110.pdf"

Content-Disposition: attachment;

filename="POL01100110.pdf"

Content-Transfer-Encoding: base64
JVBERi0xLjUNJeLjz9MNCjEwIDAgb2JqDTw8L0xpbmVhcml6ZWQgMS9MIDM1MDAxL08gMTIvRSAz


I will willingly pay if someone can fix it for me.

Jim

D
dovercomputers author 9/30/2010

I thought it was something to do with the headers, so I replace the phprunner mail with a standard php mail and it worked fine.

$arr = mail($email, $email, $body, $headers);




Thanks for the reply. I have got so far as the email is sent and the pdf is encoded but the code is displayed within the email body.

One thing I am not sure of is the email headers. In the example there are no headers so I have made them up like 'headers' => $headers. Is this correct?