Learning
Learning

Reputation: 20051

PHP mail not working for some reason

I am new to PHP and I'm using the mail function to send emails which is not working. I get a success message, but still it does not work

same code

<?php
    $email_to = "[email protected]";
    $email_subject = "Test mail";
    $email_body = "Hello! This is a simple email message.";


    if(mail($email_to, $email_subject, $email_body)){
        echo "The email($email_subject) was successfully sent.";
    } else {
        echo "The email($email_subject) was NOT sent.";
    }
?>

Am I missing anything, do I need to include any files for this function.. I am from asp.net & this is the basic script which found on website.

I tried other scripts related to mail they didn't work either..

I AM RUNNING THIS SCRIPT ON THE WEBSITE NOT on the localhost

Upvotes: 48

Views: 191007

Answers (7)

Prabhat Kashyap
Prabhat Kashyap

Reputation: 1016

If you are using Ubuntu and it seem sendmail is not in /usr/sbin/sendmail, install sendmail using the terminal with this command:

sudo apt-get install sendmail

and then run reload the PHP page where mail() is written. Also check your spam folder.

Upvotes: 75

Orentet
Orentet

Reputation: 2363

This is probably a configuration error. If you insist on using PHP mail function, you will have to edit php.ini.

If you are looking for an easier and more versatile option (in my opinion), you should use PHPMailer.

Upvotes: 21

kztd
kztd

Reputation: 3405

For HostGator, you need to use the following for your headers:

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

It only worked for me when the from user was host e-mail while the Reply-To can be something different e.g. From: [email protected], Reply-To: [email protected]

http://support.hostgator.com/articles/specialized-help/technical/php-email-from-header http://support.hostgator.com/articles/specialized-help/technical/how-to-use-sendmail-with-php

Upvotes: 1

Robin Michael Poothurai
Robin Michael Poothurai

Reputation: 5664

This might be the issue of your SMTP config in your php.ini file.

Since you new to PHP, You can find php.ini file in your root directory of PHP installation folder and check for SMTP = and smtp_port= and change the value to

SMTP = your mail server e.g) mail.yourdomain.com
smtp_port = 25(check your admin for original port)

In case your server require authentication for sending mail, use PEAR mail function.

Upvotes: 6

Piskvor left the building
Piskvor left the building

Reputation: 92792

"Just because you send an email doesn't mean it will arrive."

Sending mail is Serious Business - e.g. the domain you're using as your "From:" address may be configured to reject e-mails from your webserver. For a longer overview (and some tips what to check), see http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html

Upvotes: 5

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26749

The mail function do not guarantee the actual delivery of mail. All it do is to pass the message to external program (usually sendmail). You need a properly configured SMTP server in order for this to work. Also keep in mind it does not support SMTP authentication. You may check out the PEAR::Mail library of SwiftMailer, both of them give you more options.

Upvotes: 2

TJHeuvel
TJHeuvel

Reputation: 12618

Check your SMTP settings in your php.ini file. Your host should have some documentation about what credentials to use. Perhaps you can check your error log file, it might have more information available.

Upvotes: 1

Related Questions