Question

I just started learning how to use SwiftMailer and I am having trouble sending a simple test message from my localhost. Below is the code that I am trying to use.

//Pass it as a parameter when you create the message
$message = Swift_Message::newInstance();
$message->setSubject('My subject');
$message->setFrom(array('noreply@domain.com' => 'No Reply'));
$message->setTo(array('myemail@domain.com' => 'My Name'));

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 25);
//Supposed to allow local domain sending to work from what I read
$transport->setLocalDomain('[127.0.0.1]');

$mailer = Swift_Mailer::newInstance($transport);
//Send the message
$result = $mailer->send($message);

Here is part of my error message,

Warning:  fsockopen() [<a href='function.fsockopen'>function.fsockopen</a>]:php_network_getaddresses: getaddrinfo failed: Name or service not known in /path/Swift/Transport/StreamBuffer.php

Update

I got it to work using gmail. I changed the Swift_SmtpTransport line to the following,

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('username')->setPassword('password');
Was it helpful?

Solution

localhost is an alias for current machine (in this case, the machine PHP runs on). If you really want to send mail with localhost you have say so:

$transport = Swift_SmtpTransport::newInstance('localhost', 25);

... but you also need to install and configure your own mail server. If you don't know what's this all about, I suggest you use your mail provider's SMTP server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top