dp926460 2015-04-04 12:19
浏览 70

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

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 , the issue is occurring when I'm sending mail using above given plugin I'm not receiving attachment and when I'm simply put it works perfectly. So I need to work it when I selected files like in the given plugin.

PHP code that I've added is:

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



if(empty($_POST['name'])  || 
 empty($_POST['email']) || 
 empty($_POST['website']))
{
   $errors .= "
 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 .= "
 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: 
 
 Name : $name 
 Email : $email_address 
 Website : $website 
 Company : $company"; 


    $headers = "From: $to
"; 
    $headers .= "Reply-To: $email_address";



  $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
         $headers = "From: $to 
" .
         "MIME-Version: 1.0
" .
            "Content-Type: multipart/mixed;
" .
            " boundary=\"{$mime_boundary}\"";
         $message = "This is a multi-part message in MIME format.

" .
            "--{$mime_boundary}
" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"
" .
            "Content-Transfer-Encoding: 7bit

" .
         $message . "

";
         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}
" .
                  "Content-Type: {$type};
" .
                  " name=\"{$name}\"
" .
                  "Content-Disposition: attachment;
" .
                  " filename=\"{$fileatt_name}\"
" .
                  "Content-Transfer-Encoding: base64

" .
               $data . "

";
            }
         }
         $message.="--{$mime_boundary}--
";
if (mail($to, $subject, $message, $headers))
   header('Location: thank-you.html');
else
   echo "Error in mail";

?>

Thanks in Advance!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘