开发者

"Failed to connect to mailserver" when using SMTP

开发者 https://www.devze.com 2023-03-23 06:04 出处:网络
I\'m trying to send an email with the following function as a test function: function SendEmail($to, $subject, $body, $headers)

I'm trying to send an email with the following function as a test function:

function SendEmail($to, $subject, $body, $headers)
{
    ini_set("SMTP", "smtp.myexistingdomain.com");
    mail($to, $subject, $body, $headers);
}

and I get this error:

War开发者_高级运维ning: mail() [function.mail]: Failed to connect to mailserver at "smtp.myexistingdomain.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()


PHP doesn't implement SMTP protocol. PHP's mail() - is wrapping around sendmail binary. Works fine on unix/linux. But 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.

If you prefer to send emails without installing you own mailserver (which can be a pain in the *ss) - you must use a library that actually implements SMTP protocol and thus allows you to connect to someone else SMTP server. These are most popular nowadays:

  1. PHPmailer (download)
  2. Swiftmailer
  3. Zend_Mail

Using these libraries one can construct text or html messages with ease. Adding files also becomes an easy thing to do.

/*******************************************************************
  Example of sending email using gmail.com SMTP server and phpmailer
*******************************************************************/
$mail = new PHPMailer();  // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465; 
$mail->Username = GUSER;  
$mail->Password = GPWD;           
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
    $error = 'Mail error: '.$mail->ErrorInfo; 
    return false;
} else {
    $error = 'Message sent!';
    return true;
}


are you sure this domain works and an smtp server is active on port 25? secure smtp traffice uses a different port, google can tell u which in a sec :)

you can try this if telnet is installed on your pc:

open a command prompt, type: telnet smtp.myexistingdomain.com 25 when it connects you can be sure the domain works and has a listening smtp server. if it fails, the smtp server is probably unreachable or uses a different port.

Another thing you can check is if the right credentials are set in the php.ini file, most smtp servers require authentication.


The SMTP setting is only available on Windows. Are you able to connect directly to that host/port via telnet? If something external to PHP, but running on the host, is unable to connect as well, then either that's not the right hostname, it's not listening on port 25, and/or there's a firewall issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号