/    Sign up×
Community /Pin to ProfileBookmark

mail() function, crazy results, problems

I recently built a contact form for a client and am putting the data into a table and then sending the data to 3-5 recepients using the mail function.

the data is being put into a table no problem whatsoever. but with the mail() function i am getting crazy, mixed results.

ex: I get the mail right away at all 3 of my mail accounts (on diff hosts). my client is not getting them at all, and a test subject, received an email at today at 2:20 a.m. that was sent on Friday afternoon!

i don’t know what to make of this?

are there any know issues with this function?, or any alternatives to using it.
the client is on a commercial shared hosting provider. i’ll include the code just in case it’s on my end.

[code=php]<?php
if (isset($_POST[‘submit’])) {
echo “<p>submitted</p>”;

$name = $_POST[‘name’];
$email = $_POST[’email’];
$subject = $_POST[‘subject’];
$message = $_POST[‘message’];
$date = date(“m/d/y”);

$mail_to_r1 = ‘[email protected]’;
$mail_to_r2 = ‘[email protected]’;
$mail_to_r3 = ‘[email protected]’;
$esubject = ‘New Mail from Contact Form’;
$emessage = ‘From: ‘ . $name . “n”;
$emessage .= ‘Email: ‘ . $email . “n”;
$emessage .= ‘Subject: ‘ . $subject . “n”;
$emessage .= “Message:n” . $message . “nn”;

// $name = $_POST[‘name’];

if ($name != “” && $name != “Name:”) {
$name = filter_var($_POST[‘name’], FILTER_SANITIZE_STRING);
if ($name == “”) {
$errors .= ‘Please enter a valid name.<br/><br/>’;
echo $errors;
}
} else {
$errors .= ‘Please enter your name.<br/>’;
echo $errors;
}

if ($email != “” && $email != ‘Email:’) {
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= “$email is <strong>NOT</strong> a valid email address.<br/><br/>”;
}
} else {
$errors .= ‘Please enter your email address.<br/>’;
}

if ($subject != “” && $subject != ‘Subject:’) {
$subject = filter_var($subject, FILTER_SANITIZE_STRING);
if ($subject == “”) {
$errors .= ‘Please enter a Subject.<br/>’;
}
} else {
$errors .= ‘Please enter a Subject.<br/>’;
}

if ($message != “” && $message != ‘Message:’) {
$message = filter_var($message, FILTER_SANITIZE_STRING);
if ($message == “”) {
$errors .= ‘Please enter a Message.<br/>’;
}
} else {
$errors .= ‘Please enter a Message.<br/>’;
}

//echo “This is the $name”;

$connection = mysql_connect(“localhost”, “root”, “password”);

if(!$connection) {
die(“Database connection failed: ” . mysql_error());
} //else {echo “<p>connection successful</p>”;}

//2. Select a database to use
$db_select = mysql_select_db(“database”, $connection);

if(!$db_select) {
die(“Database selection failed: ” . mysql_error());
} //else {echo “<p>connection successful</p>”;}

$query = “INSERT INTO contact_form (name, email, subject, message, date) VALUES (‘$name’, ‘$email’, ‘$subject’, ‘$message’, ‘$date’)”;

if(!$errors) {
mail($mail_to_r1, $esubject, $emessage);
mail($mail_to_r2, $esubject, $emessage);
mail($mail_to_r3, $esubject, $emessage);
header(“Location: http://www.whatever.com/sent”);
} else {
//echo “<p>There were errors</p>”;
//echo $errors;
header(“Location: http://www.whatever.domain.com/email-error.php”);
}
} // END IF SUBMIT
?>[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@dukesdemiseMay 08.2012 — If you're on a unix, linux, or an osx system try sending the email using the "mail" command line program. Type "man mail" for command options. That would isolate whether it is your particular application thats is having problems, or if it is your client's server that is having problems with the email or the domain from which you are sending it from.
Copy linkTweet thisAlerts:
@rtretheweyMay 08.2012 — I see where the script sends three Emails, which sounds like they're your 3 Email accounts, but I don't see anywhere it sends the message to your client. Did you try changing one of the Email addresses to your client's address?

It's (barely) possible that three calls to mail() in a single script could cause the script to exceed the server's time limit. It would be better to include a 'bcc:' field in the header to send the message to multiple addresses with a single call to mail().

It's not that unusual for Email generated by a script to be slow to arrive. Mail servers often classify such messages as SPAM or borderline SPAM and hold them in their queue for later processing. Similarly, hosting services often queue outgoing Email if they get busy or a single account is sending a lot of messages.

I expect that with a few minor changes and some patience, you'll get this all to work for you.
Copy linkTweet thisAlerts:
@toptomatoauthorMay 08.2012 — thanks, i will try both
×

Success!

Help @toptomato spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...