Reputation: 327
I wrote a script such that if the form fails, the user sees an error message and an email should be sent to me. However, I'm not getting the email. I don't see any mistakes in my script and the email is nowhere to be found (not even in spam). What could be the issue? I believe we are working on a Linux server, so a Mail agent should be installed by default, right? Could a firewall be blocking the messages?
The code:
<?php
if (isset($aErrors) && count($aErrors) > 0 ) {
print '<ul class="errorlist">';
foreach ( $aErrors as $error ) {
print '<li>' . $error . '</li>';
}
print '</ul><br />';
$mail_ontv = "[email protected]";
$_POST['onderwerp'] = "Geen Twinfield Abonnement";
// set datum
$datum = date("d.m.Y H:i");
// set ip
$ip = $_SERVER['REMOTE_ADDR'];
$inhoud_mail .= $_SERVER['SCRIPT_URI'] . "\n\n";
$inhoud_mail .= "Iemand probeerde zich aan te melden, maar had geen Twinfield abonnement!\n";
$inhoud_mail .= "Bedrijfsnaam: " . $_SESSION['bedrijfsnaam'] . "\n";
$inhoud_mail .= "Telefoonnummer: " . $_SESSION['telefoonnummer'] . "\n";
$inhoud_mail .= "E-mail adres: " . $_SESSION['email'] . "\n";
$inhoud_mail .= "Telefoonnummer contactpersoon: " . $_SESSION['telefoonnummercontact'] . "\n";
$inhoud_mail .= "E-mail adres contactpersoon: " . $_SESSION['emailcontact'] . "\n";
$inhoud_mail .= "Gewenste gebruikersnaam: " . $_SESSION['gebruikersnaam'] . "\n";
$inhoud_mail .= "Gekozen abonnement: " . $_SESSION['abonnement'] . "\n\n";
$inhoud_mail .= "Verstuurd op " . $datum . " via het ip " . $ip . "\n\n";
$headers = "From: " . $_SESSION['bedrijfsnaam'] . " <" . $_SESSION['emailcontact'] . ">";
$headers = stripslashes($headers);
$headers = str_replace("\n", "", $headers); // Verwijder \n
$headers = str_replace("\r", "", $headers); // Verwijder \r
$headers = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $headers)); // Slashes van quotes
$_POST['onderwerp'] = str_replace("\n", "", $_POST['onderwerp']); // Verwijder \n
$_POST['onderwerp'] = str_replace("\r", "", $_POST['onderwerp']); // Verwijder \r
$_POST['onderwerp'] = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $_POST['onderwerp'])); // Slashes van quotes
mail($mail_ontv, $_POST['onderwerp'], $inhoud_mail, $headers);
}
?>
Thanks guys!
Upvotes: 2
Views: 233
Reputation: 9891
Several extra thoughts for you beyond what Mac B suggested:
1) There are many PHP libraries out there that will take care of sending an email for you. They can work with a local or a remote SMTP server. For example, http://swiftmailer.org/.
2) Most email providers these days (Gmail, Yahoo, etc) will spam-block email that comes from just your local computer. I recommend using a cheap third-party SMTP service to send your emails - this will help with reliability. One I've been using for years is called CritSend (critsend.com). There are many others.
Upvotes: 5
Reputation: 360812
Did you check the return value of mail()
? If you get a boolean fALSE, something blew up while PHP was trying to handle the email over to your SMTP server.
If mail returns true, then the handoff was successful, and you'll have to go look at the SMTP server's logs to find out what happened to the email from that point.
Remember: PHP's involvement with sending email, in a real world equivalent, is taking the envelope and dropping it into a mailbox. That's it. Everything else is completely outside of PHP's purview and control.
Upvotes: 2