简体   繁体   English

在php邮件中发送附件

[英]send attachment in php mail

I want to send a file in a link which want to get download.I linked the path of the file in anchor tag.But I doesn't get downloaded.That file gets open in next page without downloading.I want to download a file in anchor tag.I want to download in from a link instead of attachment. 我想在要下载的链接中发送文件。我在锚标记中链接了文件的路径。但是我没有下载。该文件在下一页打开而不下载。我想下载文件在锚标记中。我想从链接而不是附件中下载。

   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>';
   }

Download PHPMailer from here 从这里下载PHPMailer

Create a PHP test file : 创建一个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 ;
}

Try this... 尝试这个...

The mail() function doesn't support attachment or HTML mail by default. 默认情况下,mail()函数不支持附件或HTML邮件。 You need to use different headers and MIME mail parts to make this possible. 您需要使用不同的标头和MIME邮件部分才能实现这一点。 Many shared hosting providers doesn't allow the usage of this function and it might be disabled. 许多共享的托管服务提供商不允许使用此功能,因此可能已被禁用。

Normally you will pass three values to the mail() function plus some headers. 通常,您会将三个值以及一些标头传递给mail()函数。 In the example below I skip the value for the message value, because the message is defined as a MIME part together with the attachment. 在下面的示例中,我跳过了邮件值的值,因为邮件和附件一起被定义为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);

Instead of linking to the real file, make a link to a PHP page with the filename as an argument, and add this header : 而不是链接到实际文件,而是使用文件名作为参数链接到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.'"');

You may of course have to adapt the paths. 当然,您可能必须调整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM