2011-07-20 245 views
6

我想發送一個電子郵件使用mail()功能在PHP與PDF附件。 我在localmachine上運行腳本。我在php.ini中設置了smtp ip。發送電子郵件與pdf附件

Warning: mail() [function.mail]: SMTP server response: 503 Unexpected command or sequence of commands in C:\AppServ\www\PhpProject1\CV-Generator\testemail2.php on line 55

誰能告訴我什麼是錯的,請: 我可以完美,但帶有附件我收到以下錯誤發送文本電子郵件?

這裏是我的代碼:

<?php 
// download fpdf class (http://fpdf.org) 
require('./pdf/fpdf.php'); 

// fpdf object 
$pdf = new FPDF(); 

// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/) 
$pdf->AddPage(); 
$pdf->SetFont("Arial","B",14); 
$pdf->Cell(40,10, "this is a pdf example"); 

// email stuff (change data below) 
$to = $_GET['send']; 
$from = "[email protected]"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>"; 

// a random hash will be necessary to send mixed content 
$separator = md5(time()); 

// carriage return type (we use a PHP end of line constant) 
$eol = PHP_EOL; 

// attachment name 
$filename = "example.pdf"; 

// encode data (puts attachment in proper format) 
$pdfdoc = $pdf->Output("", "S"); 
$attachment = chunk_split(base64_encode($pdfdoc)); 


// main header (multipart mandatory) 
$headers = "From: ".$from.$eol; 
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol; 
$headers .= "This is a MIME encoded message.".$eol.$eol; 

// message 
$headers .= "--".$separator.$eol; 
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 
$headers .= $message.$eol.$eol; 

// attachment 
$headers .= "--".$separator.$eol; 
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol; 
$headers .= "Content-Disposition: attachment".$eol.$eol; 
$headers .= $attachment.$eol.$eol; 
$headers .= "--".$separator."--"; 

// send message 
mail($to, $subject, "", $headers); 

?> 
+0

占卜是一個55行是將是一個良好的開端。否則,這個錯誤是您嘗試發送郵件的* remote *服務器的答案。 – Bobby

+0

@Bobby - 因爲代碼中只有一行可以與SMTP服務器通信,所以我認爲我們可以安全地進行猜測 - 但是(@Amjad)指出它是一種很好的做法。 – symcbean

回答

0

我會建議你使用PHP Mailer從你的PHP發送電子郵件。我已經在許多不同的配置上取得了巨大的成功。這個類有處理的編碼,附件,custome頭,通過sendmail的發送,等等,等等

+0

好吧,我會看到我可以做謝謝你:) – Amjad

7

所有必要的方法我使用PHP的SwiftMailer(http://swiftmailer.org/):

require_once('../lib/swiftMailer/lib/swift_required.php'); 

... 


$body="Dear $fname,\n\nPlease find attached, an invoice for the period $startDate - $endDate\n\nBest regards,\n\nMr X"; 

$message = Swift_Message::newInstance('Subject goes here') 
    ->setFrom(array($email => "[email protected]")) 
    ->setTo(array($email => "$fname $lname")) 
    ->setBody($body); 
$message->attach(Swift_Attachment::fromPath("../../invoices_unpaid/$id.pdf")); 

$result = $mailer->send($message); 
+0

謝謝你們幫助解決問題:D – Amjad

+7

你應該接受正確的答案! – Eamorr

7

附件不會進入標題!他們應該只聲明MIME頭:

// main header (multipart mandatory) 
$headers = "From: ".$from.$eol; 
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol; // see below 
$headers .= "Content-Transfer-Encoding: 7bit".$eol; 


// message 
$msg = "--".$separator.$eol; 
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 
$msg .= $message.$eol.$eol; 

// attachment 
$msg .= "--".$separator.$eol; 
$msg .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$msg .= "Content-Transfer-Encoding: base64".$eol; 
$msg .= "Content-Disposition: attachment".$eol; 
$msg .= $attachment.$eol; 
$msg .= "--".$separator."--".$eol; 

// send message 
mail($to, $subject, $msg, $headers); 

還請注意,你不應該在頭中連續2個線路終端 - SMTP使用空行頭和身體之間的分隔符。

此外,EOL不應該是你的操作系統上的默認 - 它應該是通過SMTP所定義的EOL序列 - 即CR + LF