Skip to content

Commit 3d4a2b9

Browse files
bshafferSethbass
andauthored
Add documentation for sending emails with Symfony on GAE (GoogleCloudPlatform#1030)
Co-authored-by: Sébastien LE BRUCHEC <[email protected]>
1 parent 8ac4b09 commit 3d4a2b9

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

appengine/php72/symfony-framework/README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,74 @@ 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+
The recommended way to send emails is to use a third-party mail provider such as [Sendgrid][sendgrid], [Mailgun][mailgun] or [Mailjet][mailjet].
271+
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.
272+
You will have the possibility to track your email delivery and benefit from all the feature of a real email broadcasting system.
273+
274+
### Install
275+
276+
First you need to install the mailer component:
277+
278+
```
279+
composer require symfony/mailer
280+
```
281+
282+
For this example, we will use `Mailgun`. To use a different mail provider, see the [Symfony mailer documentation][symfony-mailer].
283+
284+
```
285+
composer require symfony/mailgun-mailer
286+
```
287+
288+
This recipe will automatically add the following ENV variable to your .env file:
289+
290+
```
291+
# Will be provided by mailgun once your account will be created
292+
MAILGUN_KEY= xxxxxx
293+
# Should be your Mailgun MX record
294+
MAILGUN_DOMAIN= mg.yourdomain.com
295+
# Region is mandatory if you chose server outside the US otherwise your domain will not be found
296+
MAILER_DSN=mailgun://$MAILGUN_KEY:$MAILGUN_DOMAIN@default?region=eu
297+
```
298+
299+
From that point, you just need to create your account and first domain adding all the DNS Records.
300+
[Mailgun documentation][mailgun-add-domain] will lead you through these steps.
301+
302+
You can now send emails in Controller and Service as usual:
303+
```
304+
// src/Controller/MailerController.php
305+
namespace App\Controller;
306+
307+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
308+
use Symfony\Component\Mailer\MailerInterface;
309+
use Symfony\Component\Mime\Email;
310+
311+
class ExampleController extends AbstractController
312+
{
313+
/**
314+
* @Route("/email")
315+
*/
316+
public function sendEmail(MailerInterface $mailer)
317+
{
318+
$email = (new Email())
319+
320+
321+
->subject('Time for Symfony Mailer!')
322+
->text('Sending emails is fun again!');
323+
324+
$mailer->send($email);
325+
}
326+
}
327+
```
328+
268329
[cloud-sdk]: https://cloud.google.com/sdk/
269330
[cloud-build]: https://cloud.google.com/cloud-build/
270331
[cloud-sql]: https://cloud.google.com/sql/docs/
271332
[cloud-sql-create]: https://cloud.google.com/sql/docs/mysql/create-instance
272333
[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
334+
[cloud-sql-apis]: https://console.cloud.google.com/apis/library/sqladmin.googleapis.com/?pro
335+
[cloud-migration]: https://cloud.google.com/appengine/docs/standard/php7/php-differences?hl=en#migrating_from_the_app_engine_php_sdk
274336
[create-project]: https://cloud.google.com/resource-manager/docs/creating-managing-projects
275337
[enable-billing]: https://support.google.com/cloud/answer/6293499?hl=en
276338
[symfony]: http://symfony.com
@@ -280,5 +342,10 @@ the [Stackdriver Error Reporting UI][stackdriver-errorreporting-ui]! If you copi
280342
[symfony-secret]: http://symfony.com/doc/current/reference/configuration/framework.html#secret
281343
[symfony-env]: https://symfony.com/doc/current/configuration/environments.html#executing-an-application-in-different-environments
282344
[symfony-override-cache]: https://symfony.com/doc/current/configuration/override_dir_structure.html#override-the-cache-directory
345+
[symfony-mailer]: https://symfony.com/doc/current/mailer.html
283346
[stackdriver-logging-ui]: https://console.cloud.google.com/logs
284347
[stackdriver-errorreporting-ui]: https://console.cloud.google.com/errors
348+
[sendgrid]: https://sendgrid.com/
349+
[mailgun]: https://www.mailgun.com/
350+
[mailjet]: https://www.mailjet.com/
351+
[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)