Jump to content
xisto Community
Houdini

Using The Php Mail() Function For Images Or Attachments Cant find a decent tutorial

Recommended Posts

I read the one mail() tutorial that was posted in the tutorial section and to my horror found that he had quoted almost verbatim from the PHP Manual off php.net, and made a comment about it, and also found that if you were new to PHP or the Manual that it was informative but not indepth enough for my tastes. This is not a tutorial although with the code that will be posted it might look like one, that is not its intent or purpose.

 

I have searched and found many so called tutorials about MIME mail and boundries and all that but basically it either told me to use PHPMailer which I have but I want to write and customise my very own code not somebody elses. So after much research and attempts to find such, one day I got a lucky brack when a user posed an MIM mail query on DevShed. I took his code and altered it to suit my own needs.

 

When you send mail normally it will be MIM if using Outlook, Outlook Express, Messenger, Thunderbird or Eudora it is sent as Content-Type: multipart/mixed and has boundries between all the parts including attachments, which also must be read into the mail and encoded with base64 encoding. With this in mind you will have to structure your mail to donform to the requirements of MIME mail. Of course you will need to use at least one of the allowed additional two parameters of the mail() function.

 

Now actually putting all this together will be much more complex than sending a plain text file using just the below

mail($to,$subject,$message,$headers);

So I am going to show the code and see what others think. Feel free to use and test this code your self and make improvements and suggestions.

mimemail.php

<?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.            ###
###########################################################################################
$headers = "From: Me <me@email.com>";//put you own stuff here or use a variable
$to = 'someone@email.com';// same as above
$subject = 'Testing Inline attachment HTML Emails';//your own stuff goes here
$html ="<img src='beerchug.gif'><br /><br />
<b>This</b> is HTML <span style='background:cyan'>and this is a cyan highlight</span>
<br />So this should be a new line.<br /><br />This should be a new line with a space between the above.
<br />Here's dead Al<br><img src='DeadAl.jpg'><br />He is dead in this photo!<br />This is a martyr, well
OK then I think I will pass on looking like that all blowed up and all.<br /><br />So much for being a martyr!<br /> He's just another dead terrorist in the pile of the others ... ougggh nooooo!";//make up your own html or use an include
//the below is your own plain text message (all the $message(x))
$message0 = 'Dear valued customer,';// or make up your own for  plain text message
$message1 = 'NukeXtra  just released our new search engine optimisation (SEO) services.
We have exciting new packages from Cost-Per-Click (CPC, Paid advertising) to specialised optimization of your website by a designated SEO campaign manager.';
$message2 = 'Studies have proven that top placement in search engines, among other forms of online marketing, provide a more favourable return on investment compared to traditional forms of advertising such as, email marketing, radio commercials and television.';
$message3 = 'Search engine optimization is the ONLY fool proof method to earning guaranteed Top 10 search engine placement.';
$message4 = '95% of monthly Internet users utilize search engines to find and access websites';
$message5 = 'Attached is the NukeXtra SEO & CPC packages guide for your information.';
$message6 = 'If you have any questions or are interested in proceeding with our SEO services, please do not hesitate to contact us.';
$message7 = 'I look forward to this opportunity for us to work together.';
$message8 = 'With Kindest regards,';
$message9 = 'Someone';
$message10 = 'PHP Web Programmer';
$message11 = 'NukeXtra - stevedemarcus@ahost.com - [url="http://dhost.info/content/view/104/;"]http&; ;
$message12 = '218 Some Court<br />Somewhere, ST 55555';
$message12 = 'Tel: (xxx)-xxx-xxx  |  Fax: {xxx)-xxx-xxxx';
//Now lets set up some attachments (two in this case)
//first file to attach
$fileatt2 = '../images/beerchug.gif';//put the relative path to the file here on your server
$fileatt_name2 = 'beerchug.gif';//just the name of the file here
$fileatt_type2 = filetype($fileatt2);
$file2 = fopen($fileatt2,'rb');
$data2 = fread($file2,filesize($fileatt2));
fclose($file2);
//another file to attach
$fileatt = 'DeadAl.jpg';//relative path to image two and more (this one is in the same directory)
$fileatt_name = 'DeadAl.jpg';//just the name of the file
$fileatt_type = filetype($fileatt);
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
// Generate a boundary string that is unique
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
            "Content-Type: multipart/alternative;\n" .
            " boundary=\"{$mime_boundary}\"";
$message = "--{$mime_boundary}\n" .
           "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
           "Content-Transfer-Encoding: 7bit\n\n" .
           "<font face=Arial>" .
           $html."\r\n";
           $message .= "--{$mime_boundary}\n" .
           "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
           "Content-Transfer-Encoding: 7bit\n\n" .
           $message0 . "\n\n" .
           $message1 . "\n\n" .            
           $message2 . "\n\n" .
           $message3 . "\n\n" .
           $message4 . "\n\n" .
           $message5 . "\n\n" .
           $message6 . "\n\n" .
           $message7 . "\n\n" .
           $message8 . "\n\n" .
           $message9 . "\n" .
           $message10 . "\n" .
           $message11 . "\n" .
           $message12 . "\n\n";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed;\n" .
            " boundary=\"{$mime_boundary}\"";
// Base64 encode the file data
$data2 = chunk_split(base64_encode($data2));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
            "Content-Type: image/gif;\n" . //  {$fileatt_type}
            " name=\"{$fileatt_name2}\"\n" .
            "Content-Disposition: inline;\n" .
            " filename=\"{$fileatt_name2}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
            $data2 . "\n\n" .
            "--{$mime_boundary}--\n";
// Add another file attachment to the message as many as you have                
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
            "Content-Type: image/jpg;\n" . //  {$fileatt_type}
            " name=\"{$fileatt_name}\"\n" .
            "Content-Disposition: inline;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
            $data . "\n\n" .
            "--{$mime_boundary}--\n";
// Send the message
$send = mail($to, $subject, $message, $headers);
if ($send) {
echo "<p>Email Sent to intended recipients successfully!</p>";
} else {
echo "<p>Mail could not be sent. You missed something in the script. Sorry!</p>";
}
?>

Play around with the code and if you have more then post it here. I have struggled with such stuff for the last couple of weeks, since I usually just use my e-mail client, but now want and have a need to create by own without using PHPMailer. Hope that those that are wanting the ability to do such will find this of use. Also if you don't want to use the attachments inline just change the "Content-Disposition: inline;\n" . to "Content-Disposition: attachment;\n" .

Share this post


Link to post
Share on other sites

I read the one mail() tutorial that was posted in the tutorial section and to my horror found that he had quoted almost verbatim from the PHP Manual off php.net, and made a comment about it, and also found that if you were new to PHP or the Manual that it was informative but not indepth enough for my tastes. This is not a tutorial although with the code that will be posted it might look like one, that is not its intent or purpose.

You are right, and that happens with a lot of functions in the Php Manual. BTW, i read that post too and i finish very disapointed :(

 

Play around with the code and if you have more then post it here. I have struggled with such stuff for the last couple of weeks, since I usually just use my e-mail client, but now want and have a need to create by own without using PHPMailer. Hope that those that are wanting the ability to do such will find this of use. Also if you don't want to use the attachments inline just change the "Content-Disposition: inline;\n" . to "Content-Disposition: attachment;\n" .

 

Excellent info, very useful especially this last paragraph. I have a question, how and where i use the Reply-to function???

 

best regards,

Share this post


Link to post
Share on other sites

Reply-To: Cc: Bcc: and such would go into the additional headers for mail. From the manual:

additional_headers (optional)String to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).

so it would look something like this;

$headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

Note this is also from the Manual. A great page to bookmark or save as a favorite.

Share this post


Link to post
Share on other sites

images sending through mail body

Using The Php Mail() Function For Images Or Attachments

 

Hi

 

I wroet mail functionality and if I copy and paste images in that teaxt area they are displaying well

But after sending mail if I checked the mail it comes as a source code

 

Plese send the solution

Thanks &regards

Ashok

 

-reply by ashok

Share this post


Link to post
Share on other sites

Thanks man

Using The Php Mail() Function For Images Or Attachments

 

You rock dude, why is so difficult to create a simple tutorial about this, only your code was easy to go through.. I'm not newbie, years and years of programming hahaha

 

-reply by AAAA

Share this post


Link to post
Share on other sites

Many many thanks for you.I was wondering how to send attachment using php mail() function.Now i can send attachment along with newsletter for my member.Once again thank you very much

Share this post


Link to post
Share on other sites
THANK YOUUsing The Php Mail() Function For Images Or Attachments

Thank you!

I have been searching for this exact solution for years!

Thank you, its perfect :)

-reply by Jason

Share this post


Link to post
Share on other sites
Attachments and Html not being sentUsing The Php Mail() Function For Images Or Attachments

I used this above successfully on my Mac (Snow Leopard) as part of a web site that emails a standard html letter plus attachments. However I cannot get the same script to work on the Mac where the web server fot the local intranet resides. The email comes over but it shows  the code rather than the formatted html and attachments. Any ideas where the failing may be?

I am sure that there must be a config setting in postfix or php that is slightly different otherwise it should work. I have tested on my machine and then copied across the files. I have tried simple scripts with attachments hard coded in the script as tests but these also fails

Postfix is set up on both machines to relay to the exchange server and output from the source of the successful email and the failed one are exactly the same except for the obvious hostnames and one line break  after the X-PHP-Originating-Script.

Any help would be much appreciated.

Share this post


Link to post
Share on other sites
Attachment problemsUsing The Php Mail() Function For Images Or Attachments

I used this above successfully on my Mac (Snow Leopard) as part of a web site that emails a standard html letter plus attachments. However I cannot get the same script to work on the Mac where the web server fot the local intranet resides. The email comes over but it shows  the code rather than the formatted html and attachments. Any ideas where the failing may be?

I am sure that there must be a config setting in postfix or php that is slightly different otherwise it should work. I have tested on my machine and then copied across the files. I have tried simple scripts with attachments hard coded in the script as tests but these also fails

Postfix is set up on both machines to relay to the exchange server and output from the source of the successful email and the failed one are exactly the same except for the obvious hostnames and one line break  after the X-PHP-Originating-Script.

Any help would be much appreciated.

-reply by Carl

 

Share this post


Link to post
Share on other sites
Correction and a questionUsing The Php Mail() Function For Images Or Attachments

This code only shows one attachment because the final boundary

"--{$mime_boundary}--and";

appears twice.

Delete the first final boundary line and the code attaches both files.

Is there  a way to display an attached image file within the message at a certain

spot ?

 

 

-reply by Douglas Purdy

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.