php mail() function not working as it should on my box
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
php mail() function not working as it should on my box
Hi there
I've written a simple php script to send an email. As I'm using if/else statements the scripts indicates the mail reached the outgoing mail service but the email never arrives.
Sendmail is installed at /usr/sbin/sendmail -t -i according to my php.ini
I was told the mail log would give me valuable info on what happens each time I use the script. I located it in an ssh environment and when using pico .....(pico mail.log) it just gave a blank screen.
As a Linux newbie can anyone help me further in terms of seeing the mail.log etc.
The name of the mail system's logfile isn't simply "mail.log", it's usually something like:
/var/log/maillog
By the way, most of your system's logfiles are stored in /var/log. Can be a nice tip if you run into other problems too.
You may want to check some things like:
-is sendmail running? (use the command "/sbin/service sendmail status" or "ps -efl|grep -i sendmail"
-has sendmail been configured probably? By default, it accepts only mail originating from your host, which is fine. But does it know how to contact external mail servers to send the messages out? Other network protocols, like DNS may play a role in this too. Personnally, I stick with Postfix, which is similar to sendmail, but a lot easier to configure.
-do you know the mail server to which the mail is sent to? If you do, can you open a connection on port 25 (SMTP) on that machine, by using the command "telnet mail_name 25" or "telnet mail_IP 25" where "mail_name" and "mail_IP" need to be replaced by the mail server's full hostname and it's IP address respectively.
If you don't know that, please post the domain you're sending e-mails to (just take the e-mail address and eliminate anything before @ and the @ itself).
Can you also please post the exact PhP code and even the parts of php.ini that you think are relevant to sending out the mails?
Using /usr/sbin/sendmail -t -i to send out mails should indeed work, if sendmail can reach the destination's e-mail server.
Thanks for your reply …keeping in mind I’m a Linux newbie here’s what I found after your recommendations.
I wasn’t sure how to test whether sendmail is running. Is it that I just type
/sbin/service sendmail status at any point in an ssh environment? ………..
I did this and it came back with command not found
However what was interesting was that I tried to connect through port 25. Keeping in mind the ip of the box I’m working on is 217.10.135.14 I did telnet 217.10.135.14 25 in an ssh environment and it came back
trying 217.10.135.14
then after about 2 mins or so it came back
telnet: Unable to connect to remote host: Connection timed out
what does that mean?
Am right in saying there’s not an outgoing mail server on this box I’m working on?
For the record here’s the php code I’ve used
Code:
<?php
$mail_to="my email address";
$mail_from="from header";
$mail_sub="Emailing with php";
$mail_mesg="This is a test to see if I am able to email using php";
if(mail($mail_to,$mail_sub,$mail_mesg,"From:$mail_from/r/nReply-to:$mail_from")){
echo "E-mail has been sent successfully to $mail_to";
}
else{
echo "Failed to send the E-mail from $mail_sub to $mail_to";
}
?>
sorry I forgot to mention too that I don't have /var/log/maillog but I do have var/log/mail.log and it's that that's comes up blank when I open it in pico.
In hindsight, you may not need to have sendmail running. Calling it with -t -i immediately sends out the mail.
I'm not sure though.
In any case the "/sbin/service ..." command and the "ps -efl|grep -i sendmail" commands were indeed intended to be run while you're logged in via ssh.
The "command not found" error probably means that there's something wrong with your environment variables.
To resolve it, try the following commands (also after logging in via ssh):
whereis service
locate service
ls -l /sbin/service
If I understood you correctly, the machine you login to has IP 217.10.135.14, no?
If telnet sais you can't connect to port 25 (the time out after a few seconds/minutes is normal if the
connection can't be established), then this means that sendmail isn't listening to port 25 (the SMTP - simple mail transfer protocol- port, used for sending e-mails). This would mean that no other computers can send e-mail to the machine with IP 217.10.135.14, which is just fine (you probably don't want your webserver to act as an e-mail server too - ie the web server doesn't have to actually deliver mails that have come in from other computers to it's own users or to relay mails to other computers on your network - those 2 tasks are what a mail server typically does).
By default, sendmail doesn't listen to the normal network interface (the network interface that has IP 217.10.135.14 in your case), but to the localhost interface. This localhost interface is a special interface, called the loopback interface, that allows programs that are running on a host to contact services on the same host (hence the name "loopback" - the connection "loops" back to the same host). This localhost interface is only known on the machine itself and each computer has it's own loopback interface.
Sendmail listens to that interface by default for security reasons. This implies that sendmail will only send out mails that were created on the machine itself. The loopback interface always has IP 127.0.0.1.
To make a long story short, you could try:
telnet localhost 25
or
telnet 127.0.0.1 25
If both of those respond with a time out, then there is no mail server (ie sendmail) running and listening to port 25 for new mails.
This simply means that there is a mail server, probably sendmail, listening to port 25 on the localhost interface.
Actually, when doing the telnet, you could actually send mails if you would type some simple commands in the SMTP protocol (described in an official document, called an RFC, and the protocol itself is really simple).
But that's not what you were trying to do.
In any case, your sendmail is running and it will accept e-mails that you want to send out.
If you didn't alter the default sendmail configuration, this means that the mail logfile should contain any
e-mail related error messages.
Example:
-send a mail via PhP
-then type:
for i in `ls /var/log/*`; do echo ${i}; tail -20 ${i}; done
This displays the last 20 lines for each of your files in /var/log.
You should find a message related to the queueing of your message (if it was queued).
You could also try the "mailq" command (when logged in, after sending a mail via PhP).
Does php.ini say anything about the use of SMTP?
Also, to what domain are you sending e-mails? Can you connect to port 25 of the mail server of that domain?
If that fails, then sendmail can't contact the remote domain's mail server and it simply can't deliver the mail you've created. PhP may not indicate this as a failure, since sendmail will accept the mail given by PhP, it will simply not send it out but keep it in the queue for a while.
Trying 127.0.0.1...
Connected to localhost.localdomain.
Escape character is '^]'.
220 tms1.theinternetgroup.net ESMTP Exim 4.50 Thu, 16 Feb 2006 16:52:04 +0000
So everything seems fine.......
any other ideas........the original prob is that while my php script suggests the mail successfully reaches the outgoing mail service it never arrives
I don't have /var/log/exim/ but I do have /var/log/exim4 when I tried less /var/log/exim4/main.log I get permission denied so I guess I can't take it any further at this stage?
Kind of obvious things to try would be to make sure you can receive email on your hotmail account from another known good email account. Then, try to change the recipient address in the php to the known good account instead of hotmail.
A slightly more difficult thing would be to set up a manual SMTP session with the mail server, but some admins don't like this because it's kind of sneaky and there's potential for abuse. You took the first step in doing this by doing "telnet localhost 25".
The best route would be to try to have an admin give you access to the mail logs, and the web server error log so you can "tail -f" the files and see what's going on in real-time when the php code is called.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.