在php邮件中发送附件

时间:2015-09-18 08:21:06

标签: php email

我想在一个想要下载的链接中发送一个文件。我在锚标签中链接了文件的路径。但我没有下载。这个文件在下一页打开而没有下载。我想要下载锚标签中的文件。我想从链接而不是附件下载。

   move_uploaded_file($_FILES['resume']    ['tmp_name'],'resume/'.$_FILES['resume'][name]);
   $url='resume/'.$_FILES['resume']['name'];
   $from = $email;
     $to="websoftbms@gmail.com";
     $headers1 = "From: $from\n";
     $headers = "From: $email\r\n";
     $headers .= "Reply-To: websoftbms@gmail.com\r\n";
     $headers .= "Return-Path: sathurka.mca@gmail.com\r\n";
     $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

      $body = "
      Hello,<br>
     This mail is sent via blumounts.com<br>
     Name:$user<br>
     Email:$email<br>
     Subject:$subject<br>
     message:$message<br>
     resume :<a href='//domain.com/website/$url' download>Download</a> <br>
      ";

    $body.="<br>
    Thank you,<br>
    $user<br>";

    if( $sentmail = mail( $to,"Sent via career form.", $body, $headers ))
    {
    echo '<script>
    window.alert("Email sent");
    window.reload();
   </script>';
   }

3 个答案:

答案 0 :(得分:1)

下载PHPMailer from here

创建一个PHP测试文件:

<?php
include_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer () ; 
$mail->IsSMTP () ;

// UPDATED CODE -->>
$mail->SMTPAuth   = true;  // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host       = 'smtp.gmail.com';
$mail->Port       = 465; 
$mail->Username   = // your gmail address "user3386779@gmail.com" 
$mail->Password   = // your gmail password  "passwordUser3386779!"  
// <<-- UPDATED CODE

// if you want to format your message body wih HTML Tags
$mail->IsHTML ( true ) ;

$mail->From     = $sender_s  ;
$mail->Subject  = $subject_s ;
$mail->Body     = $mail_body_lt ;

// -- Rc : you can loop to add multiple receivers ....
$mail->AddAddress (trim($rc_s));
// -- Cc : you can loop to add multiple receivers ....
$mail->AddCC (trim($cc_s));

// -- Attach file
if (file_exists($attached_files_s) !== TRUE) {
    sprintf("file: %s doesn't exist.", $attached_files_s);
}
else {
    // Attachement: you can loop to attach multiple files ....
    $mail->AddAttachment($attached_files_s);
}

// -- sending mail and catch errors
if ( ! $mail->send () ) {
    return $mail->ErrorInfo ;
}

答案 1 :(得分:1)

试试这个......

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

通常,您会将三个值传递给mail()函数以及一些标题。在下面的示例中,我跳过了消息值的值,因为消息被定义为MIME部分以及附件。

   <?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 ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }
}
$my_file = "file.extension";
$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,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);

答案 2 :(得分:0)

不是链接到真实文件,而是以文件名作为参数链接到PHP页面,并添加此标题:

// $mimetype is the mimetype of your file.
// You may force it, or use finfo functions to get it : 
// http://php.net/manual/fr/function.finfo-file.php
header('Content-type: '.$mimetype);
header('Content-Disposition: attachment; filename="'.$file.'"');

你当然可以调整路径。