Saturday, January 29, 2011

Failing to send email on Ubuntu box (Karmic Koala)

I have a home network with an XP and Ubuntu (9.10) box. I have created a small test php script for checking that I can send emails from my machine. I am using the same php.ini file with the same [mail settings], yet the file works on my XP box, and fails on the Ubuntu box. I have included the script here, hopefully, someone can spot whats going wrong:

<?php

// send e-mail to ...
$to="myemail@hotmail.com";

// Your subject
$subject="Test Email";

// From
//$header="from: test script";
$header='From: host-email-username@hostdomain_here' . "\r\n" .

// Your message
$message="Hello \r\n";
$message.="This is test\r\n";
$message.="Test again ";

// send email
$sentmail = mail($to,$subject,$message,$header);

// if your email succesfully sent
if($sentmail){
echo "Email Has Been Sent .";
}
else {
echo "Cannot Send Email ";
}
?>

The emails have been spoofed for obvious reasons, but otherwise, the script is exactly as the one I tested

[Edit] I have since installed mailutils package on my Ubuntu box, now the script runs and returns 'Email has been sent'. However, the mail never arrives in my mail inbox (I've waited 1 day so far). Is there something else I need to be looking at?

  • Can you send mail directly from the command line on that machine? If not, then that's the problem -- the machine is not properly set up.

    I think the mail() function can use many different methods to send mail, with the default using sendmail or its replacement. Perhaps this part is where you need to look.

    : tx michael, but could you be a bit more specific?. For instance, how do I send mail from the command line (I'm relatively new to *nix)
    Michael Graff : I commonly cheat when I send test mail. Find a file (I use `/etc/motd` on my machines) and run: mail -s "test email" your-address@example.com < /etc/motd This will send you the contents of `/etc/motd`. If you don't like that, make a sample file with some junk in it and use that.
    Dennis Williamson : You can just pipe `echo "test message"` into the mail command for the message body.
    : tx for the tip Michael, I'll rember that. In the mean time I did a search and found that I had to install the mailutils package. the mail appears to have been sent (no error in the script) - allthough I am still waiting for the mail to arrive in my inbox. I'll check again later.
    : Hmm, 1 day later, still message has not arrived in my mailbox - thats not right. I think sending mail is still not working as it should on on my Ubuntu box. Does anyone know why the mail() command will return a success code (refering to the code snippet above), and yet the message FAILS to be delivered?
  • Try the following commands and check if they show anything that can be used to fix the problem

    more /var/log/maillog mailq

    : I dont have that file. The only files I have in the /var/log/ folder are: mail.err, mail.info, mail.warn and mail.log. All of these files have size 0 bytes (i.e. empty)
    Srikrishnan Chitoor : Do you get anything when you run "mailq" command?
    : Sri: I run the command, but got no output
  • All application needs to connect to a smpt on port 25(fefault) before it can send out the message. In your php application you have to mentioned which smtp to connect to. I think if you do not mention any, it uses the smtp installed in your local system(I am not a php developer myself so not sure). Also your ubuntu machine might not have the smtp server(eg postfix) configured to properly send mails out to the internet. There is a setup called as "local only" where in it delivers mails only to the local system. Best way to trouble shoot is, in one terminal type the command "tail -f /var/log/mail.log" This shows updates happening to that log file in real time. Then you execute your php script. you must be able to see what messages your smtp serevr gives you when you execute the script.If you post those messages here we might further try to solve your problem.

    : proy: I run the command as you suggested, then run my script. No messages were issued by the smtp server (the tail command did not show any output) ??? - whats going on !?
    proy : Thats strange. Can you do "netstat -pant | grep 25" and see if there is any smtp server running on the machine.
    : proy: I run the command you suggested (using sudo) and got the following output: tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1666/exim4 Hope that makes more sense to you than me (BTW, they are two seperate lines - the formatting is likely to get lost). The second line starts is 1666/exim4 HTH
    From proy
  • You probably need to set up sending mail through an SMTP server. For example, if you use Gmail, here is a good article with step by step instructions on how to send through Gmail's SMTP server: http://www.marksanborn.net/linux/send-mail-postfix-through-gmails-smtp-on-a-ubuntu-lts-server/

    Many of the he listed commands require super user access, or prefixing with 'sudo'.

    It worked for me on the first run through. If it doesn't for you, be sure to check the comments on that page, which provide some extra hints.

    From Trung
  • I have just tried your code with the sendmail_path uncommented in the php.ini:

    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    sendmail_path = sendmail -t -i
    

    Also check your spam folder. I put my gmail account in the 'To' field and I found it in the Spam folder :)

    From ccheneson

0 comments:

Post a Comment