php中的邮件附件

时间:2018-03-06 05:37:08

标签: php phpmailer

在我的代码邮件功能在这里工作很好,但附件只显示为名称,但我希望附件是下载类型。

以下是代码:

<?php 
 include("db.php");
 if(isset($_POST['submit']))
 {
    $cname=$_POST['cname'];
    $file = $_FILES['file']['name'];
    if($file != "")
    {
     $secondname=rand(0,10000000000);
     $uploaddir = "img/careers/";
     $upload_pic = $uploaddir.$secondname.$file;
     copy($_FILES['file']['tmp_name'], $upload_pic);
     chmod("$uploaddir",0777); 
   } 

$email='nishanth.radical@gmail.com'; 
$subject ="Acknowledgement"; 
$from='developer@sixthstar.in'; 
$message ="Dear $cname,

Test Mail your resume is $upload_pic";

$headers = "From:".$from;

@mail($email,$subject,$message,$headers); 

//echo "<div style='width: 350px; text-align: center; margin: 20% auto 0px; font-family: arial; font-size: 14px; border: 1px solid #ddd; padding: 20px 
//   40px;'> Please wait while we update the record loading...</div>";
            echo "<script type='text/javascript'>window.location = 'careers.php?msg=1'</script>";
exit();

 }  
?>

这是我的表单代码:

 <form method="post" action="" id="accountx" method="post" enctype="multipart/form-data"> 
    <div class="form-body pl-0">
      <div class="spacer-b30">
        <div class="tagline">
           <span>Apply for a Job </span>
        </div>
  <!-- .tagline -->
      </div>
      <div class="section">
        <label class="field prepend-icon">
           <input type="text" name="cname" id="cname" class="gui-input" placeholder="Name...">
           <span class="field-icon">
             <i class="fa fa-user"></i>
           </span>
        </label>
    </div>
    <div class="section">
      <label class="field prepend-icon">
          <input type="file" name="file" id="file" class="gui-input" placeholder="Upload Resume">
           <span class="field-icon">
                    <i class="fa fa-file"></i>
           </span>
        </label>
     </div> 
</form> 

所有邮件功能都运行正常但我希望附件是下载格式。

2 个答案:

答案 0 :(得分:0)

所以, 你可以使用phpmailer库 https://github.com/PHPMailer/PHPMailer

`

$email = new PHPMailer();
$email->From      = 'from@abcd.com';
$email->FromName  = 'from name';
$email->Subject   = 'Subject';
$email->Body      = 'Body';
$email->AddAddress( 'to@someperson.com' );
$email->AddAttachment( "/path/to/file" , "filename.ext", 'base64', 'application/octet-stream' );
$email->Send();

`

答案 1 :(得分:0)

我在经过长时间的搜索后找到了这段代码,但我的建议是给我们phpmailer

默认情况下,mail()函数不支持附件或HTML邮件。您需要使用不同的标头和MIME邮件部分才能实现此目的。许多共享托管服务提供商不允许使用此功能,但可能会被禁用。

通常,您会将三个值传递给mail()函数以及一些标题。在下面的示例中,我跳过了消息值的值,因为消息被定义为MIME部分以及附件。下面是我如何使用此mail_attachment函数发送带有一个附加.zip文件的电子邮件的示例:

<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
  $file = $path.$filename;
  $file_size = filesize($file);
  $handle = fopen($file, "r");
  $content = fread($handle, $file_size);
  fclose($handle);
  $content = chunk_split(base64_encode($content));
  $uid = md5(uniqid(time()));
  $header = "From: ".$from_name." <".$from_mail.">\r\n";
  $header .= "Reply-To: ".$replyto."\r\n";
  $header .= "MIME-Version: 1.0\r\n";
  $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
  $header .= "This is a multi-part message in MIME format.\r\n";
  $header .= "--".$uid."\r\n";
  $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
  $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  $header .= $message."\r\n\r\n";
  $header .= "--".$uid."\r\n";
  $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
  $header .= "Content-Transfer-Encoding: base64\r\n";
  $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
  $header .= $content."\r\n\r\n";
  $header .= "--".$uid."--";
  if (mail($mailto, $subject, "", $header)) {
   echo "mail send ... SUCCESS";
  } else {
   echo "mail send ... ERROR!";
  }
}

$my_file = "somefile.zip";
$my_path = "/your_path/to_the_attachment/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);

我已经测试了这个,这太有用了!