Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  styleseye : (24 April 2014 - 02:29 PM) Https://www.facebook.com/l.php?u=Https://www.youtube.com/watch?v=Geh4Rs-Boh8&feature=Youtu.be&h=Taqexxpem
@  styleseye : (24 April 2014 - 02:28 PM) Https://www.facebook.com/l.php?u=Https://www.youtube.com/watch?v=Geh4Rs-Boh8&feature=Youtu.be&h=Taqexxpem
@  yordan : (21 April 2014 - 09:11 PM) Hey, Ritesh, Did You Hear Me?
@  yordan : (14 April 2014 - 05:28 PM) By The Way, This Could Be An Interesting Subject For A Topic, What About Posting This Question? Let's See If Other People Have The Same Feeling Concerning Bootlists!
@  yordan : (13 April 2014 - 09:36 AM) Boot Order : Cd, [Usb,] Hard Drive :D
@  yordan : (11 April 2014 - 07:23 PM) I Simply Let The Bios Do That
@  Ritesh : (11 April 2014 - 10:23 AM) Is It Possible To Launch Fedora Live Cd Or Installation Disk From Hard Drive On Windows Platform Using Grub Mbr File.
@  Ritesh : (11 April 2014 - 10:21 AM) No U Are Not.. Btw.. I Have Question For You.
@  yordan : (10 April 2014 - 08:02 AM) You Are Partially Right.
I Was Not.
Nevertheless, I Am Again :)
@  Ritesh : (09 April 2014 - 07:33 PM) :P
@  Ritesh : (09 April 2014 - 07:33 PM) I Think U R Not..
@  yordan : (09 April 2014 - 09:28 AM) I'm The Master Of The Shoutbox!
@  yordan : (05 April 2014 - 10:32 PM) He-He
@  Ritesh : (04 April 2014 - 06:59 PM) Ha Ha Ha ....
@  yordan : (04 April 2014 - 11:15 AM) Welcome Back, Starscream!
@  yordan : (03 April 2014 - 02:31 PM) And I Hope That He Will Come Back Soon :)
@  yordan : (01 April 2014 - 02:53 PM) Nice, Ritesh Came, I'm Not Home Alone Today.
@  Ritesh : (01 April 2014 - 08:51 AM) Oh!!! Poor Dear Yordan..
@  yordan : (31 March 2014 - 10:02 AM) I'm A Poor Lonesome Cow-Boy
@  yordan : (27 March 2014 - 02:22 PM) He Is Unpatient Due To His Patients!

Photo
- - - - -

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


10 replies to this topic

#1 Houdini

Houdini

    Super Member

  • Members
  • 572 posts
  • Location:Nashville Tennessee

Posted 29 June 2006 - 04:37 PM

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/stevedemarcus/steve/'"]http://dhost.info/stevedemarcus/steve/'[/url] ;
$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" .

#2 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 01 July 2006 - 05:54 AM

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,

#3 Houdini

Houdini

    Super Member

  • Members
  • 572 posts
  • Location:Nashville Tennessee

Posted 01 July 2006 - 01:06 PM

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.

#4 Guest_iGuest-ashok_*

Guest_iGuest-ashok_*
  • Guests

Posted 31 July 2008 - 07:47 AM

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

#5 Guest_iGuest-AAAA_*

Guest_iGuest-AAAA_*
  • Guests

Posted 17 November 2008 - 04:47 AM

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

#6 liod

liod

    Newbie [ Level 1 ]

  • Members
  • 4 posts

Posted 18 November 2008 - 09:36 AM

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

#7 magiccode9

magiccode9

    Advanced Member

  • [HOSTED]
  • 162 posts
  • Gender:Male
  • myCENTs:77.89

Posted 21 November 2008 - 10:37 AM

Thanks your this great discussion.
Before this I have to go with phpmailer.
Now, you let know how to correctly construct a mime mail.

I will check it again tonight.

Thanks again !

---
Magiccode9,

#8 Guest_(G)Jason_*

Guest_(G)Jason_*
  • Guests

Posted 06 December 2009 - 05:05 AM

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

#9 Guest_(G)Carl_*

Guest_(G)Carl_*
  • Guests

Posted 16 September 2010 - 11:59 AM

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.



#10 Guest_(G)Carl_*

Guest_(G)Carl_*
  • Guests

Posted 16 September 2010 - 12:00 PM

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

 



#11 Guest_(G)Douglas Purdy_*

Guest_(G)Douglas Purdy_*
  • Guests

Posted 25 January 2011 - 02:52 PM

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

 





Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users