Swift mailer mail not sent via SMTP when used gmail account - php

I have following code that uses Swift mailer to send mail via PHP
<?php
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',465, 'ssl')
->setUsername('my.email#mydomain.com')
->setPassword('emailpassword');
$message = Swift_Message::newInstance()
->setSubject("This is test subject")
->setFrom(array("sender.email#gmail.com"))
->setTo(array("recipient.email#gmail.com"))
->setBody("This is test mail body","text/html");
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
This works pretty well, the recipient gets an email. But however, if I change username and password in transport to that of gmail like this -
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',465, 'ssl')
->setUsername('sender.email#gmail.com') //this is the change, using gmail
->setPassword('senderpassword'); //this is the change
Then this doesnot work.
Can anybody tell me where I m doing wrong? what configuration I m missing?
P.S. I checked SPAM folder as well

Have u check your 2-Step Verification in your gmail account? Is it off? I have same problem before when 2-Step Verification is turned on. I turned it off then it work. Maybe you could give a try.

The only thing that worked is by unlocking the captcha
https://accounts.google.com/UnlockCaptcha‎
I got this answer from This SO Question. See reply with highest upvotes.

Related

Sending e-mails to the admin after proper registration from local WAMP server to google account - PHP, MySQL, SwiftMailer

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);

swift mail not working in the live but working in local why?

// 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.

How to set FROM in php mailer using Gmail SMTP

I was trying to set from in php mailer (gmail smtp). But in inbox it shows from mygmailaccount#gmail.com. here is my code :
$this->_mail->Username = "mygmailaccount#gmail.com";
//Password to use for SMTP authentication
$this->_mail->Password = 'mypassword';
$this->_mail->From="support#mydomain.com";
$this->_mail->FromName="Support Team";
//Set who the message is to be sent from
$this->_mail->setFrom('support#mydomain.com', 'Support Team',false);
But when I receive email in inbox its show from : mygmailaccount#gmail.com
and I want to show from : support#mydomain.com
Can anyone help me to find out what I am missing.
This is a gmail restriction, though it's not all bad news! In your gmail preferences you can configure aliases for your account, and you can send out using them as your from address. So you can set the from address, but only to a choice from that preset list, not any arbitrary address. This question has been asked on here before.

Gmail not receiving mails sent by Swiftmailer via sendmail

Ok I have a code to send emails. I'm using Swiftmailer and the transporter is sendmail. Here is the main code:
$transport = \Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -t -i');
$message = \Swift_Message::newInstance()
->setSubject('Notificatio')
->setFrom(array(BR_EMAIL => 'BR'))
->setTo(array($user->getEmail() => $user->getFirstName().' '.$user->getLastName()))
->setBody($msg, 'text/html', 'utf-8');
$headers = $message->getHeaders();
$headers->addIdHeader('Message-ID', time().'.'.uniqid('noreply').'#'.$_SERVER['SERVER_NAME']);
$headers->addTextHeader('X-Mailer', 'PHP v'.phpversion());
if(!$mailer->send($message, $failures)) {
echo "Failures:";
print_r($failures);
}
Now here is the strange part. If the recipient is a Gmail user, the email never gets to it. The send method doesn't give any failures, but the email is lost. It's not in the spam folder either.
But when the recipient is a Gmail Business user (email from Gmail engine with custom domain) the emails are received perfectly and into inbox.
So what is the problem? Is it Gmail filtering the mails? Or do I need to add other Headers?
EDIT
Don't know how, but the problem is resolved.

Google App-Gmail Unable to set from. Gmail replacing from address when sending via swiftmailer SMTP transport (PHP)

Hello and thank you for any help in advance.
I'm using swiftmailer, SMTP transport method to send email from my PHP script.
Sending the message is fine. It works.
The problem is, no matter what I set the FROM,REPLYTO,SENDER settings to, The email comes through as the GMAIL mailbox (ME - me#mydomain.com) instead.
$transport = Swift_SmtpTransport::newInstance('imap.gmail.com', 465,'ssl')
->setUsername($login)
->setPassword($password)
;
$message = Swift_Message::newInstance($subject)
->setFrom(array('test#testdomain.com' => 'test mcttest'))
->setReplyTo(array('test#testdomain.com' => 'test mcttest'))
->setSender('test#testdomain.com')
->setTo(array($to))
->setBody(wordwrap($body, 70))
->setContentType('text/plain')
;
So the email goes through fine, but instead of being from TEST-test#testdomain.com as the sender... it's ME-me#mydomain.com
I switch to a separate (non-gmail) SMTP server to test and the email went through fine, without any problems... definitely think it's a GMAIL thing.
Anyone know how to get around this?
Yes, it is Gmail that changes the sender e-mail address to be the same of the account you are authenticating. There is no workaround for that, except maybe becoming a paid customer of Google Apps.
Funnily enough I've come across the same issue. But as a bit of an insight, I think this is actually a Swiftmailer issue.
There are other libraries and frameworks (Codeignitor pops in my head) that will let you use Googlemails SMTP servers while masking the from address as an email address of your choosing.

Categories