2015-05-13 29 views
1

我使用的是PHP的mail()函數, 以下是我的代碼;如何發送帶附件的HTML郵件?

$to = "[email protected]"; 
$subject = "Application for Job"; 
$attachment =chunk_split(base64_encode(file_get_contents($_FILES['attachresume']['tmp_name']))); 
$filename = $_FILES['attachresume']['name']; 

$boundary = md5(date('r', time())); 

$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\""; 

$message = "Content-Type: text/html; charset='iso-8859-1' 
Content-Transfer-Encoding: 7bit 
<strong>Candidate Name :</strong> ".$candidatename."<br> 
$message .="--_1_$boundary 
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment $attachment --_2_$boundary--"; 

mail($to,$subject,$message,$headers); 

但我不知道爲什麼,在電子郵件,我只得到附件,我沒有得到候選名稱。我的意思是html內容。

+1

更好地使用phpMailer。它使電子郵件更容易工作 –

+2

[發送附件與PHP郵件()?](http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail) –

+0

使用庫。試試[SwiftMailer](http://swiftmailer.org/)或[PHPMailer](https://github.com/PHPMailer/PHPMailer)。他們關注技術細節,可以專注於業務目標。 – axiac

回答

0

我移動到PHPMailer的 ,我使用這個代碼,

if (isset($_FILES['uploaded_file']) && 
    $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) { 
    $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'], 
         $_FILES['uploaded_file']['name']); 
} 

用於附接。

+0

yes Gunaseelan !! –

+0

4小時後我可以做到這一點.. –