如何通过电子邮件发送附件?

时间:2015-04-04 12:19:13

标签: php email

任何人都可以指导我,我想通过邮件发送附件,并且我使用http://demo.tutorialzine.com/2013/05...e-upload-form/插件而不是简单,当我使用上面给出的插件发送邮件时问题就出现了#&# 39;我没有收到依恋,当我简单地说它完美无缺。因此,当我选择给定插件中的文件时,我需要使用它。

我添加的PHP代码是:

$errors = '';
$to = "my email id";



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";

?>

提前致谢!

0 个答案:

没有答案