Jump to content

Email attachments only recognized in gmail


jon23d

Recommended Posts

I have a class that I use for emailing.  I've never needed attachments before, so I added the method to send them, but gmail seems to be the only place that I can see them.  In Yahoo and mail.com they appear as zero-length files.

 

I sent a test email to an anonymous email service, you can see it at http://mailinator.com/maildir.jsp?email=mail_class_outgoing_test

 

Because peeps are going to wonder:

/**
* Add an attachment to the email
*
* @param STRING $path_to_file
* @param STRING $attachment_name
* 
* @return VOID
*/
function addAttachment($path_to_file, $attachment_name = null) {
$this->attachments[$path_to_file] = $attachment_name;
}

 

This actually sends the email:

private function sendMailWithAttachment() {
// create the headers
$boundary = 'jfd-' . md5(date('r', time())); 
foreach ($this->recipients as $email => $name) {
	// format to & from as 'name <email>' or just email
	$to = ($name != $email && $name) ? "$name <$email>" : $email;
	$from = ($this->from_address[1]) ? "{$this->from_address[1]} <{$this->from_address[0]}>" : $this->from_address[0];
	$header =   "From: $from\n" .
		    	"X-Mailer: FM5 1.0\n" .
		    	"MIME-Version: 1.0\n" .
    				"Content-Type: multipart/mixed;\n" .
    				"	boundary=\"$boundary\"";
	ob_start();
	?>This is a multi-part message in MIME format.

--<?=$boundary?>

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

<?=$this->html?>


	<?

	$message = ob_get_clean();

	foreach ($this->attachments as $system_filename => $save_as_filename) {
		if (!is_string($system_filename)) {
			$system_filename = $save_as_filename;
			$save_as_filename = basename($system_filename);
		}
		$filetype = mime_content_type($system_filename);
		$data = chunk_split(base64_encode(file_get_contents($system_filename)), 64);
		ob_start();
			?>

--<?=$boundary?>

Content-Type: <?=$filetype?>; name="<?=$save_as_filename?>" 
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="<?=$save_as_filename?>"

<?=$data?>
<?
		$message .= ob_get_clean();

	}

	// add the last closing boundary
	$message .= PHP_EOL ."--$boundary--";

	// send the message
	mail($email, $this->subject, $message, $header, "-f{$this->from_address[0]}");
}
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.