繁体   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