How to sending mail with attachment?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TanuLamba15
    New Member
    • Feb 2015
    • 3

    How to sending mail with attachment?

    Hi All,

    Can anyone guide me I want to send attachment with my mail and I'm using http://demo.tutorialzine.com/2013/05...e-upload-form/ plugin instead of simple <input type="file"/>, the issue is occurring when I'm sending mail using above given plugin I'm not receiving attachment and when I'm simply put <input type="file"/> it works perfectly.

    PHP code that I've added is:

    Code:
    $errors = '';
    $to = "abc@gmail.com";
    
    
    
    if(empty($_POST['name'])  || 
     empty($_POST['email']) || 
     empty($_POST['website']))
    {
       $errors .= "\n Error: all fields are required";
    }
    
    $name = $_POST['name']; 
    $email_address = $_POST['email'];
    $website = $_POST['website'];
    $company = $_POST['company'];
    
    
    
    if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
    $email_address))
    {
       $errors .= "\n Error: Invalid email address";
    }
    
    
    
    function getSiteTitle(){ 
    $RefURL = (is_null($_SERVER['HTTP_REFERER'])) ? 'Un know' : $_SERVER['HTTP_REFERER'];
      if($RefURL != 'Un know'){
       $con = file_get_contents($RefURL) or die (" can't open URL referer ");
       $pattern = "/<title>(.+)<\/title>/i";
       preg_match($pattern,$con,$match);
       $result = array($match[1],$RefURL);
       return $result;
      }
      else{
       return false;
      }
    }
    
    $info = getSiteTitle();
    $subject =  $info[0];
    
    
    
    
    
    
    
    $message = "Message goes here..".
    	"Here are the details: \n \n Name : $name \n Email : $email_address \n Website : $website \n Company : $company"; 
    	
    	
    	$headers = "From: $to\n"; 
    	$headers .= "Reply-To: $email_address";
    	
    
    	
      $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
             $headers = "From: $to \r\n" .
             "MIME-Version: 1.0\r\n" .
                "Content-Type: multipart/mixed;\r\n" .
                " boundary=\"{$mime_boundary}\"";
             $message = "This is a multi-part message in MIME format.\n\n" .
                "--{$mime_boundary}\n" .
                "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
                "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n";
             foreach($_FILES as $userfile)
             {
                $tmp_name = $userfile['attachFile'];
                $type = $userfile['type'];
                $name = $userfile['name'];
                $size = $userfile['size'];
                if (file_exists($tmp_name))
                {
                   if(is_uploaded_file($tmp_name))
                   {
                      $file = fopen($tmp_name,'rb');
                      $data = fread($file,filesize($tmp_name));
                      fclose($file);
                      $data = chunk_split(base64_encode($data));
                   }
                   $message .= "--{$mime_boundary}\n" .
                      "Content-Type: {$type};\n" .
                      " name=\"{$name}\"\n" .
                      "Content-Disposition: attachment;\n" .
                      " filename=\"{$fileatt_name}\"\n" .
                      "Content-Transfer-Encoding: base64\n\n" .
                   $data . "\n\n";
                }
             }
             $message.="--{$mime_boundary}--\n";
    if (mail($to, $subject, $message, $headers))
       header('Location: thank-you.html');
    else
       echo "Error in mail";
    
    ?>
    Thanks in advance!
    Last edited by Rabbit; Apr 2 '15, 03:29 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
Working...