Sending email from localhost
I'm using WampServer on Windows to test a site. I have a registration system where users get a confirmation email.
Is开发者_如何学Python it possible to send emails from a localhost?
If you want to send emails from localhost directly, you need to install a Mail Transport Agent (MTA), or if you like, a SMTP service.
IIS provides one. You can otherwise find some others on Google.
You can also change your php.ini
mail settings. This won't use localhost
per say to send emails, but a relay host that will allow you to send emails from a PHP script.
Not unless you install a SMTP server on it. I think XAMPP includes one, but you're probably better off just using an existing external server. You can set this with the php.ini SMTP
setting.
I actually just set this up the other day using fake sendmail.
It is actually fairly easy to set up. I found this tutorial to be helpful.
There are other alternatives out there but this is working great for me using Gmail.
I can use the mail()
function in PHP without issues. One issue that I haven't resolved yet is that the email always appears to come from my gmail address instead of whatever I supplied in the mail()
arguments. I believe this may be a Gmail feature, not a sendmail issue though.
It's quite simple. (Adapt syntax for your convenience)
public $smtp = array(
'transport' => 'Smtp',
'from' => 'your_email@gmail.com',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'your_email@gmail.com',
'password' => '*****'
)
The easiest way I have found to create a mail sever in windows is to install Python and then use the inbuilt SMTP debug server with the following one-liner:
python -m smtpd -c DebuggingServer -n localhost:25
On Windows, I have found that it sometimes has strange behaviour with localhost and 127.0.0.1 so, if all else fails, you could trying finding your IP address using the 'ipconfig' command (at the DOS command prompt) and you might see something like:
c:\> ipconfig
-- stuff cut here ---
Wireless LAN adapter WiFi:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::856d:fd33:3e01:e656%9
IPv4 Address. . . . . . . . . . . : 192.168.8.111
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.8.1
and, let's say you find you ip address is 192.168.8.11 (as I did in the example above), then changing the command to look like:
python -m smtpd -c DebuggingServer -n 192.168.8.11:25
And setting the following lines in your php.ini file:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = 192.168.8.111
; http://php.net/smtp-port
smtp_port = 25
And, this has worked for me on occasions where nothing else does!
I also note you can check if you local mail server is working by typing the command:
C:/> telnet localhost 25
or for my second configuration:
C:/> telnet 192.168.8.111 25
Telnet comes for free on Linux but to get it on windows you have to go to "Programs and Features" and then click "Turn Windows feaures on and off" (from the little panel on the left) and then check the box for "Telnet Client" in the list of Windows Features.
When you run the telnet command you will see something like this if the server is not running:
C:\Projects>telnet localhost 25
Connecting To localhost...Could not open connection to the host, on port 25: Connect failed
But, if it is all working you should get the screen cleared and a "server identifier" string back from the mail server. On my machine this looked like:
220 ZOOT Python SMTP proxy version 0.3
If you are really keen you can type some SMTP commands and send a mail here or you can type 'quit' to disconnect from the server.
精彩评论