Jump to content

Send inline images and attachments with email via PHP


DarkRanger

Recommended Posts

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?  :wtf:

 

Perhaps someone can help me with this script?

 

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

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.