简体   繁体   中英

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.

Because your current code does not work if the receiver mail client does not support HTML message for instance. What happens if you have to use the SMTP server instead of the php mail function ? etc...

More than that SMTP RFC is not really respected among SMTP servers this kind of thing would be handle by the 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

2) You don't need all that code just to replicate the issue.

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

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

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.

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. For sendmail users, this file is /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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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