I can't connect with Gmail SMTP server.
Look:
$transport = Swift_SmtpTransport::newInstance()
->setHost('smtp.gmail.com')
->setPort(465)
->setEncryption('ssl')
->setUsername('email#gmail.com')
->setPassword('mypasss');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Contato via Site')
->setFrom(array($email => $de))
->setTo(array($destinatario => 'AgĂȘncia Linka'))
->setBody($corpo_mensagem, 'text/html')
->setCharset('UTF-8');
$mailer->send($message);
And then I get this:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [ #0]' in ...
Anyone has experienced this?
First login with your account and open this in new tab,
https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha
https://security.google.com/settings/security/activity?hl=en&pli=1
you need to make sure your using email id has enable for less secure apps.
You can send mail by using tls instead of ssl certificate like below:
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587,'tls')
->setUsername('email#gmail.com')
->setPassword('mypasss');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Contato via Site')
->setFrom(array($email => $de))
->setTo(array($destinatario => 'AgĂȘncia Linka'))
->setBody($corpo_mensagem, 'text/html')
->setCharset('UTF-8');
$mailer->send($message);
Gmail has changed his policy so yuo will need to take special additional steps to make this working, I could explain it here for you but You can have a look at this answer which explains the action you need to take:
Using php's swiftmailer with gmail
Related
After a proper user registration (data is logged to the database and registration is running correctly) I would like to receive an email to a gmail account confirming the success. I am working on a WAMP local server and I am using the SwiftMailer library.
Below the code, which unfortunately this e-mail does not send me.
<?php
$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('testphp2017#gmail.com')
->setPassword('testxxx');
$mailer = Swift_Mailer::newInstance($transporter);
$mailer = Swift_Mailer::newInstance($transporter);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('testphp2017#gmail.com'))
->setTo(array('testphp2017#gmail.com'))
->setBody('Test Message Body')
;
Your code is missing the send function:
$result = $mailer->send($message);
I've searched high and low and I can't seem to find a working example of a swiftmailer script that uses a hotmail smtp, I have tried using the google one's in examples and upating the port, user and password but the page just hangs until it times out.
My code:
require_once 'lib/swift_required.php';
// Create the SMTP configuration
$transport = Swift_SmtpTransport::newInstance("smtp.live.com", 465, "TLS");
$transport->setUsername("xxxx#hotmail.com");
$transport->setPassword("XXXXXX");
// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
"XXXX#company.com" => "xxx",
));
$message->setCc(array("another#fake.com" => "Aurelio De Rosa"));
$message->setBcc(array("boss#bank.com" => "Bank Boss"));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
$message->setFrom("XXXXX", "XX XXXXXXX");
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message, $failedRecipients);
// Show failed recipients
print_r($failedRecipients);
I expect you need to set your encryption param correctly. For port 465 use ssl; for 587, tls. hotmail isn't any different to anywhere else - your code would not work anywhere.
// Create the mail transport configuration
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername(PASUPUKUMKUMA_EMAIL)
->setPassword(PASUPUKUMKUMA_PASS);
// Create the message
$message = Swift_Message::newInstance()
->setTo(array($email => $name))
->setSubject("Registration Success !")
->setBody('You have Registered Successfully ! Thank You For Registering With Us.Click the link to confirm your account <a href='.site_url().'/main/confirm/'.$name.'/'.$ran.'>confirm here</a>', 'text/html')
->setFrom(PASUPUKUMKUMA_EMAIL,EMAIL_FROM_NAME);
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
This is my code for sending email through swift mailer everything was perfect when I send the mail in the local I can be able to send the mail successfully.
When comes to live website with the same code not able to send the mail and shows the error:
Connection could not be established with host smtp.gmail.com..
https://www.google.com/settings/security/lesssecureapps
Turn it on for the account that you are using. Hope it will help.
So I am trying to use swiftmailer to send emails using a gmail account. I know there are questions that address this issue, but none of the proposed solutions have helped me. My problem is that when I run my code I get "PHP Fatal error: Uncaught exception 'Swift_TransportException' with message 'Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted." I know that my password and username are correct, and the Google two-step verification is not enabled. Here is my code:
require_once 'vendor/swiftmailer/swiftmailer/lib/classes/Swift.php';
Swift::registerAutoload();
require_once 'vendor/swiftmailer/swiftmailer/lib/swift_required.php';
require_once 'vendor/swiftmailer/swiftmailer/lib/swift_init.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
->setUsername ('myemail#gmail.com')
->setPassword ('mypassword');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Weekly Hours')
->setFrom (array('myemail#gmail.com' => 'My Name'))
->setTo (array('recipient#hotmail.com' => 'Recipient'))
->setSubject ('Weekly Hours')
->setBody ($data, 'text/html');
$result = $mailer->send($message);
Note that I have also tried port 465 with 'lss' encryption. Thanks in advance.
The problem could be related to the fact that google can use a range of IPs.
I solved the problem in my case with something like this:
#get the host dynamically
$smtp_host_ip = gethostbyname('smtp.gmail.com');
#set the transport
$transport = Swift_SmtpTransport::newInstance($smtp_host_ip,465,'ssl')->setUsername('myemail#gmail.com')->setPassword('mypassword');
I hope it helps.
When I change providers I always have to make sendmail work and it's a real drag.
Are there any free providers I can use within my PHP code to send the same variables like keycodes for registration verification and stuff?
I tried looking on Google but I only found stuff that had form generators, which is not what I need.
I use PHPMailer with great success.
Right now Swift Mailer is one of the best mail solutions around for PHP. It's highly modular and can easily be configured to suit your needs. You could define multiple transports (in a config file for example) and use the one you need when you change providers.
Here is an example from the docs:
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail
$transport = Swift_MailTransport::newInstance();
*/
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
What you need is an outgoing authenticated SMTP server so your not using the hosts one and don't have to change the details each time.
Take a look at AuthSMTP (there are lots of other websites that provide this) and then use something like PHPMailer or Swift Mailer to send the email with authentication.
The problem is that a lot of ISPs block connections on port 25 (default smtp) to servers other than their own. Has to do with spam blocking etc.