You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
Small companies and startups use cheap email services or even Cpanel's mail services which are less secure and compete directly with bigger email providers like Microsoft with Outlook and Google with Gmail. This creates a problem when you try to use their services to send/receive emails from this cheap services.
Google says this: Google Compute Engine does not allow outbound connections on ports 25, 465, and 587. By default, these outbound SMTP ports are blocked because of the large amount of abuse these ports are susceptible to. In addition, having a trusted third-party provider such as SendGrid, Mailgun, or Mailjet relieves Compute Engine and you from maintaining IP reputation with your receivers.
Some time ago I had a project that requieres this, send emails using a cpanel mail services but we have Google Cloud services so when the client send me the credentials to put it in production the app crashes in a critical time, client will not pay to use Gsuite and I have all the stuff ready for production and configured in Google Cloud, so I found a solution.
- Make sure you have swiftmail extension installed (it also comes with the basic and advance template)
- First get a Gmail or Gsuite account to use it as a bridge between emails. The name doesn't matter it will never be seen by final users. let's say email@example.com or use a Gsuite custom email. It is also important that you have full priviledges to this email as you will need it in the next step.
- Enable less secure apps to your email. [https://support.google.com/accounts/answer/6010255?hl=en](Official Docs here)
- Now configure the swiftmailer extension to send emails with your account. I use this config for gmail/gsuite accounts:
'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'smtp.gmail.com', 'username' => 'firstname.lastname@example.org', 'password' => 'your password', 'port' => '587', 'encryption' => 'tls', ], ],
- Test that you can send an email so we can get sure the past configuration works well.
- After you can send an email, login into your account on the web and go to settings > Accounts and Import > Send mail as
- Do the import process of the other service/cpanel email, usually they will send you a link/code to the other email and you will have to confirm it.
- Now you can from your gmail account send mails as some other mail.
- Do this in your Yii code:
Yii::$app->mailer->compose() ->setFrom('email@example.com') ->setTo('firstname.lastname@example.org') ->setSubject('Message subject') ->setTextBody('Plain text content') ->setHtmlBody('<b>HTML content</b>') ->send();
And that's it!