2009-09-13 77 views
0

我有一個PHP腳本,一旦有人提交了一些信息發送附件的電子郵件。我在我的Gmail收件箱中收到了這些電子郵件,沒有任何問題。但是,當我使用我的個人電子郵件地址或我的工作電子郵件地址時,電子郵件從未交付。這是我的腳本(下面)或我在服務器上設置的一些問題嗎?我認爲這可能是一個頭問題,但每次我改變頭,他們打破了電子郵件,一切都出現在消息體。有誰知道如何解決這一問題? 服務器是客戶端管理的Linux服務器,帶有一個plesk控制面板,所以我無法訪問php ini文件。Php mail()郵件發送到達gmail帳戶,但不是在普通的電子郵件帳戶

//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Email with Attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$mime_boundary = "<<<--==+X[".md5(time())."]"; 

$path = $_SERVER['DOCUMENT_ROOT'].'/two/php/'; 
$fileContent = chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf'))); 


$headers .= "From: [email protected] <[email protected]>\r\n"; 

$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed;\r\n"; 
$headers .= " boundary=\"".$mime_boundary."\""; 




$message = "This is a multi-part message in MIME format.\r\n"; 

$message .= "\r\n"; 
$message .= "--".$mime_boundary."\r\n"; 

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; 
$message .= "Content-Transfer-Encoding: 7bit\r\n"; 
$message .= "\r\n"; 
$message .= "Email content and what not: \r\n"; 
$message .= "This is the file you asked for! \r\n"; 
$message .= "--".$mime_boundary."" . "\r\n"; 

$message .= "Content-Type: application/octet-stream;\r\n"; 
$message .= " name=\"CTF-brochure.pdf\"" . "\r\n"; 
$message .= "Content-Transfer-Encoding: base64 \r\n"; 
$message .= "Content-Disposition: attachment;\r\n"; 
$message .= " filename=\"CTF_brochure.pdf\"\r\n"; 
$message .= "\r\n"; 
$message .= $fileContent; 
$message .= "\r\n"; 
$message .= "--".$mime_boundary."\r\n"; 

//send the email 
$mail_sent = mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
+0

喜德魯。當您發送給其他非Gmail賬戶時,輸出爲「郵件發送」或「郵件失敗」。你嘗試發送到另一個Gmail帳戶? – mauris 2009-09-13 02:14:42

+0

輸出每次都是郵件發送 – Drew 2009-09-13 08:26:43

回答

2

試試這個多部分替代版本。它接受純文本和html消息,並允許郵件客戶端選擇要顯示的內容。


//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Email with Attachment'; 
//create 2 boundary strings. They must be unique 
$boundary1 = rand(0,9)."-" 
          .rand(10000000000,9999999999)."-" 
          .rand(10000000000,9999999999)."=:" 
          .rand(10000,99999); 
$boundary2 = rand(0,9)."-".rand(10000000000,9999999999)."-" 
          .rand(10000000000,9999999999)."=:" 
          .rand(10000,99999); 

$path = $_SERVER['DOCUMENT_ROOT'].'/two/php/'; 
$fileContent = chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf'))); 

$txt_message = "Email content and what not: \r\n"; 
$txt_message .= "This is the file you asked for! \r\n"; 

$html_message = "Email content and what not: <br />"; 
$html_message .= "This is the file you asked for! "; 

$headers  =<<<AKAM 
From: [email protected] <[email protected]> 
MIME-Version: 1.0 
Content-Type: multipart/mixed; 
    boundary="$boundary1" 
AKAM; 

$attachment = <<<ATTA 
--$boundary1 
Content-Type: application/octet-stream; 
    name="CTF_brochure.pdf" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
    filename="CTF_brochure.pdf" 

$fileContent 

ATTA; 

$body = <<<AKAM 
This is a multi-part message in MIME format. 

--$boundary1 
Content-Type: multipart/alternative; 
    boundary="$boundary2" 

--$boundary2 
Content-Type: text/plain; 
    charset=ISO-8859-1; 
Content-Transfer-Encoding: 7bit 

$txt_message 
--$boundary2 
Content-Type: text/html; 
    charset=ISO-8859-1; 
Content-Transfer-Encoding: 7bit 

$html_message 

--$boundary2-- 

$attachment 
--$boundary1-- 
AKAM; 

//send the email 
$mail_sent = @mail($to, $subject, $body, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
+0

謝謝你。唉,我沒有收到關於Gmail賬戶的消息,也沒有任何其他信息 – Drew 2009-09-13 08:27:16

0

檢查您的其他帳戶上的垃圾郵件文件夾。有時,如果電子郵件帳戶提供商正在使用任何垃圾郵件過濾軟件觸發太多警告,則郵件最終可能會出現垃圾郵件。

+0

感謝您的建議,但唉,這是我看的第一個地方! – Drew 2009-09-13 08:27:46

0

mail()函數是非常有用的,除了當你不得不處理標題。大多數電子郵件將被沒有正確標題的垃圾郵件機器人拒絕。

我會推薦使用PHPMailer類,並且完成所有工作。自己做一個是痛苦的,不推薦。

https://github.com/PHPMailer/PHPMailer

這帶有許多奇妙的功能,將節省您的所有這些問題的生活:)

相關問題