Metropolis
Metropolis

Reputation: 6622

PHP SwiftMailer Localhost Test Setup

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('[email protected]' => 'No Reply'));
$message->setTo(array('[email protected]' => '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');

Upvotes: 4

Views: 20660

Answers (1)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146380

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.

Upvotes: 6

Related Questions