簡體   English   中英

我的電子郵件PHP代碼不起作用

[英]My email PHP code isn't working

我的電子郵件代碼不起作用。

<?php
if(isset($_POST['send'])){
$to = "info@erbimages.com" ; // change all the following to $_POST
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$subject2 = "Thank you for contacting us.";
$autoreply = "<html><body><p>Dear " . $name . ",</p><p>Thank you for registering with ERB Images.</p>
<p>To make sure that you continue to receive our email communications, we suggest that you add info@erbimages.com to your address book or Safe Senders list. </p>
<p>In Microsoft Outlook, for example, you can add us to your address book by right clicking our address in the
'From' area above and selecting 'Add to Outlook Contacts' in the list that appears.</p>
<p>We look forward to you visiting the site, and can assure you that your privacy will continue to be respected at all times.</p><p>Yours sincerely.</p><p>Edward R Benson</p><p>Edward Benson Esq.<br />Founder<br />ERB Images</p><p>www.erbimages.com</p></body></html>";

    $headers2  = 'MIME-Version: 1.0' . "\r\n";
    $headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers2 .= 'From: noreply@erbimages.com' . "\r\n";

mail($from, $subject2, $autoreply, $headers2); 
    $send=false;
    if($name == '') {$error= "You did not enter your name, please try again.";}
    else {
    if(!preg_match("/^[[:alnum:]][a-z0-9_.'+-]*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$/",$from)) {$error= "You did not enter a valid email address, please try again.";}
    else {
    $send = mail($to, $subject, $body, $headers);
    $send2 = mail($from, $subject2, $autoreply, $headers2);
    }
    if(!isset($error) && !$send)
        $error= "We have encountered an error sending your mail, please notify service@erbimages.com"; }
}// end of if(isset($_POST['send']))
?>

<?php include("http://erbimages.com/php/doctype/index.php"); ?>

<?php include("http://erbimages.com/php/head/index.php"); ?>

<div class="newsletter">
    <ul>
        <form method="post" action="http://lilyandbenson.com/newletter/index.php">
            <li>
                <input size="20" maxlength="50" name="Name" value="Name"  onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;">
            </li>
            <li>
                <input size="20" maxlength="50" name="Email" value="Email" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;">
            </li>
            <li>
                <input type="submit" name="send" value="Send" id="register_send">
            </li>
        </form> 
        <?php
        ?>
    </ul>
    <div class="clear"></div>
</div>

<div class="section_error">
        <?php
        if(isset($error))
            echo '<span id="section_error">'.$error.'</span>';
        if(isset($send) &&  $send== true){
            echo 'Thank you, your message has been sent.';
        }
        if(!isset($_POST['send']) || isset($error))
        ?>

    <div class="clear"></div>
</div>

</body>
</html>

我不知道您對此的確切需求,但是如果它是一個專業項目,則應使用PHPMailer之類的東西,該東西將從您的代碼中提取所有特定於郵件的問題。

因為如果接收方郵件客戶端不支持HTML消息,則您當前的代碼不起作用。 如果您必須使用SMTP服務器而不是php郵件功能會怎樣? 等等...

SMTP服務器中並沒有真正尊重SMTP RFC,這種事情將由PHPMailer處理。

1)“我的電子郵件代碼不起作用”不是錯誤消息-您需要具體說明您的期望是什么以及為什么您的代碼看起來不符合這些期望

2)您不需要所有代碼即可復制問題。

如果您是特定的並提供相關信息,並且忽略不相關的內容,則可能會獲得更多幫助。

代碼本身不好。 除了缺少任何相關的注釋和錯誤陷阱之外,還很容易通過電子郵件標頭注入濫用。 您用於驗證電子郵件地址的正則表達式將丟棄有效地址並接受無效地址。 不建議在數組索引周圍使用大括號的IIRC。 但這可能不是為什么它達不到您的期望。

假設根本沒有傳遞郵件,請嘗試:

set_error_reporting(E_ALL);
init_set("display_errors", 1);
trigger_error("If you can't see this then your error reporting is not working");
mail("your_address@example.com", "test","hello world");

大多數郵件問題是由於使用了錯誤的php配置或電子郵件處理系統中的問題所致。

有在診斷軟件問題和獲得幫助解決這些問題一個很好的文章在這里 請閱讀。

在此處閱讀文檔http://php.net/manual/en/function.mail.php

特別是這部分:

“ sendmail_path配置設置所定義,“ additional_parameters參數可用於將其他標志作為命令行選項傳遞給配置為發送郵件時使用的程序。例如,在使用sendmail時,可用於設置信封發件人地址。 -f sendmail選項。

使用此方法設置信封發件人(-f)時,應將運行Web服務器的用戶作為受信任用戶添加為sendmail配置,以防止將“ X警告”標頭添加到郵件中。 對於sendmail用戶,此文件為/ etc / mail / trusted-users。”

因此,您需要在系統中安裝sendmail ,運行Web服務器的用戶應具有通過sendmail發送電子郵件的權限。

檢查/var/log/apache2/error.log (如果您使用的是Linux,並且使用的是Apache服務器)或網絡服務器的任何日志,以查找任何線索。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM