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]}");
}
}

 

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.