12

I know that to send e-mail from localhost on Windows, you need to change SMTP server in php.ini however this is valid only on Windows:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

So what I should do to be able send e-mails from Linux OS?

nikc.org
  • 16,462
  • 6
  • 50
  • 83
Templar
  • 1,843
  • 7
  • 29
  • 42
  • In linux by default their is no configuration required to send mail. What is the output of php mail function ?. Try mail command on terminal and check mail is correctly configured http://linux.about.com/od/commands/l/blcmdl1_Mail.htm – nidhin Aug 03 '11 at 08:18
  • AFAIK, mail command sends receive email in same fashion as outlook would do, its not an email server – Kumar Aug 03 '11 at 08:30
  • @nidhin mail($to,$subject,$message,$headers); gives nothing, no error but if $send = mail($to,$subject,$message,$headers); and echo $send; it shows nothing again so it means it's FALSE – Templar Aug 03 '11 at 08:30
  • 1
    @templar you need to install a mail server like sendmail or exim – Kumar Aug 03 '11 at 08:31
  • i guess this one will help you much better way to send email by using your gmail credentials. http://www.kvcodes.com/2016/03/send-e-mail-localhost-ubuntu-php/ – Kvvaradha Mar 18 '16 at 05:19

7 Answers7

6

I would suggest installing ssmtp rather than installing a full mail server like postfix. If this is just a local test environment, you probably don't need a full MTA. ssmtp is very easy to setup--you just supply your smtp credentials for a remote server. There's a tutorial here.

Jacob Dalton
  • 1,643
  • 14
  • 23
4

This worked for me on Linux Mint 17 for sending emails from the localhost:

sudo apt-get install sendmail
3

To send mail from localhost (WAMP, XAMP, or LAMP) you can use PHPMailer package (Download PHPMailer from here).

First you have to edit the "php.ini" To find this file display the phpinfo by using following code from the WAMP server. Create one php file and add this content.

<?php
     echo phpinfo();
?>

There search for "Loaded Configuration File" That will be the path to your php.ini.

In this file remove the ;(semi colon) given to extension=php_openssl.dll.

After downloading PHPMailerX.X.X package

Extract->Copy the full folder into your project folder.

In test folder there is one file called testemail.php.

Change the parameter as your need. (Example given below).

Then in the browser type 127.0.0.1/PHPMailer/test/testemail.php.

Then it will show successful message if email sent, else it will give error message. Example:

//add these codes if not written

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465; 

//You have to change these parameters to your requirements.
//…….code….

$mail->Username = “abc@gmail.com”; // GMAIL username
$mail->Password = “abcdefgh”; // GMAIL password
//……..code….. There are many other functions to attach file etc.. For that refer doc file.
$mail->AddAddress(“destination@gmail.com”,”Nick name”);
//…….code…..
Shashidhara
  • 665
  • 6
  • 22
3

If you're running Debian and variants thereof (*buntu, etc.), you can install a mail server by running sudo tasksel install mail-server, which should set you up with basic email capabilities. You can test this by running in command line echo 'body' | sendmail recipient@example.net, or as others have mentioned, mail($to, $subj, $msg) in PHP.

mzhang
  • 392
  • 3
  • 12
  • When i was installing I have chosen by mistake no configuration, how i can change configuration? And maybe you can offer which one should I take – Templar Aug 03 '11 at 10:30
  • well I found out how to change setting, but my mail was delivered ONLY in 2 days, maybe I made wrong settings, can you help me? – Templar Aug 11 '11 at 09:04
  • I'm not sure, there was a lot of them or do you want me to resetup them and tell you again? – Templar Aug 14 '11 at 10:56
2

There should be a stub in your php.ini file already, something like:

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

Check that the command specified exists on your filesystem and you have (installed and) configured the MTA correctly.

If you've not already got an MTA set up, there are lots of MTAs available for Linux systems, I'd recommend sendmail (comples/difficult to configure but great performance and amazing flexibility) or postfix (easier to configure, good security out of the box).

Learn how to use the 'mail' cli client or run a different MUA on the server to seperate configuring the MTA from PHP integration.

symcbean
  • 47,736
  • 6
  • 59
  • 94
1

Setup sendmail or exim. Search in your package manager for either of the option and install them. Senmail is common and setting it up should do the job. The mail command on linux is an MUA or mail user agent, a Mail User Agent (MUA) is an application that is used to send and receive email, more of a client. Whereas mail transfer agent transfers emails from one computer to another using a client–server application architecture.

Kumar
  • 5,038
  • 7
  • 39
  • 51
1

You need to install a MTA such as sendmail (Some distros like Debian seem to prefer exim, or maybe postfix).

Using PHP's SMTP implementation is a bad idea, because it will not respect the retry requirement of RFC5321 "4.5.4.1. Sending Strategy: mail that cannot be transmitted immediately MUST be queued and periodically retried by the sender."

This violation of an RFC "MUST" requirement generally reduces robustness and will interfere with correct interoperation in most cases. This is why you need to use a local MTA (under Linux or Windows). Ideally, you need a non-SMTP mechanism for submitting messages to the MTA, which is why PHP will should execute "sendmail" to send mail. It tends to do this by default if a MTA is installed.

MarkR
  • 62,604
  • 14
  • 116
  • 151