2012-06-12 141 views
2

在嘗試閱讀各種關於用PHP附件發送郵件的電子郵件(我用VB來使用ASP)後,我寫了下面的代碼。不幸的是,它根本不起作用。它不僅不會發送帶有附件的電子郵件,即使我的腳本說它確實發送了,電子郵件也不會發送。我哪裏錯了?我沒有使用表單上傳文件。這是一個靜態腳本。PHP發送電子郵件與文件附件 - 電子郵件不發送

<?php 

$EmailTo = "[email protected]"; 
$EmailFrom = "[email protected]"; 
$EmailSubject = "The Email Subject"; 

$MailBoundary = md5(uniqid(time())); 

$Headers = "To: ". $EmailTo . "\r\n"; 
$Headers .= "From: ". $EmailFrom . "\r\n"; 
$Headers = "MIME-Version: 1.0\r\n"; 
$Headers .= "Content-type: multipart/mixed;boundary=\"$MailBoundary \""; 
$Headers .= "\r\n\r\n"; 
$Headers .= "This is a multi-part message in MIME format."; 
$Headers .= "\r\n\r\n"; 

$FileAttachment = "AttachedFile.pdf"; 
$File = fopen($FileAttachment, "r"); 
$FileData = fread($File, filesize($FileAttachment)); 
$FileData = chunk_split(base64_encode($FileData)); 
$FileName = basename($FileAttachment); 

$EmailBody = "--$MailBoundary\r\n"; 
$EmailBody .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$EmailBody .= "Content-transfer-encoding: 8bit\r\n\r\n"; 

$EmailBody .= "<html>" . chr(13) . 
       "<head>" . chr(13) . 
       "<style>" . chr(13) . 
       ".breg {font-family:arial;font-size:10pt;color:#000000;padding:5px;}" . chr(13) . 
       "</style>" . chr(13) . 
       "</head>" . chr(13) . 
       "<body>" . chr(13) . 
       "<div class=" . chr(34) . "breg" . chr(34) . ">" . chr(13) . 
       "The message text body goes here" . chr(13) . 
       "</div>" . chr(13) . 
       "</body>" . chr(13) . 
       "</html>"; 

$EmailBody .= "--$MailBoundary\r\n"; 


$EmailBody .= "Content-type: " . mime_content_type($File) . "; name=$FileName\r\n"; 
$EmailBody .= "Content-transfer-encoding:base64\r\n\r\n"; 
$EmailBody .= $FileData. "\r\n\r\n"; 

$EmailBody .= " --$MailBoundary--"; 

if (mail($EmailTo, $EmailSubject, $EmailBody, $Headers)) 
{ 
echo "Email to " . $EmailTo . " has been sent" . chr(13) . "<br />" . chr(13); 
} 
else 
{ 
echo "<b>Email to " . $EmailTo . " was not sent</b>" . chr(13) . "<br />" . chr(13); 
} 

?> 
+1

您是否嘗試過剝離一切多餘的東西了,只是發送最小的電子郵件,以確保郵件電話正常工作?如果可行,您可以慢慢添加功能,直到找到停止工作的部分。 – andrewsi

+0

我會停止嘗試與PHP郵件()它非常不足,看看phpmailer或其他calss – 2012-06-12 20:34:59

+0

也許你應該使用PHPMailer類而不是!谷歌這一 – Yang

回答

10
For sending mail with attachment using php mail().Try this code: 

<?php 

//If there is no error, send the email 
if(isset($_POST['ur_submit_button_name'])) { 
    $EmailTo = "[email protected]"; 
    $EmailFrom = "[email protected]"; 
    $EmailSubject = "The Email Subject"; 


    $separator = md5(time()); 

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

    // attachment name 
    $filename = "ip.zip";//store that zip file in ur root directory 
    $attachment = chunk_split(base64_encode(file_get_contents('ip.zip'))); 

    // main header 
    $headers = "From: ".$from.$eol; 
    $headers .= "MIME-Version: 1.0".$eol; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; 

    // no more headers after this, we start the body! // 

    $body = "--".$separator.$eol; 
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; 
    $body .= "This is a MIME encoded message.".$eol; 

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

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

    // send message 
    if (mail($to, $subject, $body, $headers)) { 
    $mail_sent=true; 
    echo "mail sent"; 
    } else { 
    $mail_sent=false; 
    echo "Error,Mail not sent"; 

} 
} 

?> 
+0

完美的代碼工作就像一個魅力 –

+0

郵件不發送我們必須做的。 –

+0

太棒了!它只有在我注意到你的字符集雙引號後才起作用:''Content-Type:text/plain; charset = \「iso-8859-1 \」「。$ eol' – deblocker

0

我想你在你的電子郵件的內容有錯誤, 生成你的電子郵件內容與imap_mail_compose功能:

mail('', 'message subject', '', imap_mail_compose(array header, array body));