Forum Moderators: coopster

Message Too Old, No Replies

send mail - two email - one good, one empty

Problem with PHP send mail

         

purmar

7:22 pm on May 26, 2012 (gmt 0)

10+ Year Member



Hello!

I have limited PHP experience not going over arrays, include, and some basis stuff. I have been testing and testing for ever this script and have no idea why it sends regular email together with another one empty. Any idea?

<?
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$subject = $_POST['subject'] ;
$message = $_POST['message'] ;

$main = 'From: ' . $name . ' ('. $email .')<br>' . $phone . '<br>' . $subject . '<br>';

$main .= '<br>';
$main .= 'Message: ' . $message . '<br>';
$main .= '<br>';

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:'. $email . "\r\n" ;

mail('info@company.com', 'From website', $main, $headers); //mail command :)
?>


I get this:

From: mark p (test@test.com)
1231231234
General Inquiry

Message: 3.12 time


and this :

From: ()



Message:


Could ANYBODY tell me how to stop the stupid second empty email or why this is happening?

Thank you

CoursesWeb

5:29 am on May 27, 2012 (gmt 0)

10+ Year Member



Hi
The cause of the seccond email, empty, can be because that page is accessed again by google bot, but without POST data.
To solve it, try this, to send mail only if valid data from POST:

if(isset($_POST['name']) && strlen($_POST['name'])>1} {
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$phone = $_POST['phone'] ;
$subject = $_POST['subject'] ;
$message = $_POST['message'] ;

$main = 'From: ' . $name . ' ('. $email .')<br>' . $phone . '<br>' .$subject . '<br>';

$main .= '<br>';
$main .= 'Message: ' . $message . '<br>';
$main .= '<br>';

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:'. $email . "\r\n" ;

mail('info@company.com', 'From website', $main, $headers);
}

purmar

1:39 pm on May 28, 2012 (gmt 0)

10+ Year Member



Great, thanks. That seems to fixed the issue.

I was thinking that it may be the issue of somebody accessing the page directly, though the email was usually always sent together with the "real" one....

In any case I have not received any empty email for 2 days now!