1

I have a little problem with the following code. When I click "Send" it says "PHP Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:***.com**\mail.php on line 21"

This is the jQuery:

$(document).ready(function(){
$("#send").click(function(){


    var valid = '';
    var isr = ' requested.</h6>';
    var name = $("#name").val();
    var mail = $("#email").val();
    var messaggio = $("#message").val();

    if (name.length<1) {
        valid += '<h6>A valid name is'+isr;
    }
    if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
        valid += '<h6>A valid e-mail address is'+isr;
    }


    if (valid!='') {
        $("#re").fadeIn("slow");
        $("#re").html("<h6><b>Error:</b></h6>"+valid);
        $("#re").css("background-color","#ffc0c0");
    }

    else {
        var datastr ='name=' + name + '&mail=' + mail + '&messaggio=' + encodeURIComponent(messaggio);
        $("#re").css("display", "block");
        $("#re").css("background-color","#FFFFA0");
        $("#re").html("<h6>Sending..</h6>");
        $("#re").fadeIn("slow");
        setTimeout("send('"+datastr+"')",2000);
    }
    return false;
});
});

function send(datastr){
    $.ajax({    
        type: "POST",
        url: "mail.php",
        data: datastr,
        cache: false,
        success: function(html){
        $("#re").fadeIn("slow");
        $("#re").html(html);
        $("#re").css("background-color","#e1ffc0");
        setTimeout('$("#re").fadeOut("slow")',2000);
    }
    });
}

And the PHP

<?php
$mail = $_POST['mail'];
$name = $_POST['name'];
$text = $_POST['messaggio'];
$ip = $_SERVER['REMOTE_ADDR'];

$to = "admin@****.com";

$message = "E-mail received from: ".$name.", ".$mail.".<br />";
$message .= "Messaggio: <br />".$text."<br /><br />";
$message .= "IP: ".$ip."<br />";
$headers = "From: $mail \n";
$headers .= "Reply-To: $mail \n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";

 if(mail($to, $message, $headers)){
    echo "<h6>Message sent.</h6>";
}

else{ 
    echo "<h6>Ops! We've got a problem! :(</h6>";
}
?>

The mentioned line is "if(mail($to, $message, $headers)){" but I don't understand the error.

I am using the same code on another website (another domain also) and it runs without problems.

3
  • You shouldn't be generating your own mime messages. Use PHPMailer or Swiftmailer (google for them) and let them handle the details. Getting email headers right is tricky, and you're not generating them properly. Commented Apr 27, 2012 at 16:14
  • does ini_set("sendmail_from","[email protected]"); work? Commented Apr 27, 2012 at 16:15
  • 1
    In addition, try using \r\n instead of just \n - it's supposed to be both a CR and a LF for header separation Commented Apr 27, 2012 at 16:15

4 Answers 4

1

Headers need to be terminated with \r\n not \n.

$headers = "From: $mail \r\n"; 
$headers .= "Reply-To: $mail \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \r\n";

Also there is a Header Injection vulnerability in your code. You should validate all of the post data that you put into email headers.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you checked the SMTP Settings in PHP.INI?

Set this in your code or in your php.ini:

  <?php 
     ini_set("SMTP","smtp-server-address" ); 
     ini_set('sendmail_from', 'from-email-address'); 
   ?> 

And if you are sending through a different port than 25, you can also override the default:

   ini_set('smtp_port', '2525'); 

1 Comment

That was the problem. Thank you and everyone for trying.
0

You might try replacing your FROM headers with:

$headers = "From: $name <$mail>\r\n";

designating the email address within lt/gt brackets.

One other thought is to be sure the $_POST['mail'] value is indeed being passed.

Comments

0

I think you might have a carriage return coming from your field. You need to scrub your data input in the $_POST like:

$mail = trim($_POST['mail']);

You might also want to use mysql_real_escape_string or something similar in regexp if inputting into a DB.

Also for consistantcy in your code:

$headers = "From: ".$mail." \r\n";
$headers .= "Reply-To: ".$mail." \r\n";

Also I think your missing the correct mail parameters. According to the mail documentation your missing a subject parameter:

mail($to, $subject, $message, $headers);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.