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

时间:2010-06-10 13:38:22

标签: 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();
$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>

3 个答案:

答案 0 :(得分:1)

我不知道你对此的确切需求,但如果是专业项目,你应该使用像PHPMailer这样的东西来抽象代码中的所有特定邮件问题。

因为例如,如果接收方邮件客户端不支持HTML消息,则当前代码不起作用。 如果必须使用SMTP服务器而不是php邮件功能会发生什么? 等...

在SMTP服务器中,并不真正尊重SMTP RFC,PHPMailer会处理这种事情。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

请阅读http://php.net/manual/en/function.mail.php

中的文档

特别是这部分:

“additional_parameters参数可用于将其他标志作为命令行选项传递给配置为在发送邮件时使用的程序,如sendmail_path配置设置所定义。例如,这可用于设置信封发件人地址使用带有-f sendmail选项的sendmail时。

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

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

检查/var/log/apache2/error.log(如果您使用的是Linux,并且使用的是Apache服务器)或任何您的网络服务器的日志,以查找任何线索。

相关问题