Re: email w/attachments

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 4/17/2011 7:26 PM, tedd wrote:
Hi gang:

Anyone have an email script that allows attachments they would share?

I've been trying to figure this out and everything I've tried has
failed. I've looked at over a dozen scripts that don't work. I'm about
to pull out what hair I have left.

Cheers (I think),

tedd



I'm still using pear::mail (rhaah...) so here is an example using pear::mail.

So let's say you have:
- text only version of the body in $body
- an html version of it in $html
- and a file to attach to the email whose path is $attached_file

Something like this (extracting from a class I use) should help you:

//...

$params["host"] = ...;
$params["port"] = ...;
$params["auth"] = ...;
$params["username"] = ...;
$params["password"] = ...;
$params["localhost"] = ...;
$params["timeout"] = ...;
$params["verp"] = ...;
$params["debug"] = ...;

$smtp = Mail::factory("smtp",$params);

//...

$random_hash = "0123456789"; // :))

$mailHeaders = array();

// Setup other headers in $mailHeaders (i.e. "From", "to")

$mailHeaders["MIME-Version"] = "1.0";
$mailHeaders["Content-Type"] = "multipart/mixed;\r\n" .' boundary="PHP-mixed-' . $random_hash . '"';
$mailBody =  "This is a multi-part message in MIME format.\r\n";

// --- Text body --------------------------------------------------
				
if($body!=null){
	$mailBody .= "--PHP-mixed-" . $random_hash ."\r\n";
	$mailBody .= "Content-Type: text/plain; CHARSET=US-ASCII\r\n";
	$mailBody .= "Content-Transfer-Encoding: 7bit";
	$mailBody .= "\r\n\r\n";
	$mailBody .= $body;
	$mailBody .= "\r\n\r\n";
}

// --- HTML body --------------------------------------------------
				
if($html!=null){
	$mailBody .= "--PHP-mixed-" . $random_hash ."\r\n";
	$mailBody .= "Content-type: text/html; charset=US-ASCII\r\n";
	$mailBody .= "Content-Transfer-Encoding: 7bit";
	$mailBody .= "\r\n\r\n";
	$mailBody .= $html;
	$mailBody .= "\r\n\r\n";
}

// --- Attached file (already zipped) -----------------------------

if($attachedFile!=null){		
	if(is_file($attachedFile)){
		$mailBody .= "--PHP-mixed-" . $random_hash ."\r\n";
$mailBody .= "Content-Type: application/zip; name=\"". basename($attachedFile) . "\"\r\n";
		$mailBody .= "Content-Transfer-Encoding: base64\r\n";
		$mailBody .= "Content-Disposition: attachment";
		$mailBody .= "\r\n\r\n";
		$mailBody .= chunk_split(base64_encode(file_get_contents($attachedFile)));
	}
}

$mailBody .= "--PHP-mixed-" . $random_hash ."--";
$mailBody .= "\r\n\r\n";
					
// Setup $recipients (see note below)

ob_start();
$sending = $smtp->send($recipients,$mailHeaders,$mailBody);
ob_clean();

if ($_pear->isError($sending)){
	// Process error
}

----

Note
In the class that runs the stuff above, I put the following comment a long time ago:

/*
 Undocumented pear::mail
	
	Recipients
	
	$recipients = array(
		"To" => "...",
		"Cc" => "...",
		"Bcc" => "..."
	);
	
	Headers
	
	$headers = array(
		"To" => "..."
		"Cc" => "..."
	);
	// Do NOT specify "Bcc" in $headers (yeah make f...ing sense @$%!%)

*/



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux