LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 02-16-2006, 05:44 AM   #1
juancha
LQ Newbie
 
Registered: Feb 2006
Posts: 17

Rep: Reputation: 0
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.


thanks


Juan
 
Old 02-16-2006, 05:54 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 02-16-2006, 06:57 AM   #3
juancha
LQ Newbie
 
Registered: Feb 2006
Posts: 17

Original Poster
Rep: Reputation: 0
Hi Timeke

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";
}
?>
And here's the path to sendmail in my ini file

/usr/sbin/sendmail -t -i

hope you or anyone else can help
 
Old 02-16-2006, 07:01 AM   #4
juancha
LQ Newbie
 
Registered: Feb 2006
Posts: 17

Original Poster
Rep: Reputation: 0
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.
 
Old 02-16-2006, 07:23 AM   #5
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 02-16-2006, 07:53 AM   #6
juancha
LQ Newbie
 
Registered: Feb 2006
Posts: 17

Original Poster
Rep: Reputation: 0
Hi thanks again for coming back to me....

I did telnet localhost and I was able to connect

telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.localdomain.

Is there anything you can deduce from that
 
Old 02-16-2006, 08:43 AM   #7
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 02-16-2006, 10:01 AM   #8
juancha
LQ Newbie
 
Registered: Feb 2006
Posts: 17

Original Poster
Rep: Reputation: 0
Hi there

Many thanks for your help so far.

As for the domains I'm sending email to, it's my hotmail account so it can't be it's not accepting incoming mails....

When I do mailq I get permission denied....by the way I've not got root access...

With for i in `ls /var/log/*`; do echo ${i}; tail -20 ${i}; done

I get an error .....must be my fault doing something wrong.

I have this setup when I look var/log/mail.log and var/log/mail.info and var/log/mail.warn

Can you tell me another way to access any of these files in an ssh environment.......that would suit me for now.

As for my .ini file I've got SMTP on localhost adn smtp_port on 25

Hope you can help
 
Old 02-16-2006, 10:42 AM   #9
joeface
LQ Newbie
 
Registered: Aug 2003
Location: NJ
Distribution: Fedora
Posts: 17

Rep: Reputation: 0
The mail log isn't showing up because it's permissions may only allow root to read it.

If you do "ls -l /var/log/mail.log", you'll see something like this:

-rw------- 1 root root 12345 Feb 16 10:37 mail.log

The "-rw-------" means that only root can read from or write to that file. If you don't have root access, you're out of luck.
 
Old 02-16-2006, 11:04 AM   #10
juancha
LQ Newbie
 
Registered: Feb 2006
Posts: 17

Original Poster
Rep: Reputation: 0
Hi a big thanks for that

I did it and got

-rw-r--r-- 1 root root 0 2006-01-11 16:58 /var/log/mail.log

which I reckon means I should be able to read it.....can you or anyone else tell how I would do so

thanks


Juan
 
Old 02-16-2006, 11:41 AM   #11
joeface
LQ Newbie
 
Registered: Aug 2003
Location: NJ
Distribution: Fedora
Posts: 17

Rep: Reputation: 0
You're right -- you have permission to read the file.

Your ls output also says that the file is empty. So that explains the lack of info when you view it

If sendmail is running on this machine, and you telnet to localhost 25, you should see a line that looks like this:

220 host.domain.com ESMTP Sendmail 8.13.4/8.13.1; Thu, 16 Feb 2006 11:32:49 -0500

If you don't see that line, there's something going on w/the sendmail installation.
 
Old 02-16-2006, 11:55 AM   #12
juancha
LQ Newbie
 
Registered: Feb 2006
Posts: 17

Original Poster
Rep: Reputation: 0
Joeface thanks!!

I just did telnet localhost 25 and got

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
 
Old 02-16-2006, 12:27 PM   #13
joeface
LQ Newbie
 
Registered: Aug 2003
Location: NJ
Distribution: Fedora
Posts: 17

Rep: Reputation: 0
OK. So, back to square one... log files. Your system is using Exim...

According to the Exim web page, these are the log files that it uses:

/var/log/exim/main.log
/var/log/exim/reject.log
/var/log/exim/panic.log

See if you can get any info from these files.

You may also want to look in /etc/exim4/conf.d for configuration info.
 
Old 02-16-2006, 12:42 PM   #14
juancha
LQ Newbie
 
Registered: Feb 2006
Posts: 17

Original Poster
Rep: Reputation: 0
Hi Joeface

thanks for your help

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?
 
Old 02-16-2006, 01:43 PM   #15
joeface
LQ Newbie
 
Registered: Aug 2003
Location: NJ
Distribution: Fedora
Posts: 17

Rep: Reputation: 0
It will be tough to work blind.

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mail-function php sharp81 Linux - Software 3 02-17-2006 10:45 AM
HELP !!! php mail function akamaru607 Programming 22 08-26-2005 03:44 PM
How does the mail function work(PHP) Alexander.s Programming 1 05-15-2005 10:02 AM
PHP - mail function saravanan1979 Programming 2 08-06-2003 02:46 AM
Using the PHP mail() Function !?!? Hdata Programming 0 06-22-2003 07:02 AM


All times are GMT -5. The time now is 06:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration