What task should be done to send email from the server?

Posted November 26, 2014 10.4k views


I am new at linux configuration. My problem is: I do not what tasks should be done to enable sending email from the server. Normally we use contact form where we use php mail() function. I want when I use this function, I would be able to send email. So, please could you guide me anyone as how to configure or which package should be installed.

Moreover, I tried something reading this link and use the following command:

sudo apt-get install sendmail

And then applied this command:

sudo netstat -plutn | grep 25

And find the result:

$ sudo netstat -plutn | grep 25
tcp 0 0* LISTEN 1293/sendmail: MTA:

However, please help what I should do in this circumstance.

Thanks in advance,

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
10 answers

Installing an MTA such as sendmail is usually enough. Do you get any errors when you try to use php’s mail() function? Can you post the output of the following command:

nc -vz 25

Using the command you have given, I found:

Connection to 25 port [tcp/smtp] succeeded!

When I use the mail(); nothing happens, no email received to my inbox.

Try this:

sudo sendmailconfig

it will ask you a few questions..just choose the default.
reboot and then try

@sierracircle I tried as your instruction but no luck.

are you checking your junk-mail folder?

I am working on a website now, and all email sent from mail() goes to my junk folder..not sure why

@sierracircle Yes I checked. But none of email.

can you test from command line? First create a text file:

nano mail.txt

then paste this into it:

Subject: Put a subject here

Of cause, here's the place to put the body

then save and exit, then run:

sendmail -v  < mail.txt


try a external service and forget about configuration, if you know php you can handle an external service, but if you don’t know how to config a mta service on linux (which can bring you several problems, like open relays, blacklists, etc) then use an external service, I use mandrill which is almost free and pretty good, also gives you a lot of useful information about your email tasks.


Yes I am getting email using the command: sendmail -v < mail.txt

  • great! so mail() works, but probably your script needs some help. Can you post what you are using in php?

  • I wrote as:

    $to      = '';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From:' . "\r\n" .
        'Reply-To:' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $message, $headers);
  • that script works perfectly for me. how are you calling it?

  • I placed this file in /var/www/html/ directory and ran through browsing the IP address. i.e. I have added one domain in the DNS setting, I browsed by that domain too. But nothing works. Is there any issue the way I am doing?

  • Your page opens okay with no problems? if so, that should work..

    I pasted that into a page on one of my sites and browsed to the page and the email popped up instantly in my gmail…so it should be working.

  • Yes it opens because I echoed a string bottom of the code that’s being printed. But no luck.

  • I just tested again on a new blank php document and again it worked.

    I wonder…I do not think this is it, but try anyway:

    sudo apt-get install php5-imap
    sudo php5enmod imap

    sudo apache2 reload

  • I tried and applied these commands, nothing good. :(

  • another far-fetched idea:

    set my email to be the receiving email:

    I will respond when it/if it arrives

  • Where should I add either in the text file(nano mail.txt) or in the php script. However, I have added your email address in the php script now and run. Please let me know did you get email? Thanks

  • did not get it. Clearly not sending out from your PHP script.

    Can you try once more to this address:

    just to be thorough:

    you are running Ubuntu 14.04 (I am assuming)
    you have Apache and PHP installed, latest versions

    You have installed sendmail with:

    sudo apt-get install sendmail

    configure sendmail

    sudo sendmailconfig

    You have a domain pointing to your Droplet and are using that to access your php files?

    When I think about it, it must be something in Apache or PHP, since you can send from command-line.

    I have looked through my php.ini, and here are the mail settings I have (most things are commented out):

    [mail function]
    ; For Win32 only.
    SMTP = localhost
    smtp_port = 25
    ; For Win32 only.
    ;sendmail_from =
    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ;sendmail_path =
    ; Force the addition of the specified parameters to be passed as extra parameters
    ; to the sendmail binary. These parameters will always replace the value of
    ; the 5th parameter to mail(), even in safe mode.
    ;mail.force_extra_parameters =
    ; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
    mail.add_x_header = On
    ; The path to a log file that will log all mail() calls. Log entries include
    ; the full path of the script, line number, To address and headers.
    ;mail.log =
    ; Log mail to syslog (Event Log on NT, not valid in Windows 95).
    ;mail.log = syslog
  • Thanks to reply, there is a step perhaps I missed that is configure sendmail. Could you help what kind of configuration I should do. Meanwhile I am checking php.ini. Thanks my friend for your cooperation.

  • I have compared the part of php.ini. Everything exactly same as here.

  • when you run:
    sudo sendmailconfig

    you can just select the defaults of all the options it gives you…restart apache afterwards.

  • However, after long try and installing Postfix, I got email. Now it works now. But failure to sendmail or try later. Thanks @sierracircle , you gave me a lot of time and I learn many things from you. Thanks again man.

One more thing to try:

sudo apt-get install mailutils