You are viewing revision #4 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.
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 firstname.lastname@example.org 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' => 'email@example.com', '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('firstname.lastname@example.org') ->setTo('email@example.com') ->setSubject('Message subject') ->setTextBody('Plain text content') ->setHtmlBody('<b>HTML content</b>') ->send();
And that's it!
This is the first post of many about using Yii/Open Source in business and enterprise level becuase Open source != not cost.