Reputation: 4509
I'm trying to send an email from my website, I put a correct name and email, but it's not sending it.. May you hel me?
The URL is being parsed, and the html is well comunicated with php. I put this lines
echo $_POST["userName"].' y ';
//(to know if the name is getting to the php)
echo $_POST["userEmail"];
//(to know if the email is getting to the php)
and it's working correctly... maybe the error is in the mail line or something.
This is my PHP Code
elseif ($page_name=='contact-post.html') {
if($_POST["userName"]!='' && $_POST["userEmail"]!=''){
echo $_POST["userName"].' y ';
//(to know if the name is getting to the php
echo $_POST["userEmail"];
//(to know if the email is getting to the php
$nombre = $_POST["userName"];
$correo = $_POST["userEmail"];
$asunto = "Mensaje de ".$nombre;
$mensaje = $_POST["userMsg"];
$correoMarsur = "[email protected]";
if(mail($correoMarsur,$asunto,$mensaje,$correo)){
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Mensaje enviado')
window.location.href='http://page.cl';
</SCRIPT>");
}
else{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Imposible enviar su mensaje')
window.location.href='http://page.cl';
</SCRIPT>");
}
}
else{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Ingrese Nombre y Correo')
window.location.href='http://page.cl';
</SCRIPT>");
}
}
and This is my HTML code
<div class="form">
<!--<form method="post" action="/index.php/contact-post.html"> -->
<form method="post" action="../index.php/contact-post.html">
<input name="userName" id="userName" type="text" class="textbox" placeholder="Nombre*" />
<input name="userEmail" id="userEmail" type="text" class="textbox" placeholder="Email*" />
<div class="clear"> </div>
<div>
<textarea name="userMsg" id="userMsg" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Mensaje* ';}">Mensaje*</textarea>
</div>
<div class="submit">
<input type="submit" value="Enviar " />
</div>
</form>
</div>
And the mail boolean is returning false, How can I fix it?
Upvotes: 1
Views: 101
Reputation: 157
//SEND MAIL FUNCTION
function sendMail($to, $subject, $body, $headers)
{
if (mail($to, $subject, $body, $headers)) {
$returnMessage = "Success";
}
else {
$returnMessage = "Failed";
}
return $returnMessage;
}
//SET YOUR POST VARIABLES HERE
$name = ($_POST['userName']);
$email = ($_POST['userEmail']);
//SET YOUR EMAIL DETAILS HERE, HOWEVER THIS CAN BE PASSED IN FROM ELSEWHERE, HENSE THE FUNCTION
$to = "[email protected]";
$subject = "Some subject";
$body = ("Name: " . $name . "\nEmail Address: " . $email . "\nSome other text you may want in your subject etc");
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]';
// CALL FUNCTION
sendMail($to, $subject, $body, $headers);
It is good practice to use functions to promote reusability of code. This mail script can now be used for more than just this contact form and it saves you re-writing the whole thing again.
If this doesn't work (I have tested this and it works), it may possibly be your servers SendMail, to check this is enabled you can create a PHPInfo and upload that to your FTP to check if sendmail is enabled.
PHP Info:
<? phpinfo() ?>
I hope this helps, Dan
Upvotes: 0
Reputation: 6379
You have put the user email into the additional_headers parameter slot in the mail()
function, thats why it returns false.
This are your parameters you have to fill in :
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
For example:
mail($correoMarsur,$asunto,$mensaje);
This should work, except if you have weird data in your variables.
Upvotes: 1