Mail function php returns false

software development

#1

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!


#2

Oops, you didn’t specify a ‘From’ message header.

See the PHP documentation on the mail() function

[quote]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’);
}[/quote]
It is bad practice to include user input directly in output. Are you checking $_POST[‘alias’] against known and expected values?

[code]# 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]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