Php代码输出文件,但不发送确认电子邮件


Php code outputting file, but not sending confirmation email

有一个我正在工作的网站上的表单,用户数据放在一个csv文件,并存储在我们的服务器上;然后发邮件通知我服务器上有一个新文件,附带了CSV文件。

然而,最近表单停止发送确认邮件,尽管它仍然在服务器上创建文件。

这是代码:

          <?php
$fileDir = "csv/";
?>
           <?php
if (isset($_POST['Submit'])) {
?>
           <?php
    $filename = date("ymd_His", time()) . ".csv";
    $myFile   = $fileDir . $filename;
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "TYPE,NAME,NUMBER,SIZE,BACKSTAMP,DESCRIPTION'n";
    fwrite($fh, $stringData);
    if ($_POST['qty_Royal_Doulton_Figurines'] > 0) {
        for ($counter = 1; $counter <= $_POST['qty_Royal_Doulton_Figurines']; $counter += 1) {
            $stringData = "Royal Doulton Figurine," . $_POST['name_Royal_Doulton_Figurines_' . $counter] . "," . $_POST['number_Royal_Doulton_Figurines_' . $counter] . "'n";
            fwrite($fh, $stringData);
        }
    }
    if ($_POST['qty_Royal_Doulton_Jugs'] > 0) {
        for ($counter = 1; $counter <= $_POST['qty_Royal_Doulton_Jugs']; $counter += 1) {
            $stringData = "Royal Doulton Jugs," . $_POST['name_Royal_Doulton_Jugs_' . $counter] . "," . $_POST['number_Royal_Doulton_Jugs_' . $counter] . "," . $_POST['size_Royal_Doulton_Jugs_' . $counter] . "'n";
            fwrite($fh, $stringData);
        }
    }
    if ($_POST['qty_Royal_Doulton_Bunnykins'] > 0) {
        for ($counter = 1; $counter <= $_POST['qty_Royal_Doulton_Bunnykins']; $counter += 1) {
            $stringData = "Royal Doulton Bunnykins," . $_POST['name_Royal_Doulton_Bunnykins_' . $counter] . "," . $_POST['number_Royal_Doulton_Bunnykins_' . $counter] . "'n";
            fwrite($fh, $stringData);
        }
    }
    if ($_POST['qty_Royal_Doulton_Beatrix_Potter'] > 0) {
        for ($counter = 1; $counter <= $_POST['qty_Royal_Doulton_Beatrix_Potter']; $counter += 1) {
            $stringData = "Royal Doulton Beatrix Potter," . $_POST['name_Royal_Doulton_Beatrix_Potter_' . $counter] . "," . $_POST['number_Royal_Doulton_Beatrix_Potter_' . $counter] . ",," . $_POST['backstamp_Royal_Doulton_Beatrix_Potter_' . $counter] . "'n";
            fwrite($fh, $stringData);
        }
    }
    if ($_POST['qty_Other_Royal_Doulton_Pieces'] > 0) {
        for ($counter = 1; $counter <= $_POST['qty_Other_Royal_Doulton_Pieces']; $counter += 1) {
            $stringData = "Other Royal Doulton Pieces," . $_POST['name_Other_Royal_Doulton_Pieces_' . $counter] . "," . $_POST['number_Other_Royal_Doulton_Pieces_' . $counter] . ",,," . $_POST['desc_Other_Royal_Doulton_Pieces_' . $counter] . "'n";
            fwrite($fh, $stringData);
        }
    }
    fclose($fh);
?>
           <?php
}
?>
            <?php
//define the receiver of the email
$to          = 'sales@seawaychina.com';
//define the subject of the email
$subject     = 'COLLECTION FOR SALE';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$message     = $_POST['First_Name'] . $_POST['Last_Name'] . "'n" . $_POST['Address1'] . $_POST['Address2'] . "'n" . $_POST['City'] . ", " . $_POST['State'] . " " . $_POST['Zip'] . "'n" . $_POST['Country'] . "'n" . "Phone: " . $_POST['Phone'] . "'n" . "Fax:  " . $_POST['Fax'] . "'n" . $_POST['Email'] . "'n--------------------'n" . $_POST['Comments'];
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with 'r'n
$headers     = "From: " . $_POST['Email'] . "'r'nReply-To: " . $_POST['Email'];
//add boundary string and mime type specification
$headers .= "'r'nContent-Type: multipart/mixed; boundary='"PHP-mixed-" . $random_hash . "'"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/csv/" . $filename)));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
           --PHP-mixed-<?php
echo $random_hash;
?> 
            Content-Type: multipart/alternative; boundary="PHP-alt-<?php
echo $random_hash;
?>"
            --PHP-alt-<?php
echo $random_hash;
?> 
            Content-Type: text/plain; charset="iso-8859-1"
            Content-Transfer-Encoding: 7bit
            COLLECTION FOR SALE
            --PHP-alt-<?php
echo $random_hash;
?> 
            Content-Type: text/html; charset="iso-8859-1"
            Content-Transfer-Encoding: 7bit
            <font face="arial">
            <strong><?php
echo $_POST['First_Name'];
?> <?php
echo $_POST['Last_Name'];
?></strong><br />
            <?php
echo $_POST['Address1'];
?> <?php
echo $_POST['Address2'];
?><br />
            <?php
echo $_POST['City'];
?>, <?php
echo $_POST['State'];
?> <?php
echo $_POST['Zip'];
?><br />
            <?php
echo $_POST['Country'];
?><br />
            Phone: <?php
echo $_POST['Phone'];
?><br />
            Fax: <?php
echo $_POST['Fax'];
?><br />
            <?php
echo $_POST['Email'];
?><br /><br />
            <?php
echo $_POST['Comments'];
?>
           </font>
            --PHP-alt-<?php
echo $random_hash;
?>--
            --PHP-mixed-<?php
echo $random_hash;
?> 
            Content-Type: text/csv; name="<?php
echo $filename;
?>" 
            Content-Transfer-Encoding: base64 
            Content-Disposition: attachment 
            <?php
echo $attachment;
?>
           --PHP-mixed-<?php
echo $random_hash;
?>--
            <?php
//copy current buffer contents into $message variable and delete current output buffer
$message   = ob_get_clean();
//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";
?> 

            <style type="text/css">
            <!--
            body,td {
            font-family: Arial, Helvetica, sans-serif;
            color: #333333;
            font-size: 12px;
            }
            input,select,textarea {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 11px;
            color: #333333;
            }
            -->
            </style>
            <meta http-equiv="Refresh" content="3;URL=http://www.seawaychina.com" />
            <br> 
            <table width="500" border="1" align="center" cellpadding="5" cellspacing="5">
            <tr>
            <td align="center"><img src="images/header.gif" /> </td>
            </tr>
            <tr>
            <td align="center"><p>&nbsp;</p>
              <p>Thank you, your list has been submitted!</p>
              <p><font color="#FF0000"><i>Please note that it may take up to a month for us to process your list. Thank you for your patience!</i></font> <br />
                </p>
              <p><br />
              </p></td>
            </tr>
            </table>

和下面是到表单的链接:

http://www.seawaychina.com/sellerform.aspx

你能帮我一下吗?

你得到mail sent了吗?如果是这样,这仅仅意味着电子邮件已经被移交给SMTP服务器,它而不是表示它实际上被传递到收件人的邮箱。如果你确实收到了邮件,那么检查邮件服务器的日志,看看php移交邮件后发生了什么。

如果你得到mailfailed,那么你的PHP配置有问题(坏的smtp服务器设置?),或者电子邮件是如此混乱,它被你的smtp服务器直接拒绝。