Skip to content

Commit 0683a7e

Browse files
committed
Add documentation about sending emails with Symfony on GAE
1 parent fcc99cd commit 0683a7e

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

appengine/php72/symfony-framework/README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,76 @@ the [Stackdriver Error Reporting UI][stackdriver-errorreporting-ui]! If you copi
265265
`https://YOUR_PROJECT_ID.appspot.com/en/logging/notice` and
266266
`https://YOUR_PROJECT_ID.appspot.com/en/logging/exception`
267267

268+
## Send emails
269+
270+
As stated in the [Migration guide from PHP 5.x to 7.x][cloud-migration] the Mail API or App Engine has been removed.
271+
The recommended way to send emails is to use a third-party mail provider such as [Sendgrid][sendgrid], [Mailgun][mailgun] or [Mailjet][mailjet].
272+
Hosting your application on GAE, most of these providers will offer you up to 30,000 emails per month and you will be charged only if you send more.
273+
You will have the possibility to track your email delivery and benefit from all the feature of a real email broadcasting system.
274+
275+
### Install
276+
277+
First you need to install the mailer component:
278+
279+
```
280+
composer require symfony/mailer
281+
```
282+
283+
Then depending on the provider you chose, you need to add the additional component, [Symfony mailer documentation][symfony-mailer]
284+
will give you all the specific per provider but here is an example with Mailgun:
285+
286+
```
287+
composer require symfony/mailgun-mailer
288+
```
289+
290+
This recipe will automatically add the following ENV variable to your .env file:
291+
292+
```
293+
# Will be provided by mailgun once your account will be created
294+
MAILGUN_KEY= xxxxxx
295+
# Should be your Mailgun MX record
296+
MAILGUN_DOMAIN= mg.yourdomain.com
297+
# Region is mandatory if you chose server outside the US otherwise your domain will not be found
298+
MAILER_DSN=mailgun://$MAILGUN_KEY:$MAILGUN_DOMAIN@default?region=eu
299+
```
300+
301+
From that point, you just need to create your account and first domain adding all the DNS Records.
302+
[Mailgun documentation][mailgun-add-domain] will lead you through these steps.
303+
304+
You can now send emails in Controller and Service as usual:
305+
```
306+
// src/Controller/MailerController.php
307+
namespace App\Controller;
308+
309+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
310+
use Symfony\Component\Mailer\MailerInterface;
311+
use Symfony\Component\Mime\Email;
312+
313+
class MailerController extends AbstractController
314+
{
315+
/**
316+
* @Route("/email")
317+
*/
318+
public function sendEmail(MailerInterface $mailer)
319+
{
320+
$email = (new Email())
321+
322+
323+
->subject('Time for Symfony Mailer!')
324+
->text('Sending emails is fun again!');
325+
326+
$mailer->send($email);
327+
}
328+
}
329+
```
330+
268331
[cloud-sdk]: https://cloud.google.com/sdk/
269332
[cloud-build]: https://cloud.google.com/cloud-build/
270333
[cloud-sql]: https://cloud.google.com/sql/docs/
271334
[cloud-sql-create]: https://cloud.google.com/sql/docs/mysql/create-instance
272335
[cloud-sql-install]: https://cloud.google.com/sql/docs/mysql/connect-external-app#install
273-
[cloud-sql-apis]:https://console.cloud.google.com/apis/library/sqladmin.googleapis.com/?pro
336+
[cloud-sql-apis]: https://console.cloud.google.com/apis/library/sqladmin.googleapis.com/?pro
337+
[cloud-migration]: https://cloud.google.com/appengine/docs/standard/php7/php-differences?hl=en#migrating_from_the_app_engine_php_sdk
274338
[create-project]: https://cloud.google.com/resource-manager/docs/creating-managing-projects
275339
[enable-billing]: https://support.google.com/cloud/answer/6293499?hl=en
276340
[symfony]: http://symfony.com
@@ -280,5 +344,10 @@ the [Stackdriver Error Reporting UI][stackdriver-errorreporting-ui]! If you copi
280344
[symfony-secret]: http://symfony.com/doc/current/reference/configuration/framework.html#secret
281345
[symfony-env]: https://symfony.com/doc/current/configuration/environments.html#executing-an-application-in-different-environments
282346
[symfony-override-cache]: https://symfony.com/doc/current/configuration/override_dir_structure.html#override-the-cache-directory
347+
[symfony-mailer]: https://symfony.com/doc/current/mailer.html
283348
[stackdriver-logging-ui]: https://console.cloud.google.com/logs
284349
[stackdriver-errorreporting-ui]: https://console.cloud.google.com/errors
350+
[sendgrid]: https://sendgrid.com/
351+
[mailgun]: https://www.mailgun.com/
352+
[mailjet]: https://www.mailjet.com/
353+
[mailgun-add-domain]: https://help.mailgun.com/hc/en-us/articles/203637190-How-Do-I-Add-or-Delete-a-Domain-

0 commit comments

Comments
 (0)