DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

PHP mail() function

By: | 24 Jul 2009 11:50 am

Hello,

Please review the script posted below this message. I am using this script to send the results of an HTML form to an email. The only problem: This script works only about 1 out of 10 times that I use it. Not all messages through the contact form are reaching my inbox and are ending up somewhere in limbo!?!?!?!
Does anyone have a better method of sending form results to email that works 100% of the time. PLEASE LET ME KNOW!

I thank you all for your consideration and help in this matter.
Thanks,

--------------------------------------------------------- sample script is

listed below this line
------------------------------------------------------------
<?
# RegisterGlobals OFF
$name = $_POST[`NAME`];

$email = $_POST[`EMAIL`];

$phone = $_POST[`PHONE`];

$message = $_POST[`MESSAGE`];

$submit = $_POST[`SUBMIT`];
# Email Body
$body_form_email = "Name: $namen"
. "Name: $namen"

. "Email: $emailn"

. "Phone: $phonen"

. "Message: $messagen"

. "Submit: $submitn"

. "n"

. "";
# Send the email to the form owner
mail("somebody@...", "Message from somesite.com",
$body_form_email, "From: $email");

# Redirect user to FAIL page
header("Location: http://www.somesite.com/messagesent.htm");

exit;

?>

Comments

I would point my finger at your header, a invalid header will raise the spam flag a lot of points so if the body gets a few more points from the anti spam software the msg would be deleted.

Here is the code I use to build my emails. It will not be filtered by yahoo mail or google mail.
I don`t add From: so the mail is sent by apache but you can add that line below. You may also want to change the charset= option. All my sites run in UTF8, not sure about yours.

$headers = "Content-Type: text/plain; "
. "charset=UTF-8; format=flowedn"

. "MIME-Version: 1.0n"
. "Content-Transfer-Encoding: 8bitn"
. "X-Mailer: PHPn";
$body = "The msg to be sent out goes in here";
mail(someone@... ,"A message from...",$body,$headers)

I also have a script which will talk to a SMTP server directly but it does not work on lot of servers due to safe_mode restrictions.

By: | 24 Jul 2009

Hello

I don`t see any reason fo it to fail 9 times out of 10.

The only suspicious line is this
mail( , , , "From: $emailrn");
I would normaly expect to see a "rn" after headers.

Also no valoidation! Dangerious!

Look in your error log, you can also alter php.ini so that you see warnings and not just errors.

Here is a working script that I wrote - perhaps it helps? It is long but that is nessesary to keep out abusers.

<?php

$business_address = `Bussiness name<info@...>`;
$redirect_url = `thankyou.html`;
$keys =
`Name|Company|address1|address2|site_address1|site_address2|Phone|fax|email`;
$keys .= `|private_or_agent|Inquiry|Method_of_discovery`;

if(isset($_POST[`SendButton`]))
{
$keys = explode(`|`, $keys);
import_post($keys);
if($phone == ``)
{
$error = `your phone number was not entered.`;
}
if($name == ``)
{
$error = `your name was not entered.`;
}
if(!valid_email($email))
{
$error = `your e-mail address is not valid.`;
}
if(!isset($error))
{
$message = file_get_contents(`message.txt`);
foreach($keys as $key)
{
$thiskey = strtolower($key);
$searchtext = `[` . $thiskey . `]`;
$replacetext = $$thiskey;
$message = str_replace($searchtext, $replacetext, $message);
}
$message = wordwrap($message, 70);
$client_address = $name . `<` . $email . `>`;
$subject = `Some Subject`;
$headers .= `X-Mailer: PHP/` . phpversion() . "rn";
$headers .= `MIME-Version: 1.0` . "rn";
// The three lines below are for HTML with inline images
//$headers .= `Content-Type: multipart/related;` . "rn";
//$headers .= "t" . `type="multipart/alternative";` . "rn";
//$headers .= "t" . `boundary="----=_NextPart_000_0006_01C9A3DF.462FC4E0"`;
$business_headers = $headers . `From: ` . $client_address . "rn";
$client_headers = $headers . `From: ` . $business_address . "rn";
@mail($business_address, $subject, $message, $business_headers);
@mail($client_address, $subject, $message, $client_headers);
redirect_to($redirect_url);
die();
}
$error = `Your message was not sent because ` . $error;
}

function valid_email($emailaddress)
{
if(strlen($emailaddress) < 7) return FALSE;
if(strspn(`.@`, $emailaddress) != 2) return FALSE;
$accept = `abcdefghijklmnopqrstuvwxyz`;
$accept .= strtoupper($accept);
$accept .= `0123456789`;
$accept .= `._-@`;
if(strlen($emailaddress) != strspn($emailaddress, $accept))
{
return FALSE;
}
return TRUE;
}

function import_post($keys)
{
foreach($keys as $key)
{
$post = $_POST[$key];
$thiskey = strtolower($key);
if($thiskey == `inquiry`)
{
$post = str_replace("rn", "r", $post);
$post = str_replace("r", "n", $post);
$post = str_replace("n.n", "n..n", $post);
$post = str_replace("n", `[#LF#]`, $post);
}
while($post != stripslashes($post))
{
$post = stripslashes($post);
}
if($thiskey == `inquiry`)
{
$post = str_replace(`[#LF#]`, "n", $post);
}
$post = trim($post);
global $$thiskey;
$$thiskey = $post;
}
}

function redirect_to($basename)
{
if(basename($_SERVER[`PHP_SELF`]) != $basename)
{
$location = $_SERVER[`HTTP_HOST`];
$location .= rtrim(dirname($_SERVER[`PHP_SELF`]), `/`);
$location .= `/` . $basename;
header(`Location: http://` . $location);
die();
}
die();
}

?>

<html>
<head>
</head>

<body>
<?php echo($error); ?>
<form action="<?php echo($_SERVER[`PHP_SELF`]); ?>" method="post">

<input id="Name" type="text" name="Name" value="<?php echo($name); ?>" size="26"
maxlength="80" />

<input id="Company" type="text" name="Company" value="<?php echo($company); ?>"
size="26"
maxlength="80" />

<input id="address1" type="text" name="address1" value="<?php echo($address1);
?>" size="26"
maxlength="80" />

<input id="address2" type="text" name="address2" value="<?php echo($address2);
?>" size="26"
maxlength="80" />

<input id="site_address1" type="text" name="site_address1" value="<?php
echo($site_address1); ?>"
size="26" maxlength="80" />

<input id="site_address2" type="text" name="site_address2" value="<?php
echo($site_address2); ?>"
size="26" maxlength="80" />

<input id="Phone" type="text" name="Phone" value="<?php echo($phone); ?>"
size="26" maxlength="20"

/>
<input id="fax" type="text" name="fax" value="<?php echo($fax); ?>" size="26"

maxlength="20" />
<input id="email" type="text" name="email" value="<?php echo($email); ?>"

size="26" maxlength="40"
/>
<select name="private_or_agent" size="2" id="private_or_agent">

<option value="For Me"<?php if($private_or_agent == `For Me`) {echo(`
selected="selected"`);}
?>>For Me</option>
<option value="For an Agent"<?php if($private_or_agent == `For an Agent`)
{echo(`
selected="selected"`);} ?>>Agent</option>

</select>
<textarea id="Inquiry" name="Inquiry" rows="17" cols="35"><?php echo($inquiry);
?></textarea>
<select name="Method_of_discovery" id="Method_of_discovery">

<option value="-"<?php if($method_of_discovery == `-`) {echo(`
selected="selected"`);}
?>>Choose one</option>
<option value="Referred by previous client"<?php if($method_of_discovery
== `Referred by
previous client`) {echo(` selected="selected"`);} ?>>Referred by previous
client</option>

<option value="Link from another site"<?php if($method_of_discovery ==
`Link from another
site`) {echo(` selected="selected"`);} ?>>Link from another site</option>
<option value="Yellow pages"<?php if($method_of_discovery == `Yellow
pages`) {echo(`
selected="selected"`);} ?>>Yellow pages</option>

<option value="White pages"<?php if($method_of_discovery == `White pages`)
{echo(`
selected="selected"`);} ?>>White pages</option>
<option value="Search engine"<?php if($method_of_discovery == `Search
engine`) {echo(`
selected="selected"`);} ?>>Search engine</option>

<option value="Other"<?php if($method_of_discovery == `Other`) {echo(`
selected="selected"`);}
?>>Other</option>
</select>
<input name="SendButton" type="submit" id="SendButton" value="Submit" />

</form>
</body>
</html>

By: | 24 Jul 2009

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move