我的电子邮件PHP代码是';行不通

我的电子邮件PHP代码是';行不通,php,email,Php,Email,我的电子邮件代码不起作用 <?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();

我的电子邮件代码不起作用

<?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>


阅读此处的文档

特别是本部分:

“附加_参数”参数可用于将附加标志作为命令行选项传递给配置为在发送邮件时使用的程序,如sendmail_路径配置设置所定义。例如,当使用带有-f sendmail选项的sendmail时,可用于设置信封发件人地址

应将运行Web服务器的用户作为受信任用户添加到sendmail配置中,以防止在使用此方法设置信封发件人(-f)时将“X警告”标头添加到邮件中。对于sendmail用户,此文件为/etc/mail/trusted users。“

因此,您需要在系统中安装
sendmail
,运行Web服务器的用户应该有权通过
sendmail
发送电子邮件


检查
/var/log/apache2/error.log
(如果您在Linux下使用Apache服务器)或Web服务器的任何日志,以找到任何线索。

我不知道您在这方面的确切需求,但是如果是一个专业项目,你应该使用类似PHPMailer的东西,从你的代码中抽象出所有邮件特定的问题

例如,如果收件人邮件客户端不支持HTML消息,则当前代码将无法工作。 如果必须使用SMTP服务器而不是php邮件功能,会发生什么? 等等

更重要的是,SMTP RFC在SMTP服务器中没有得到真正的尊重。这种事情将由PHP管理员处理。

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配置或电子邮件处理系统中的问题造成的


有一篇关于诊断软件问题并获得帮助以解决问题的非常好的文章。请阅读。

请重新格式化您的问题。“它不工作”是什么意思?什么是“不工作”?你有错误吗?在浏览器中?在错误日志?和说“我的电子邮件代码不工作,…”是不够的信息,请提供更多的细节。。。与errors etcIt一样,etcIt不会向客户端发送电子邮件,也不会将数据通过电子邮件发送回info电子邮件地址或显示任何错误。有什么想法吗?