2009-09-13 82 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

这带有许多奇妙的功能,将节省您的所有这些问题的生活:)

相关问题