mail function php returns false

mail function php returns false

Posted by: Guido.Mallee
Posted on: 2009-09-07 04:43:00

Hi,

On my website i have the following function:

function submit_contact()
{
$message =
'name: ' . $_POST['name'] . '
country: ' . $_POST['country'] . '
email: ' . $_POST['email'] . '
tel: ' . $_POST['tel'] . '
question: ' . $_POST['question'];

if (mail('info@bob-dive.com','You received a new question from the Bob-dive website.',$message))
redirect(base_url() . $_POST['alias'] . '/success');
else
redirect(base_url() . $_POST['alias'] . '/failed');
}

Somehow, the e-email never gets send and there is always redirected to the 'failed' page.

Any idea how this is possible and how i can solve this problem?

Thanks!

Re: mail function php returns false

Posted by: Atropos7
Posted on: 2009-09-07 10:49:00

In reply to:

if (mail('info@bob-dive.com','You received a new question from the Bob-dive website.',$message))


Oops, you didn't specify a 'From' message header.

See the PHP documentation on the mail() function

In reply to:

if (mail('info@bob-dive.com','You received a new question from the Bob-dive website.',$message))
redirect(base_url() . $_POST['alias'] . '/success');
else
redirect(base_url() . $_POST['alias'] . '/failed');
}


It is bad practice to include user input directly in output. Are you checking $_POST['alias'] against known and expected values?

# Initialize URL to redirect as an error page ("bad form") 
$redirect_url = base_url() . 'error';
if ($_POST['alias'] == 'contact') {
# contact form submitted, redirect back to contact page
$redirect_url = base_url() . 'contact';
if (mail()) {
# message was sent
$redirect_url .= '/success';
}
else {
# message wasn't sent
$redirect_url .= '/failed';
}
}
redirect($redirect_url);

Code like that is better because you are 100% sure what URL you are telling the browser to redirect to. Remember with Internet applications: NEVER TRUST USER INPUT. Which reminds me when you add the From header, do not include $_POST['email'] because then people could use your form page to send spam. Unless you happen to implement something like the function in this article: Validate an E-Mail Address with PHP, the Right Way


Customer since 2000 cool openvein.org

Tags: mail functionbase urlproblem thanksalias