47

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 = "abc@abc.com";
    $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

Rich
  • 5,603
  • 9
  • 39
  • 61
Learning
  • 19,469
  • 39
  • 180
  • 373
  • 1
    check your configuration – Rene Pot Jan 10 '12 at 13:28
  • Where do you use that? On local machine or on a server? – zozo Jan 10 '12 at 13:29
  • check with your host, many have disabled the mail() function for anti-spam purposes you might need to use smtp instead. –  Jan 10 '12 at 13:30
  • your server configuration. Not every server can is used as a mailserver – samn Jan 10 '12 at 13:31
  • The script looks ok. Also the sucess message suggest is a configuration problem... check your configuration... also check the configuration on the receiving server. And also... is like 90% chance that that mail go in spam... check that also. – zozo Jan 10 '12 at 13:31
  • Define "it does not work." Is the message reaching the mail server and being rejected there? Is it being filtered as spam? Check the mail server logs. – David Jan 10 '12 at 13:31

7 Answers7

73

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Prabhat Kashyap
  • 986
  • 1
  • 7
  • 11
  • 6
    Fixed for me, thank you. Was stuck on this for hours.. such a simple fix lol -- Just to add, I am using a `ovh` VPS. So if anyone in the future sees this, hello :) – NiCk Newman Jun 19 '16 at 02:34
  • in PHP code, it shows successfully sent but not actually in AWS EC2 – Shurvir Mori Jul 28 '23 at 13:56
20

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Orentet
  • 2,353
  • 1
  • 17
  • 28
  • 10
    How is downloading a whole bunch of files spread out over 8 folders, going over the documentation, downloading all the dependencies you need, setting all of them up along with their dependencies, etc, etc supposed to be easier than editing the ini file? – Daniel Bengtsson Nov 14 '19 at 14:12
  • 1
    @DanielBengtsson, since you didn't care to open the PHPMailer docs to read the reasoning why to use "a bunch of files", here's it is, directly from their documentation: "The only PHP function that supports this directly is mail(). However, it does not provide any assistance for making use of popular features such as encryption, authentication, HTML messages, and attachments. Formatting email correctly is surprisingly difficult. There are myriad overlapping (and conflicting) standards, requiring tight adherence to horribly complicated formatting and encoding rules" – b14r May 10 '22 at 10:17
6

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.

Robin Michael Poothurai
  • 5,444
  • 7
  • 23
  • 36
5

"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

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
2

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.

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
1

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

$headers = 'From: user@yourdomain.com' . " " .
'Reply-To: user@yourdomain.com' . " " .
'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: owner@domain.com, Reply-To: info@domain.com

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

kztd
  • 3,121
  • 1
  • 20
  • 18
1

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.

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46