[Solved] Send inline images and attachments with email via P

Get help on web editors (Frontpage, Dreamweaver) and web languages (HTML, ASP, PHP).
Post Reply
DarkRanger
Registered User
Posts: 8346
Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:

[Solved] Send inline images and attachments with email via P

Post by DarkRanger »

I have a script that generates an excel spreadsheet, and attaches it to an email ready for mailing. Now, I can make it email the attachment no problem.

I have searched for solutions on inserting 3 images in the email as well, but to no avail. I found a script that I thought could work, but it just sends the script via email and nothing else.

Does anybody know of a good way to do this? :?

Perhaps someone can help me with this script?

Code: Select all

<?php
	include "get_contact_list.php";
	################################################################################
	###########
	### An email script that will attach images inline in an HTML and plain text e-mail ###
	### Just enter you own email infomation and file names with paths where indicated in ###
	### the script. Have fun with it alter it add to it or whatever you want. When you are ###
	### done reading all the confusing tutorials about this kind of using mail to send your ###
	### own without PHPmailer then you can just use this file to suit your self. ###
	################################################################################
	###########
	$to = 'xxxxxxx@xxxxx.com';// same as above
	$subject = 'CONTACT & SUPERVISORS LIST_'.$date;//your own stuff goes here
	// Create a boundary string.  It needs to be unique 
	$random_hash = sha1(date('r', time()));
	
	// Add in our content boundary, and mime type specification:  
	$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
	
	// Read in our file attachment
	$attachment = file_get_contents($path);
	$encoded = base64_encode($attachment);
	$attached = chunk_split($encoded);
	
	// additional headers
	$headers = "From: Nxxxxxx Cxxxxxx <xxxxxxxx@xxxxx.com>" . "\r\n" . 'Reply-To: xxxxxxxx@xxxxx.com' . "\r\n";//put you own stuff here or use a variable
	
	$inline = chunk_split(base64_encode(file_get_contents('../../images/emails/vcelogo.jpg')));
	
	// Your message here:
	ob_start();?>
	--PHP-mixed-<?php echo $random_hash; ?>--
	Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>--"
	
	--PHP-alt-<?php echo $random_hash; ?>--
	Content-Type: text/plain
	
	Hai, Its me!
	
	
	--PHP-alt-<?php echo $random_hash; ?>--
	Content-Type: multipart/related; boundary="PHP-related-<?php echo $random_hash; ?>--"
	
	--PHP-alt-<?php echo $random_hash; ?>--
	Content-Type: text/html
	
	<html>
	<head>
	<title>Test HTML Mail</title>
	</head>
	<body>
	<font color='red'>Hai, it is me!</font>
	Here is my picture: 
	<img src="cid:PHP-CID-<?php echo $random_hash; ?>" />
	</body>
	</html>
	
	--PHP-related-<?php echo $random_hash; ?>--
	Content-Type: image/jpeg; 
	Content-Transfer-Encoding: base64
	Content-ID: <PHP-CID-<?php echo $random_hash; ?>--> 
	
	<?php echo $inline; ?>
	--PHP-related-<?php echo $random_hash; ?>--
	
	--PHP-alt-<?php echo $random_hash; ?>--
	
	--PHP-mixed-<?php echo $random_hash; ?>--
	Content-Type: application/vnd.ms-excel; name='CONTACT & SUPERVISORS LIST_'.$date.'.xls';
	Content-Transfer-Encoding: base64
	Content-Disposition: attachment
	
	<?php echo $attachment; ?> 
	
	--PHP-mixed-<?php echo $random_hash; ?>--
	
	<?php
	$body = ob_get_clean(); 
	
	// Finally, send the email
	$send = mail($to, $subject, $body, $headers);
	if ($send) {
	unlink($path);
	header("Location: hr_options.php?message=Sent successfully");
	} else {
	header("Location: hr_options.php?message=Failed Sending");
	}
	?>
Last edited by DarkRanger on 20 Jan 2011, 07:51, edited 1 time in total.
Image
DarkRanger
Registered User
Posts: 8346
Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:

Re: Send inline images and attachments with email via PHP

Post by DarkRanger »

I fixed this problem. Here is an awesome guide to achieve this.
Image
Post Reply