[英]send attachment in php mail
我想在要下載的鏈接中發送文件。我在錨標記中鏈接了文件的路徑。但是我沒有下載。該文件在下一頁打開而不下載。我想下載文件在錨標記中。我想從鏈接而不是附件中下載。
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>';
}
創建一個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 ;
}
嘗試這個...
默認情況下,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);
而不是鏈接到實際文件,而是使用文件名作為參數鏈接到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.'"');
當然,您可能必須調整路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.