rkhealey
rkhealey

Reputation: 1

PHP mail() not working help me please

can someone please help me figure out why this isn't sending? I'm new to PHP.

It's for a contact form on my website. I'm sending from my server (not localhost) and receiving the console message "Mail not sent"

<?php

include 'ChromePhp.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

   $to = '[email protected]';
   $subject = 'Email from Me';
   $message = $_POST['email'];

     if(mail($to, $subject, $message)){
        ChromePHP::log( 'Mail Sent');
     }else{
        ChromePHP::log( 'Mail Not Sent');
     }
}

?>

I realise this question has been asked before, but I can't find an answer that works. Thanks in advance!

Upvotes: 0

Views: 125

Answers (4)

kwelsan
kwelsan

Reputation: 1219

You can ask to your hosting provider to activate mail services because some hosting provider does not allow to use mail in default.

Upvotes: 0

si le
si le

Reputation: 104

You should include header to mail() function, example

$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

then insert $headers parameter to mail() function:

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

Upvotes: 2

thordarson
thordarson

Reputation: 6261

Without an error message, I can only guess. You probably need to set the SMPT server in your php.ini. You can do that at runtime using ini_set('SMTP', 'your_server_here').

More about the mail configuration here: http://php.net/manual/en/mail.configuration.php

If you don't have one, you can check if your ISP has an open server. ISPs sometimes have open SMTP servers (outgoing servers) and you can find them by looking for instructions on how to set up your mail program (Outlook).

Be careful though, and don't use this for anything but light testing. I can imagine they won't like a lot of spam going through their server.

EDIT: Also, read up on the headers required. I see you're missing a From header, which should result in a warning. See here: http://php.net/manual/en/function.mail.php

When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.

Upvotes: 1

You should check your mail services, because above PHP code quite true.

Upvotes: 0

Related Questions