PHP SwiftMailer Localhost Test Setup
Asked Answered
A

1

4

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');
Autogamy answered 19/12, 2010 at 23:2 Comment(1)
this line worked for me too using gmail accountHustings
A
6

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.

Azarria answered 19/12, 2010 at 23:23 Comment(4)
Ok so I am altering my code to use Gmails smtp....Can you tell me how to setup the SSL info through SwiftMailer to get this working? I also need to add the username/password somewhere.Autogamy
Cool, I added the ssl string to the constructor and it worked! Thanks for the help.Autogamy
I'm testing from localhost as I setup a webapp I'm writing and $transport = Swift_MailTransport::newInstance('localhost', 25); got it working for meActing
very helpful, Thank You!Apomorphine

© 2022 - 2024 — McMap. All rights reserved.