Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attach a pdf to an email

Status
Not open for further replies.

mattquantic

Programmer
Mar 28, 2004
196
GB
Hi. I'm a cold fusion developer, trying to send an auto email with a pdf attachment. I'm new to PHP, quite like it.

In cf you just add one line to add an email attachment.
<cfmailparam file = thefile" type ="filetype">

Is there a similar command in php?

M@)
 
hi matt!

i use open source phpmailer from phpmailer.sourceforge.net.

it's just two scripts to include in your project which are also able to send attachments.

i can really recommend that for php - mailing purposes.

there's a good example on the project's site.

could u make use of that?

__________________________________________
a programmer is a tool that converts coffee into code...
aschi666, sthcomputing.de
 
<?php
class mime_mail {
var $parts;
var $to;
var $from;
var $headers;
var $subject;
var $body;

function mime_mail() {
$this->parts = array();
$this->to = "";
$this->from = "";
$this->cc = "";
$this->subject = "";
$this->body = "";
$this->headers = "";
}

function add_attachment($message, $name = "", $ctype = "application/octet-stream") {
$this->parts [] = array (
"ctype" => $ctype,
"message" => $message,
"encode" => $encode,
"name" => $name
);
}

function build_message($part) {
$message = $part["message"];
$message = chunk_split(base64_encode($message));
$encoding = "base64";
return "Content-Type: ".$part["ctype"].($part["name"]? "; name=\"".$part["name"]."\"" : "")."\nContent-Transfer-Encoding: $encoding\nContent-Disposition: attachment;\nfilename=\"".$part["name"]."\"\n\n$message\n";
}

function build_multipart() {
$boundary = "b".md5(uniqid(time()));
$multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary";
for($i = sizeof($this->parts)-1; $i>=0; $i--) $multipart .= "\n".$this->build_message($this->parts[$i]). "--$boundary";
return $multipart.= "--\n";
}

function send() {
$mime = "";
if (!empty($this->from)) $mime .= "From: ".$this->from. "\n";
if (!empty($this->cc)) $mime .= "Cc: ".$this->cc. "\n";
if (!empty($this->headers)) $mime .= $this->headers. "\n";
if (!empty($this->body)) $this->add_attachment($this->body, "", "text/plain");
$mime .= "MIME-Version: 1.0\n".$this->build_multipart();
//echo $mime;
return true;
// return mail($this->to, $this->subject, "", $mime,"-fxyz@foo.com");
}
}

$attachment5 = fread(fopen($PDF_file, "r"), filesize($PDF_file));

$mail = new mime_mail();
$mail->from = "xyz@foo.com";
$mail->headers = "Errors-To: xyz@foo.com";
$mail->to = "xyz@foo.com";
$mail->cc = "xyz@foo.com";
$mail->subject = "Dealer FAX";
$mail->body = "test msg";
$mail->add_attachment("$attachment5",test.pdf, "application/pdf");



?>


Information is not knowledge.
 
Submitted the code, without giving any explanation to it. Its very generic and you can use this code to attach any files and if you want to attach more than one file, declare attachments and call the member function.
Code:
$attachment5 = fread(fopen($PDF_file, "r"), filesize($PDF_file));
$mail->add_attachment("$attachment5",test.pdf, "application/pdf");

I was testing the code, so remove the comment in this line.

Code:
// return mail($this->to, $this->subject, "", $mime,"-fxyz@foo.com");

Used variable $PDF_file to hold the file name.


If you have questions, please post it here.

Information is not knowledge.
 
Hi guys,
I didn't see this post before I posted my own, Sorry for that. I have a similar problem were I need the $mail->add_attachment to have a dynamic value instead of a static. Is that possible?
 
you mean to create the file dynamic and attach it to email.

-------Information is not knowledge-------
 
No not really,
in order for you to make an attachment in phpmailer, the variable $mail need the full path to locate where the file is saved. Like so,
Code:
$mail->AddAttachment("C:/apachefriends/xampp/htdocs/intranet/articles/ID_nr_12.pdf", 'ID_nr_12.pdf');
...Which is a static path to the file.

I need a dynamic file path based on a value from a formpage. In other words a dependend mail path based on a clientID or ClientName value. I not necessary want to save the pdf document (as an example) in the database, which should make this alot easier to query.

I need something like the path and variables beneath, but I really don't know on how to query the thing. The actual document it's not saved in the database, it's just a file in a folder under C:/something/something/ID_nr_12.pdf", 'ID_nr_12.pdf'.

Code:
$mail->AddAttachment($path, $ClientID);

Where the variable $path is dependent on what client I'm processing using a formpage. Which is equal to the variable $ClientID. I hope you got that, as I said I think it would be easier if the pdf document would be saved in the database but it's not. How do I do this?
/rz
 
Thanks for the detailed information. Here's the code, which does the similar thing. Look how the $PDF_File is constructed and used in the code.

Code:
//clientID and clientDOC are post variables

$PDF_file = "$DOCUMENT_ROOT/$clientID/LDR$clientDOC.pdf";
class mime_mail {
	var $parts;
	var $to;
	var $from;
	var $headers;
	var $subject;
	var $body;

	function mime_mail() {
	 $this->parts = array();
	 $this->to =  "";
	 $this->from =  "";
	 $this->cc =  "";
	 $this->subject =  "";
	 $this->body =  "";
	 $this->headers =  "";
	}


	function add_attachment($message, $name = "", $ctype = "application/octet-stream") {
	 $this->parts [] = array (
	  "ctype" => $ctype,
	  "message" => $message,
	  "encode" => $encode,
	  "name" => $name
	 );
	}

	function build_message($part) {
	 $message = $part["message"];
	 $message = chunk_split(base64_encode($message));
	 $encoding = "base64";
	 return "Content-Type: ".$part["ctype"].($part["name"]? "; name=\"".$part["name"]."\"" : "")."\nContent-Transfer-Encoding: $encoding\nContent-Disposition: attachment;\nfilename=\"".$part["name"]."\"\n\n$message\n";
	}

	function build_multipart() {
	 $boundary = "b".md5(uniqid(time()));
	 $multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary";
	 for($i = sizeof($this->parts)-1; $i>=0; $i--) $multipart .= "\n".$this->build_message($this->parts[$i]). "--$boundary";
	 return $multipart.=  "--\n";
	}

	function send() {
	 $mime = "";
	 if (!empty($this->from)) $mime .= "From: ".$this->from. "\n";
	 if (!empty($this->cc)) $mime .= "Cc: ".$this->cc. "\n";
	 if (!empty($this->headers)) $mime .= $this->headers. "\n";
	 if (!empty($this->body)) $this->add_attachment($this->body, "", "text/plain"); 
	 $mime .= "MIME-Version: 1.0\n".$this->build_multipart();
	 //echo $mime;
	 return mail($this->to, $this->subject, "", $mime,"-fabc@foo.com");
	}
}

$attachment5 = fread(fopen($PDF_file, "r"), filesize($PDF_file));

$mail = new mime_mail();
$mail->from = "xyz@foo.com";
$mail->headers = "Errors-To: [EMAIL=xyz@foo.com]xyz@foo.com[/EMAIL]";
$mail->to = "xyz@foo.com";
$mail->cc = "xyz@foo.com";
$mail->subject = "CLIENT DOC";
$mail->body = "";

$mail->add_attachment("$attachment5",$PDF_file, "application/pdf");

$mail->send();

-------Information is not knowledge-------
 
surtam,
thanks for the code I really appreciate it. I only use the PDF variable part though. I'm already using PHPMailer class, so all I needed to get it to work, was to add a couple of variables. I still have a small problem though when executing the pdf file, it's change the filename instead of keeping the IDnumber "15000455.pdf" to the variable "$Submission.pdf". Do you know why it does that?
Code:
$PDF_file = "$DOCUMENT_ROOT/intranet/PDF/$SubmissionID.pdf";

$mail->AddAttachment("$PDF_file", '$SubmissionID.pdf');
 
rogerzebra:
This is happening because you have a variable reference inside single quotes. PHP treats strings inside single quotes differently from strings inside double quotes (I strongly recommend that you read the online manual entry on strings for more information).

Anyway, why put your variable references inside any kind of quotes? I never do.

$mail->AddAttachment("$PDF_file", $SubmissionID . '.pdf');

should work.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
As sleipnir214 said, we don't have to put the variable inside the quotes.

your code

Code:
$PDF_file = "$DOCUMENT_ROOT/intranet/PDF/$SubmissionID.pdf";

$mail->AddAttachment("$PDF_file", '$SubmissionID.pdf');

can be modified as

Code:
$PDF_file = "$DOCUMENT_ROOT/intranet/PDF/$SubmissionID.pdf";
$filename = $SubmissionID . ".pdf";
$mail->AddAttachment($PDF_file, $filename);

That should take care of it.

-------Information is not knowledge-------
 
Thank you guys,
I'll read up on that sleipnir, I design webpages, print material, design database architecture and building this cms system. Sometimes it's hard to keep up, thanks for all your help it makes it easier :)
/rz
 
This example has saved my life as I needed a simple php mailer to send pdf files also.

Anyway in Apple Mail the body part of the mail also shows as an MIME attachment when received, so I have 2 attachments rather than the text and the single pdf file attachment.

Any ideas?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top