T4VN is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support
    HOME  |  HOSTING COUPON  |  TEMPLATE  |  PHP SCRIPTS  |  LINK TO US  |  LINK  |  REGISTER | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Tutotal :::
  PHP Basic (7)
  PHP Advanced (4)
  PHP Database (2)
  Coding Step By Step (8)
  PHP and AJAX (0)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (12)
  Date & Time (5)
  E-Mail (6)
  Forms (5)
  Guestbooks (1)
  Logging (2)
  Miscellaneous (10)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (10)
  User Authentication (6)
::: Search On T4VN :::
::: T4VN Statistics :::
  PHP Scripts : 61
  PHP Example : 67
  PHP Tutorials : 21
  PHP News : 93
  Total Coupon : 36
  Other Tutorials :
  Member : 209
  Visitor Online : 6
  Today Visit: 308
  Total Visitor : 262318
  Most Online : 31
::: Sponsored Links :::

PR 4 For This Webpage
Home Today :

Mail with attachments

Author : JeeperEngineer

These two functions create a form and when submitted will email a set recipient.

They are intended to use SESSION variable, but that code is easily modified. Enjoy.

PHP Example :

<?
function emailUs() {
    if(isset(
$_POST['action']) && $_POST['action'] == 'SEND_MAIL') {
        echo 
"              SENDING EMAIL...<br />n";
        if(
sendMyMail()) echo "              EMAIL SENT.<br /><br />n";
    }
?>
      <table width="100%" border="0" cellspacing="0" cellpadding="0" class="form">
      <form name="email" method="post" action="<?php echo "$_SERVER[PHP_SELF]"; if(isset($_GET['menu'])) echo "?menu=$_GET[menu]"; if(isset($_GET['subMenu'])) echo "&amp;subMenu=$_GET[subMenu]"?>" enctype="multipart/form-data">
        <tr> 
          <td> 
<?php
        
if(!isset($_SESSION['userID'])) {
?>
            YOUR NAME: <input type="text" name="mailer" size="40" value="<?php echo $_SESSION['userID']; ?>" /><br /><br />                    
            YOUR EMAIL: <input type="text" name="mail_from" size="40" value="<?php echo $_SESSION['email']; ?>" /><br /><br />
<?php
        
}
?>
            SUBJECT: <input type="text" name="e_subject" size="40" maxlength="80" /><br /><br />
            MESSAGE:<br /><textarea name="e_message" cols="40" rows="10"></textarea><br />
<?php
        
if(isset($_SESSION['userID'])) {
            echo 
"                    FILE(s):<br />n";
            for(
$i 0$i 4$i++) { 
                echo 
"            <input type="file" name="myFile{$i}" size="40" /><br />n"
            }
        }
?>
          </td>
        </tr> 
        <tr> 
          <td align="right">
            <input type="hidden" name="action" value="SEND_MAIL" /> 
            <input type="submit" name="submit" value="submit" class="button" />
          </td>
        </tr>
      </form>
      </table>
<?php    
// END emailUs

function sendMyMail(){
    
$mail_to "abc@123.com";

    if(!isset(
$_SESSION['userID'])) {
        if(!empty(
$_POST['mailer'])) $mailerID $_POST['mailer'];
        else {
            echo 
"              CANNOT SEND EMAIL WITHOUT SENDERS NAME!<br /><br />n";
            echo 
"              ABORTING TRANSMISSION...<br /><br />n";
            return 
0;
        }
        
        if(!empty(
$_POST['mail_from'])) $mail_from $_POST['mail_from'];
        else {
            echo 
"              CANNOT SEND EMAIL WITHOUT SENDERS EMAIL ADDRESS!<br /><br />n";
            echo 
"              ABORTING TRANSMISSION...<br /><br />n";
            return 
0;
        }
        if(!
eregi("^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+(.[a-z]{2,})$",$_POST['mail_from'],$regs)) {
            echo 
"              THE EMAIL ADDRESS ENTERED: ("".$_POST['mail_from']."") IS NOT A VALID FORMAT!<br /><br />n";
            echo 
"              ABORTING TRANSMISSION...<br /><br />n";
            return 
0;
        }
    }
    else {
         
$mailerID $_SESSION['userID'];
         
$mail_from $_SESSION['email'];
    }        
    
    
$e_subject stripslashes($_POST['e_subject']);    
    
$e_message stripslashes($_POST['e_message']);
    
    
$border_random md5(time());
    
    
$mail_boundary "x{$border_random}x";
    
    
$new_e_message "From: {$mailerID} <{$mail_from}>rn";
    
$new_e_message .= "To: {$mail_to}rn";
    
$new_e_message .= "Reply-to: {$mail_from}rn";    
    
$new_e_message .= "MIME-Version: 1.0rn";
    
$new_e_message .= "Content-type: multipart/mixed; boundary="{$mail_boundary}"rn";
    
$new_e_message .= "This is a multi-part message in MIME format.rnrn";
    
$new_e_message .= "--{$mail_boundary}rn";
    
$new_e_message .= "Content-type: text/plain; charset="iso-8859-1"rn";
    
$new_e_message .= "Content-Transfer-Encoding:7bitrnrn";
    
$new_e_message .= "{$e_message}rnrn";
         
    for (
$i 0$i 4$i++) {
        
        
$myFile{$i} = $_FILES['myFile'.$i]['tmp_name'];
        
        if (
is_uploaded_file($myFile{$i})) {
            
            
$myFile_type $_FILES['myFile'.$i]['type'];
            
$myFile_name $_FILES['myFile'.$i]['name'];
            
$myFile_size $_FILES['myFile'.$i]['size'];
            
            
$fp fopen($myFile{$i},"rb");
            
$fileData fread($fp,$myFile_size);
            
fclose($fp);
            
            
$file base64_encode($fileData);
            
$file chunk_split($file);
            
            
$new_e_message .= "--{$mail_boundary}rn";
            
$new_e_message .= "Content-type: {$myFile_type}; name="{$myFile_name}"rn";
            
$new_e_message .= "Content-Transfer-Encoding:base64rn";
            
$new_e_message .= "Content-Disposition: attachment; filename="{$myFile_name}"rnrn";
            
$new_e_message .= $file."rnrn";
            
        }
    }
    
$new_e_message .= "--{$mail_boundary}--rn";
           
    
$e_body "";
    
    
$e_message $new_e_message;  
        
    
$send mail($mail_to,$e_subject,$e_body,$e_message);
    
    if(
$send) return 1;

    else {
        echo 
"              EMAIL COULD NOT BE SENT FOR UNKNOWN REASONS!<br /><br />n";
        return 
0;
    }
// END sendMyMail


Other Example
Sending HTML email


::: Resources :::
  Links Directory
  Programming 2 3
  Webmaster 2 3
  Web Design 2 3
  Web Hosting 2 3
  Other Links 2 3
  Asian ShowBiz News
  Teach Seo For You
::: New Templates :::




::: Other Tutorials :::
 Program Design

  Powered By T4VN.NET - Version 2.0 - CopyRight © T4VN.NET 2005-2007