School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,103 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,757 people online right now. Registration is fast and FREE... Join Now!



Sending Multiple Attachments with mail()

Sending Multiple Attachments with mail() Rate Topic: -----

#1 denewey  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 6
  • Joined: 18-May 09


Dream Kudos: 0

Post icon  Posted 18 May 2009 - 07:12 PM

I've got a piece of code that creates a function to send an e-mail with an attachment which is a html file. I have tried to also include another attachment which is an image that is embedded in the html file. The code for the individual attachment works fine. When I add the second attachement, the first attachment goes but not the 2nd. Below is the code:

function mail_attached($to, $from, $subject, $message, $filename, $headers = '') 
	{
		$unique_sep = md5(uniqid(time()));
		$headers .= "From: $from\n";
		$headers .= "MIME-Version: 1.0\nContent-Type: multipart/mixed;boundary=\"$unique_sep\";\n";
		$headers .= "charset=\"iso-8859-1\"\nContent-Transfer-Encoding:7bit\n\n";
		$headers .= "If you are reading this, then your e-mail client does not support MIME.\n";		
		$headers .= "--$unique_sep\n";
		$headers .= "Content-Type: text/html; name=\"" . $filename[0]['filename'] . "\"\n";
		$headers .= "Content-Transfer-Encoding: 8bit\n";
		$headers .= "Content-Disposition: attachment; filename=\"" . $filename[0]['filename'] . "\"\n\n";
		echo "The file name1 is $filename[1]['filename']<br>";//DEBUG
		$filedata = implode("", file($filename[0]['file']));
		$headers .= $filedata . "\n\n";
		$headers .= "--$unique_sep--\n";
		$headers .= "Content-Type: image/jpg; name=\"" . $filename[1]['filename'] . "\"\n";
		$headers .= "Content-Transfer-Encoding: base64\n";
		$headers .= "Content-Disposition: attachment; filename=\"" . $filename[1]['filename'] . "\"\n\n";
		echo "The file name2 is $filename[1]['filename']<br>";//DEBUG
		$filedata = implode("", file($filename[1]['file']));
		
		$headers .= chunk_split(base64_encode($filedata));
		$headers .= "--$unique_sep--\n";
		mail($to, $subject, $message, $headers);
	}

	$date = time();
	$handle = fopen("./forms/form12b25-" . $date . ".htm", "w+");
	if($handle)
	{	
		fwrite($handle, $HTML);
		fclose($handle);
		$f_array_1 = array('file'=>"./forms/form12b25-" . $date . ".htm",'mimetype'=>"text/plain",'filename'=>"form12b25-" . $date . ".htm");
		$f_array_2 = array('file'=>"ballot-checked.jpg",'mimetype'=>"image/jpg",'filename'=>"ballot-checked.jpg");
		$file_array = array($f_array_1, $f_array_2);
		
		mail_attached("denewey@gmail.com", "12b25@edgarfilingagent.com", "form12b25 Submission", "form12b25 Submission Generated", $file_array);
				



Any idea what I've done wrong?
Was This Post Helpful? 0
  • +
  • -


#2 no2pencil  Icon User is offline

  • PHoToN PoWeR PaCK : not included
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 15,043
  • Joined: 10-May 07


Dream Kudos: 2875

Expert In: Goofing Off

Posted 18 May 2009 - 07:14 PM

View Postdenewey, on 18 May, 2009 - 09:12 PM, said:

Any idea what I've done wrong?

Is the error that both files are not attaching, or that the image is not showing up when the html is viewed?

In the event that it's the 2nd question, an html file cannot load images (or css) from email attachments. The email client isn't a webserver.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month