Wednesday, August 8, 2012

How to send email with attachement in php

<?php
include('../../../wp-load.php');

function eemail_send_mail($recipients = array(), $subject = '', $message = '',$attachment='', $type='plaintext', $sender_name='', $sender_email='',$filename='')
{
  
    global $wpdb;
    global $user_login , $user_email;
  
    if($sender_email == "" || $sender_name == '')
    {
        get_currentuserinfo();
        $sender_email = $user_email;
        $sender_name = $user_login;
    }
  
    $eemail_from_name = get_option('eemail_from_name');
    if($eemail_from_name!="")
    {
        $sender_name = $eemail_from_name;
    }
  
    $eemail_from_email = get_option('eemail_from_email');
    if($eemail_from_email!="")
    {
        $sender_email = $eemail_from_email;
    }
  
    $num_sent = 0; // return value
  
    if ( (empty($recipients)) ) { return $num_sent; }
  
    if ('' == $message) { return false; }
  
  
    // $fileatt="../wp-content/plugins/email-newsletter/attachement/".$attachment;
          $fileatt=$attachment;
         //echo  $fileatt;
        //  echo "<img src='".$fileatt."'>";
    $fileatt_type = ".jpg,.gif,.txt,.jpeg,.rar,.php,.flv,.doc,.docx,.log,.dat,.mp3,.mp4"; // File Type
         $fileatt_name =$filename;
    $email_from =$eemail_from_email;
  
        $email_subject = stripslashes($subject);
  
    $headers = "From: ".$email_from;

    $file = fopen($fileatt,'rb');
        $data = fread($file,filesize($fileatt));
        fclose($file);
        if($data){
            //echo "aaaaaaaaaaaaaaaaaa";
            //echo  $data;
        }

    $semi_rand = md5(time());
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
      
        $from = $email;
  
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        $headers .="From: \"$from\" <$from>\r\nReply-To: \"$from\" <$from>\r\nX-Mailer: PHP/".phpversion();  
        $headers .= "\nMIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" .
        " boundary=\"{$mime_boundary}\"";
      
    $email_message = stripslashes($message);
    $email_message .= "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        $email_message . "\n\n";
      
        $data = chunk_split(base64_encode($data));
      
        $email_message .= "--{$mime_boundary}\n" .
        "Content-Type: {$fileatt_type};\n" .
        " name=\"{$fileatt_name}\"\n" .
        "Content-Transfer-Encoding: base64\n\n" .
        $data . "\n\n" ."--{$mime_boundary}--\n";
  
    if(count($recipients) > 0)
    {
        for ($i=0; $i<count($recipients); $i++)
        {
            @$to = @$recipients[$i];
            if (!eemail_valid_email($to))
            {
                echo "$to email not valid<br>";
                continue;
            }
            //@$newheaders = $headers . "To: \"" . $to . "\" <" . $to . ">\n" ;
            //echo $email_message;
            //@wp_mail($to, $subject, $message , $headers);
            @mail($to, $email_subject, $email_message, $headers);
               @$num_sent = @$num_sent + 1;
        }
    }
    return $num_sent;
}


function eemail_valid_email($email) {
   $regex = '/^[A-z0-9][\w.+-]*@[A-z0-9][\w\-\.]+\.[A-z0-9]{2,6}$/';
   return (preg_match($regex, $email));
}

if(isset($_POST['submit'])){
$name=$_POST['name'];
$subject=$_POST['subject'];
$email=array($_POST['email']);
$message=$_POST['message'];
$attachment=$_FILES['uploaded_file']['tmp_name'];
$filename=$_FILES['uploaded_file']['name'];
$type='html';
$senderemail='test@test.com';
print_r($_FILES);
if(eemail_send_mail($email,$subject,$message,$attachment,$name,$senderemail,$filename))
{
echo"aaaaaaaaaaaaaaaaaaaaaaaaaaaa";
}else{
?>
<form method="POST" name="email_form"
action="" enctype="multipart/form-data">

<p>
<label for='name'>Name: </label><br>
<input type="text" name="name" >
</p>
<p>
<label for='name'>Subject: </label><br>
<input type="text" name="subject" >
</p>
<p>
<label for='email'>Email: </label><br>
<input type="text" name="email" >
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<p>
<label for='uploaded_file'>Select A File To Upload:</label> <br>
<input type="file" name="uploaded_file">
</p>
<input type="submit" value="Submit" name='submit'>

</form>
<?php
}
}else{
?>

<form method="POST" name="email_form"
action="" enctype="multipart/form-data">

<p>
<label for='name'>Name: </label><br>
<input type="text" name="name" >
</p>
<p>
<label for='name'>Subject: </label><br>
<input type="text" name="subject" >
</p>
<p>
<label for='email'>Email: </label><br>
<input type="text" name="email" >
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<p>
<label for='uploaded_file'>Select A File To Upload:</label> <br>
<input type="file" name="uploaded_file">
</p>
<input type="submit" value="Submit" name='submit'>

</form>
<?php
}

?>

2 comments: