Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Using The Php Mail() Function For Images Or Attachments, Can't find a decent tutorial!
Houdini
post Jun 29 2006, 04:37 PM
Post #1


Super Member
Group Icon

Group: Members
Posts: 572
Joined: 25-April 05
From: Nashville Tennessee
Member No.: 4,340



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
HTML
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
HTML
<?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 - http://dhost.info/stevedemarcus/steve/' ;
$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" .
Go to the top of the page
 
+Quote Post
TavoxPeru
post Jul 1 2006, 05:54 AM
Post #2


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



QUOTE(Houdini @ Jun 29 2006, 11:37 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 mad.gif

QUOTE(Houdini @ Jun 29 2006, 11:37 AM) *

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,
Go to the top of the page
 
+Quote Post
Houdini
post Jul 1 2006, 01:06 PM
Post #3


Super Member
Group Icon

Group: Members
Posts: 572
Joined: 25-April 05
From: Nashville Tennessee
Member No.: 4,340



Reply-To: Cc: Bcc: and such would go into the additional headers for mail. From the manual:
QUOTE
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;

QUOTE
$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.
Go to the top of the page
 
+Quote Post
iGuest
post Jul 31 2008, 07:47 AM
Post #4


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Form Mail Php - Use Php To Send Form Through Email(8)
  2. Do You Want A Mail Form In Your Site(2)
  3. Php/mysql Function Problem(2)
  4. Simple E-mail Validation(2)
  5. Sending Mail To A Mailing List(12)
  6. Anti-Spam Images(0)
  7. E-mail List Error(4)
  8. Php Send Mail Through Smtp(8)
  9. How To Use Cron Jobs To Save Two Images?(8)
  10. PHP: Need Help Sending Mail Using SMTP(5)
  11. Using Php With A Mail Server(2)
  12. Range Function(9)


 



- Lo-Fi Version Time is now: 11th October 2008 - 12:41 PM