Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with PHP mail() function [duplicate]

Tags:

php

email

Possible Duplicate:
php mail() function on localhost

I'm trying to do some localhost testing for password recovery on my site, but when I try to send an email, I get the following error:

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Here are the relevant settings in my php.ini file.

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

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain

I am not sure what to set these to for localhost testing. I realize that I need to set SMTP to whatever my provider's mail server is, but I work in a shared office building so I don't know how to even find out who provides the internet here.

Thanks in advance.

like image 248
tnw Avatar asked Jul 27 '11 15:07

tnw


2 Answers

PHP's mail() function doesn't implement SMTP protocol directly. Instead it relies on sendmail() MTA (SMTP server), or a replacement like postfix or mstmp. It works fine on Unix as long as MTA installed.

On Windows (from PHP.net manual):

The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).

So - the moral of the story - you need to install mail server.

However - if it's for test purpose only - simply get a PHP library that actually implements SMTP protocol and use your regular gmail email address to send emails:

Instead of using PHP's mail() use one of these:

  1. PHPmailer
  2. SwiftMailer
  3. Zend\Mail

These PHP libraries actually implement SMTP protocol so one can easily send emails from any platform, without email server installed on the same machine:

PHPMAILER example:

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "stmp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$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;                   // set the SMTP port for the GMAIL server
$mail->Username   = "[email protected]";  // GMAIL username
$mail->Password   = "pass111";            // GMAIL password
$mail->SetFrom('[email protected]', 'My name is slim shady');
$mail->AddReplyTo("[email protected]","My name is slim shady");
$mail->Subject    = "Hey, check out http://www.site.com";
$mail->AltBody    = "Hey, check out this new post on www.site.com"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "[email protected]";
$mail->AddAddress($address, "My name is slim shady");
like image 119
ThatGuy Avatar answered Sep 28 '22 06:09

ThatGuy


PHP's mail requires a local mailserver to run.

Edit: As the PHP documentation site for mail() reveils, you may use a Mail package from PEAR.

like image 30
Michael-O Avatar answered Sep 28 '22 08:09

Michael-O