-1

first of all, I am learning PHP and I am at an elementary level, (sorry). I have a script that send me an email once the form it filled in. The form is located on another page, this is the page that does all the magic. For SOME REASON, this code does not work, I do not get an emails at all. As I missing something!

Thank you

    <?php

if(!$_POST) exit;

// Verifico email.
function isEmail($email) { 
    return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));     
}

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

$name     = $_POST['name'];
$email    = $_POST['email'];
$subject  = $_POST['subject'];
$comments = $_POST['comments'];
$verify   = $_POST['verify'];

if(trim($name) == '') {
    echo '<div class="error_message">Attention! You must enter your name.</div>';
    exit();
} else if(trim($email) == '') {
    echo '<div class="error_message">Attention! Please enter a valid email address.</div>';
    exit();

} else if(!isEmail($email)) {
    echo '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
    exit();
}

if(trim($subject) == '') {
    echo '<div class="error_message">Attention! Please enter a subject.</div>';
    exit();
} else if(trim($comments) == '') {
    echo '<div class="error_message">Attention! Please enter your message.</div>';
    exit();
} else if(!isset($verify) || trim($verify) == '') {
    echo '<div class="error_message">Attention! Please enter the verification number.</div>';
    exit();
} else if(trim($verify) != '4') {
    echo '<div class="error_message">Attention! The verification number you entered is incorrect.</div>';
    exit();  
}

if(get_magic_quotes_gpc()) {
    $comments = stripslashes($comments);
}


// Configuracion.

$address = "abc@xyz.com";
$addressrrhh = "abc@xyz.com";
$e_subject = 'You\'ve been contacted by ' . $name . '.';

// Campos del form ampliables.

$e_body = "You have been contacted from JACK contact form by $name with regards to $subject, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $email" . PHP_EOL;
$headers = "BCC: $addressrrhh" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

if(mail($address, $e_subject, $msg, $headers)) {

    // Mensaje envio OK

    echo "<fieldset>";          
    echo "<div id='success_page'>";
    echo "<h1>Email Sent Successfully.</h1>";
    echo "<p>Thank you <strong>$name</strong>, your message has been submitted.</p>";
    echo "</div>";
    echo "</fieldset>";

} else {

    echo 'ERROR!';

}
?>
Arthor
  • 666
  • 2
  • 13
  • 40
  • 5
    You completely misunderstood that. – SLaks Jan 05 '12 at 13:26
  • 1
    "I have been told that $_POST is not that good to use because emails can be sent to the junk box etc." This is **completely** false. – ceejayoz Jan 05 '12 at 13:28
  • 2
    `I have been told that $_POST is not that good to use because emails can be sent to the junk box etc` - well you were told wrong. They have nothing to do with each other. Whoever told you that is a danger to themselves and others... – DaveRandom Jan 05 '12 at 13:29
  • Methinks somebody is having a bit of fun with Arthor: after all, if I want to send a mail, I do it via the post office – Mark Baker Jan 05 '12 at 13:30
  • Ok, Then why not re-educate me opposed to giving me a minus, that's not fair at all!!! – Arthor Jan 05 '12 at 13:34
  • I have updated the post, you guys need to be a little but more understanding when it comes to learning. I did not say I knew. I said I was TOLD, at least I asked and made the effort. Now I have to be re-educated. Fine, no problem. Life is based on that! – Arthor Jan 05 '12 at 13:41
  • Ok, I have a question. I do not mean for it to be STUPID. !!! Does the above script work with send emails or may it require another file? Thank you – Arthor Jan 05 '12 at 13:48
  • $_POST has nothing to do with mails going to junk folder – zista Jan 05 '12 at 13:30
  • Yes, I just had a look :The predefined $_POST variable is used to collect values from a form sent with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. – Arthor Jan 05 '12 at 13:43
  • Thank you very much, I appropriate the help. – Arthor Jan 05 '12 at 13:44

4 Answers4

2

Please have a look at http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods and http://php.net/manual/de/function.mail.php first

pintxo
  • 2,085
  • 14
  • 27
1

I think that using SMTP is better than something else. If you encounter problems with the mail() function, maybe you take a look at PEAR: pear.php.net

1

Post is something completely different. Its not like the dutch word post which means mail :D

$_POST is a variable that stores all values a post form sends. so for example: a form with the method POST has 1 input text field with the name "foo". After you type in: "bar" in that field and submit the field a $_POST array is created where $_POST["foo"] has the value "bar"

Maybe you read something about not using php's on mail() function and better use SMTP? That makes a little bit more sense...

Good luck

Hans Wassink
  • 2,529
  • 3
  • 30
  • 45
1

First thing first

  1. Do you have errors when you submit in your webserver error log ?

  2. Is your mail sent ? (Check your mail log)

  3. If so, is it in your spam folder ?

About your mail marked directly as spam Things "changed" a little bit, and you can't just send an email without setting the "fifth" parameters or your mail will go directly to spam boxes.

From http://php.net/manual/en/function.mail.php

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.

Also, i'm suggestion you the very good phpmailer

Usage exemple : http://phpmailer.worxware.com/index.php?pg=examplebmail

Hope it helps

jflaflamme
  • 1,767
  • 12
  • 9
  • Ok, thank you jflaflamme. So my question is that after looking at the link you provided. What about my script above. It is flawed or bad coding or is it just missing something fundamental? I just need to know so I can either work on the script above or start from scratch. Thank you.. – Arthor Jan 05 '12 at 14:03
  • Can you please answer to my three questions so me or somebody else can help – jflaflamme Jan 05 '12 at 14:08
  • Ok, well I am trying to get to the webs-erver error log. That my first step. The third question I can answer, there is nothing in the spam/junk. Will keep you updated.. – Arthor Jan 05 '12 at 14:16
  • Hi. Ok.. I think I have found the problem. The domain has be moved over to gamil to use their services. I logged in to the c-panel and found a mail box full of messages. "Mail delivery failed: returning message to sender". This explain now WHY the mails were not being sent because the MX records are pointing to the GMAIL System. – Arthor Jan 05 '12 at 14:27
  • So the question I have now. What is the best was to get the script above to send emails? Thank you – Arthor Jan 05 '12 at 14:28
  • Which domain, the sender or receiver ? If you mean you have a script on a server, trying to send an email to let say forms@domain.com. domain.com is on gmail, this is not a problem. For the sake of exemple, your script is sending from no-reply@domain.com to forms@domain.com. then, you should have : An SPF record in your DNS to permit the server IP + the "fifth parameter" set on the PHP mail function. – jflaflamme Jan 05 '12 at 14:35
  • The Domain which the server script is running on is the one where by all the MX records are point to GMAIL now. The recivers address is easy becuase I have server email address to test it with. This is the message in the mail box " This message was created automatically by mail delivery software. A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed: SMTP error from remote mail server after RCPT TO:: The account or domain may not exist, they may be blacklisted, or missing the proper dns entries – Arthor Jan 05 '12 at 14:41
  • Which means I have to edit the above to use the GMAIL account settings. I think that is on the right line? – Arthor Jan 05 '12 at 14:42
  • Yes, you can use your gmail's smtp info to send your mails from php, this is the preferred method and you don't have to bother with all the other settings. But keep in mind that you can send a maximum of 500 mails per day – jflaflamme Jan 05 '12 at 14:44
  • Ok, this is great, as a suggestion jflaflamme, should I create a new post indicating what I want to do or are you (and other people) able to point out to me in the code where to begin. Then it will be just trial and error I guess. Thank you jflaflamme for you support. – Arthor Jan 05 '12 at 14:47
  • Here you go, look at the accepted answer http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page – jflaflamme Jan 05 '12 at 14:48
  • Ok, I have been trying to get the mail function to work. Can you anybody provide some insight please. I have been trying for about 6 hours now, No joy :( – Arthor Jan 05 '12 at 21:10