Home | Add to Favorites | Ebooks Directory | News  
PHP  Script programming
Home
PHP  Article
PHP Tutorial 
PHP Script  Download
Books
PHP Manual
PHP Resource
PHP Tutorial

Mailing Application
   ==============
   1. Mail as attachment using mail() function
   ---------------------------------------------------------------
   Using PHP script a mail can be sent as an attachment or as a text message

 

Using PHP script a mail can be sent as an attachment or as a text message
   The PHP mail() function can be used for this purpose
   syntax: mail(arg1,arg2,arg3,arg4)
   
   arg1 is the Email id of the receiver  
   arg2 is the subject of the mail
   arg3 is the message body (which appears as plain text in the body of the mail)
   sending the mail as an attachment is a tricky one
   Header manipulation is necessary in order to send attachments
   arg4 is the header to be manipulated


The header should contain the sender , receiver email-ids, content-type, mimie-type and mime-boundary which is an encryption of microtime along with
   some appended text.This boundary has to be used along with the filename to be attached and its content in a way which helps in attaching the file.
   Manipulation of the header plays significant role here.
   
   
   <?php
   if($_GET['chk']==1)
   {
   $to_id=$_POST['to_id'];
   $from_id=$_POST['from_id'];
   $subject=$_POST['subject'];
   $msg_body=$_POST['msg_body'];
   $file_body=$_POST['file_body'];
   $filename=$_POST['filename'];
   $mime_boundary = "<<<--==+X[".md5(time())."]";
                           
                           /*************************************************/
                           
                           $headers .= "From: $from_id\r\n";
                           $headers .= "To: \r\n";
                           $headers .= "MIME-Version: 1.0\r\n";
                           $headers .= "Content-Type: multipart/mixed;\r\n";
                           $headers .= " boundary=\"".$mime_boundary."\"";
                           $message .= "This is a multi-part message in MIME format.\r\n";
                           $message .= "\r\n";
                           $message .= "--".$mime_boundary."\r\n";
                           $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
                           $message .= "Content-Transfer-Encoding: 7bit\r\n";
                           $message .= "\r\n";
                           $message.=$msg_body;
                           $message.="\r\n";
                           $message .= "--".$mime_boundary."\r\n";
                           $message .= "Content-Type: application/octet-stream;\r\n";
                           $message .= " name=\"$filename\"\r\n";
                           $message .= "Content-Transfer-Encoding: quoted-printable\r\n";
                           $message .= "Content-Disposition: attachment;\r\n";
                           $message .= " filename=\"$filename\"\r\n";
                           $message .= "\r\n";
                           $message .= "$file_body";
                           $message .= "\r\n";
                           $message .= "--".$mime_boundary."\r\n";
                           
                           $ok=mail("$to_id","$subject",$message,$headers);
                           
                           if($ok!=1)
                           echo "<font face='arial' size='4' color='red'><b>Check for Valid e-mail Id!</b></font>";
                           else
                           echo "<font face=arial size=3 color=red>Mail has been sent to</font><br><font color=green size=2><b>$to_id</b></font>";
   }                
   ?>
   
   <form name="frm_mail" action="php_tuitorials1.php?chk=1" method="post">
           <table border="0" cellspacing="0" cellpadding="0">
                   <tr>
                           <td>
                            <font face="arial" color="red" size="4"><b>Email the Experts</b></font>
                           </td>
                   </tr>
                   <tr><td> </td></tr>
                   <tr>
                           <td>
                                   From:
                           </td>
                           <td>
                                   <input type="text" name="from_id" size=30>
                           </td>
                   </tr>
                   <tr><td> </td></tr>
                   <tr>
                           <td>
                                   To:
                           </td>
                           <td>
                                   <input type="text" name="to_id" size=30>
                           </td>
                   </tr>
                   <tr><td> </td></tr>
                   <tr>
                           <td>
                                   Subject:
                           </td>
                           <td>
                                   <input type="text" name="subject" size=30>
                           </td>
                   </tr>
                   <tr><td> </td></tr>
                   <tr>
                           <td valign="top">
                                   Message Body:
                           </td>
                           <td>
                                   <textarea name="msg_body" rows=10 cols=50></textarea>
                           </td>
                   </tr>

                  
                   <tr><td> </td></tr>
                   <tr>
                           <td valign="top">
                                   Enter the File Name to be attached
                           </td>
                           <td>
                                   <input type="text" name="filename" size=25>
                           </td>
                   </tr>

 <tr><td> </td></tr>
                   <tr>
                           <td valign="top">
                                   File Content:(Copy/Paste or Type in your file content here)
                           </td>
                           <td>
                                   <textarea name="file_body" rows=10 cols=50></textarea>
                           </td>
                   </tr>
                   <tr><td> </td></tr>
                   <tr>
                           <td colspan="2" align="center valign="middle">
                                   <input type="submit" value="Send" >
                           </td>
                   </tr>
           </table>
   </form>

                                                         Go To Index