How to send email from localhost using PHP on Linux
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?
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.
This worked for me on Linux Mint 17 for sending emails from the localhost:
sudo apt-get install sendmail
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.
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.
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…..
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.
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.
精彩评论