Using The Php Mail() Function For Images Or Attachments - Can't find a decent tutorial!

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Using The Php Mail() Function For Images Or Attachments - Can't find a decent tutorial!

Houdini
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" .

 

 

 


Reply

TavoxPeru
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,

 

 

 


Reply

Houdini
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.

Reply

iGuest
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

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. php image attachment send hex - 6.34 hr back. (1)
  2. mail php attachment function - 11.01 hr back. (1)
  3. php mail attach image - 11.03 hr back. (1)
  4. how send email jpeg image using form in php - 12.30 hr back. (1)
  5. how email & save jpeg image by using html form in php - 13.64 hr back. (1)
  6. functions using pictures - 14.44 hr back. (1)
  7. php html mail image attachment - 14.58 hr back. (1)
  8. image attachment in php - 16.53 hr back. (1)
  9. how email jpeg image by using html form in php - 16.86 hr back. (1)
  10. php mail function attach file - 17.47 hr back. (1)
  11. php mail function attachment - 17.63 hr back. (2)
  12. how i attach file using mail function - 17.91 hr back. (2)
  13. php send mail inline image - 20.73 hr back. (1)
  14. multiple image attachment script - 20.77 hr back. (1)
Similar Topics

Keywords : php, mail, function, images, attachments, decent, tutorial

  1. Range Function
    (9)
  2. Using Php With A Mail Server
    (2)
    We all know you can use PHP with a web server, but can you use it with a mail server? I've made
    a personal messenger system, and I was thinking about adding additional functionality in the future,
    such as the ability to send mail from it. Then I started thinking, would it be possible to recieve
    mail with it? Say, have a PHP script that checked when a message arrived at the server, and when it
    does, check for something in the message (e.g. "pmsystem-to: admin") that would tell it to send it
    to a certain user, and then have it do the scripts to send a PM. So, is it....
  3. PHP: Need Help Sending Mail Using SMTP
    (5)
    While the mail() function of php is all bout simplicity, it lacks the otherwise necessary
    flexibility. How do I send an E-Mail using php through SMTP?....
  4. How To Use Cron Jobs To Save Two Images?
    (8)
    I have little knowlegde of PHP, and none/never done a cron job. How would I save two images that
    change every ten minutes on another website? Maybe add a timestamp and put them in a folder.....
  5. Php Send Mail Through Smtp
    (8)
    Can anyone here tell me how to send mail through SMTP server with php /mellow.gif"
    style="vertical-align:middle" emoid=":mellow:" border="0" alt="mellow.gif" /> I have search in many
    source code on web and cant find anything /sad.gif" style="vertical-align:middle" emoid=":("
    border="0" alt="sad.gif" />....
  6. E-mail List Error
    (4)
    I just coded this e-mail list, it works well for entering data into database, but if user leaves
    the field blank or adds an already exist e-mail it gives him the error message and stops loading the
    rest of the page. CODE <?php // Connects to your Database
    mysql_connect("localhost", "my_username", "mypassword") or
    die(mysql_error()); mysql_select_db("Database name") or
    die(mysql_error()); //This code runs if the form has been submitted if
    (isset($_POST['submi....
  7. Anti-Spam Images
    (0)
    For my member's profiles, I currently have it where it yanks the requested user's email in
    plain text out of the database and displays it in pure text on the page... Not good if a spam bot
    comes around! I have a book on PHP graphics, but I can't figure it out so maybe someone
    here can help me. To get the right email for the right user, it should use what the main profile
    uses to get the info, the username. I should be able to call up the script like this: CODE
    (THIS IS ALL WRAPPED IN PHP CODING SO THE "\" IS NEEDED) <img src....
  8. Sending Mail To A Mailing List
    Possibility of sending a newsletter (12)
    Task : To send an email to a list of email addresses stored in a database Premise : Page is
    written in PHP, with the database as MySQL. The email addresses need to be taken from a column of a
    table in the database which is queried with some "WHERE" clause. The email sent to these addresses
    is static. Objective : To send a newsletter to the members who have subscribed for the same on a
    website. Well, there. I have put it as objectively and as clearly as possible. I have searched
    for the keywords "mail" and "PHP" on this section of the forum, and from what I have....
  9. Simple E-mail Validation
    check for correct address and syntax (2)
    Hey , this tutorial will tell you a very simple way to check if email addresses entered are with the
    correct syntax. Hope this will help you eliminate jokers. So this script will... (check the
    characters, and check if there is a " someone somewhere.extension ". Ok, so firstly we create a
    function with all the invalid stuff in it:- CODE function CheckMail($email) { if
    (eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z](
    [-.]?[0-9a-z])*\.[a-z]{2,4}$", $em....
  10. Php/mysql Function Problem
    (2)
    I am having a problem with some of the php/mysql functions. What I want to do is display the
    results that a MySQL shell would give a user when executing a query. Using the mysql_query()
    function I get a resource, but the mysql_fetch_row() and mysql_fetch_object() functions do not seem
    to be working. If requested, code can be provided, but what I'm looking for is more of an
    example on how to use these functions or other functions to display the output of a query. Thank
    you. ~Viz....
  11. Do You Want A Mail Form In Your Site
    (2)
    Repeat post. Credits reduced by 5 days. Learn to USE THE SEARCH BUTTON before you make such posts.
    did you want to have in your web site mail form that allow the user to send mails to anther mail
    from his mail e.g. the compose in yahoo CODE <body> <form name="email"
    method="post"> <table align="center"> <tr>
    <td>from</td> <td align="right"><input type="text"
    size="60" name="from" /></td> </tr> <tr>
    <td>....
  12. Form Mail Php - Use Php To Send Form Through Email
    (8)
    Just sends all form data to a specified email. Does anyone know a free script that does this?....
  13. Mail() Not Working
    (4)
    I'm trying to use the mail() function in a script. But it doesnt work. It keeps returning false.
    It's a premade script The code is: CODE function SendPassword($userName) {
    global $db,$db_tables;//config.php // set password for username to a random value //
    return the new password or false on failure $new_password = mt_rand(); $qry =
    "UPDATE $db_tables[Users] SET
    password=password('$new_password') WHERE
    user_name='$userName'"; $db->db_query(�....
  14. How To Set Up Form Mail
    (4)
    I am programming on a web site's contact form page, How can I mail the filled in information of
    the client to particular E-mail address? I tried form action="mailto:blah@blah", but it is running
    all right on my machine rather that other computer in our LAN. The error message I got is a
    connection failure error occurred. I noticed there are some form mail info on line, but I have no
    idea to figure out how is that works. Anybody have some experience on that? Thanks!....

    1. Looking for php, mail, function, images, attachments, decent, tutorial

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for php, mail, function, images, attachments, decent, tutorial
advertisement




Using The Php Mail() Function For Images Or Attachments - Can't find a decent tutorial!



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE