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

时间:2011-11-23 19:06:01

标签: php html

我正在处理的网站上有一个表单,用户数据放在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

你能帮帮我吗?

1 个答案:

答案 0 :(得分:4)

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

如果你收到邮件失败,那么你的PHP配置出了什么问题(smtp服务器设置不好?),或者电子邮件被严重破坏,以至于它被你的smtp服务器彻底拒绝了。