views:

712

answers:

2

i have downloaded Swift Mailer from their website and try to send simple email with following code

     <?php
     require_once 'lib/swift_required.php';

    $transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
    ->setUsername('your username')
     ->setPassword('your password')
      ;


    $mailer = Swift_Mailer::newInstance($transport);

  //Create a message
  $message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('[email protected]' => 'John Doe'))
 ->setTo(array('[email protected]', '[email protected]' => 'A name'))
 ->setBody('Here is the message itself')
 ;

 //Send the message
 $result = $mailer->send($message);

?>

once i run the page it gives error

      Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\web_sites\swift_mail\lib\classes\Swift\Transport\StreamBuffer.php  on line 233

    Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.anyhost.com:25 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in E:\web_sites\swift_mail\lib\classes\Swift\Transport\StreamBuffer.php on line 233

   Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.fiveocean.net [php_network_getaddresses: getaddrinfo failed: No such host is known. #0]' in E:\web_sites\swift_mail\lib\classes\Swift\Transport\StreamBuffer.php:235 Stack trace: #0 E:\web_sites\swift_mail\lib\classes\Swift\Transport\StreamBuffer.php(70): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 E:\web_sites\swift_mail\lib\classes\Swift\Transport\AbstractSmtpTransport.php(101): Swift_Transport_StreamBuffer->initialize(Array) #2 E:\web_sites\swift_mail\lib\classes\Swift\Mailer.php(74): Swift_Transport_AbstractSmtpTransport->start() #3 E:\web_sites\swift_mail\test.php(33): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in E:\web_sites\swift_mail\lib\classes\Swift\Transport\StreamBuffer.php on line 235

if i remove the line

  $result = $mailer->send($message);

then page execute and no error message display, as soon as i add above line to send email, i got error.

my outgoing server, port and user id & passwords are correct in my file.

Thanks

+1  A: 

It's looking for the server smtp.fiveocean.net but isn't able to resolve it.

If you look at the line the last step in the stack trade is calling, you can see it throwing an exception:

if (!$this->_stream = fsockopen($host, $this->_params['port'], $errno, $errstr, $timeout))
{
  throw new Swift_TransportException(
    'Connection could not be established with host ' . $this->_params['host'] .
    ' [' . $errstr . ' #' . $errno . ']'
    );
}

So you either need to input a valid smtp server or wrap the send() line in a try/catch to catch the exception and either log it somewhere or ignore it

Mark
if i change the mail server i got following waringWarning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Dubai' for '4.0/no DST' instead in E:\web_sites\swift_mail\lib\classes\Swift\Mime\Headers\DateHeader.php on line 112
air
you need to set the timezone in your script using the date_default_timezone_set('Asia/Dubai') method or in php.ini by setting date.timezone = 'Asia/Dubai'. That will suppress the error
Mark
A: 

The errors tell you everything you need to know:

getaddrinfo failed: No such host is known.

The specified SMTP server (smtp.fiveocean.net) does not exist, so the mailer script can't connec to it to send the email. At least the fiveocean.net domain DOES exist, so perhaps they've got the SMTP server named something else:

marc@panic:~$ host -t soa fiveocean.net
fiveocean.net has SOA record ns.fiveocean.net. sales.fiveocean.net. 1267596439 10800 3600 604800 3600
marc@panic:~$ host -t mx fiveocean.net
fiveocean.net mail is handled by 10 mail.fiveocean.net.
marc@panic:~$ host fiveocean.net
fiveocean.net has address 208.109.97.130
fiveocean.net mail is handled by 10 mail.fiveocean.net.

Specify some other SMTP host that DOES exist and try again.

Marc B