简体   繁体   English

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

[英]My email PHP code isn't working

My email 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>

I don't know your exact needs on this, but if it's for a professionnal project you should use something like PHPMailer that will abstract all the mail specific question from your code. 我不知道您对此的确切需求,但是如果它是一个专业项目,则应使用PHPMailer之类的东西,该东西将从您的代码中提取所有特定于邮件的问题。

Because your current code does not work if the receiver mail client does not support HTML message for instance. 因为如果接收方邮件客户端不支持HTML消息,则您当前的代码不起作用。 What happens if you have to use the SMTP server instead of the php mail function ? 如果您必须使用SMTP服务器而不是php邮件功能会怎样? etc... 等等...

More than that SMTP RFC is not really respected among SMTP servers this kind of thing would be handle by the PHPMailer. SMTP服务器中并没有真正尊重SMTP RFC,这种事情将由PHPMailer处理。

1) "My email code isn't working" is not an error message - you need to be specific about what your expectations are and why your code appears not to meet those expectations 1)“我的电子邮件代码不起作用”不是错误消息-您需要具体说明您的期望是什么以及为什么您的代码看起来不符合这些期望

2) You don't need all that code just to replicate the issue. 2)您不需要所有代码即可复制问题。

You're likely to get a lot more help if you are specific and provide the relevant information, and omit the irrelevant stuff. 如果您是特定的并提供相关信息,并且忽略不相关的内容,则可能会获得更多帮助。

The code itself is not good. 代码本身不好。 In addition to lacking any relevant commenting and error trapping, is wide open to abuse via email header injection. 除了缺少任何相关的注释和错误陷阱之外,还很容易通过电子邮件标头注入滥用。 Your regex for validating email addresses will discard valid addresses and accept invalid ones. 您用于验证电子邮件地址的正则表达式将丢弃有效地址并接受无效地址。 IIRC, using curly brackets around array indices is deprecated. 不建议在数组索引周围使用大括号的IIRC。 But that's probably not why it's not meeting your expectations. 但这可能不是为什么它达不到您的期望。

Assuming that the mail is not getting delivered at all, try: 假设根本没有传递邮件,请尝试:

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");

Most mail issues are due to using the wrong php configuration or problems in the email handling systems. 大多数邮件问题是由于使用了错误的php配置或电子邮件处理系统中的问题所致。

There is a very good article on diagnosing software problems and getting help to resolve them here . 有在诊断软件问题和获得帮助解决这些问题一个很好的文章在这里 Please read it. 请阅读。

Read the documentation here http://php.net/manual/en/function.mail.php 在此处阅读文档http://php.net/manual/en/function.mail.php

Specially this part: 特别是这部分:

"The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option. “ sendmail_path配置设置所定义,“ additional_parameters参数可用于将其他标志作为命令行选项传递给配置为发送邮件时使用的程序。例如,在使用sendmail时,可用于设置信封发件人地址。 -f sendmail选项。

The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. 使用此方法设置信封发件人(-f)时,应将运行Web服务器的用户作为受信任用户添加为sendmail配置,以防止将“ X警告”标头添加到邮件中。 For sendmail users, this file is /etc/mail/trusted-users." 对于sendmail用户,此文件为/ etc / mail / trusted-users。”

So you need to have sendmail installed in your system, and the user running the webserver should have rights to send an email through sendmail . 因此,您需要在系统中安装sendmail ,运行Web服务器的用户应具有通过sendmail发送电子邮件的权限。

Check /var/log/apache2/error.log (if you are under Linux and you are using Apache server) or whatever log for your webserver to find any clue. 检查/var/log/apache2/error.log (如果您使用的是Linux,并且使用的是Apache服务器)或网络服务器的任何日志,以查找任何线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM