laravel 邮件发送
Sending emails in web applications has become so essential. Marketing, notifications, newsletters, adverts, etc are some of the reasons why we send emails to our clients. I'd say the majority of websites send automated emails at least via a "Contact us" form.
在Web应用程序中发送电子邮件变得非常重要。 市场营销,通知,新闻通讯,广告等是我们向客户发送电子邮件的一些原因。 我想大多数网站至少会通过“与我们联系”表格发送自动电子邮件。
Let's explore the many possible ways to send emails in a Laravel application.
让我们探索在Laravel应用程序中发送电子邮件的多种可能方式。
选择电子邮件服务提供商 ( Choosing An Email Service Provider )
Although your first thought when you see "Email Service Provider" may be service providers in Laravel, that is not what I am referring to here. I am referring to online services that provide email sending functionalities via APIs.
尽管当您看到“电子邮件服务提供商”时最初想到的可能是Laravel中的服务提供商,但这不是我在这里指的。 我指的是通过API提供电子邮件发送功能的在线服务。
You might be wondering why you need to make use of a service when you can just go hardcore with SMTP. The old way works fine, no doubt, but if you really want something awesome, robust, scalable and economic, then a service provider is better as it does all the hard jobs and just gives you an endpoint for your program to talk to,
您可能想知道为什么仅通过SMTP进行硬核时为什么需要使用服务。 毫无疑问,旧方法可以很好地工作,但是如果您真的想要一些很棒,强大,可扩展且经济的东西 ,那么服务提供商会更好,因为它可以完成所有艰苦的工作,并为您提供程序可以与之对话的端点,
We are going to review several possible providers and how to set them up in a Laravel application. Speaking of which, install a new Laravel application and leave config/services.php
open in your favorite editor.
我们将审查几种可能的提供程序,以及如何在Laravel应用程序中进行设置。 说到这, 安装一个新的Laravel应用程序,并在您喜欢的编辑器中将config/services.php
打开状态。
Mailgun (Mailgun)
Sign up for an account if you have not.
如果尚未注册,请注册一个帐户。
Verify your email and phone number.
验证您的电子邮件和电话号码。
You will be redirected to your Dashboard.
您将被重定向到仪表板。
Locate your API Key and domain
找到您的API密钥和域
邮件陷阱 (Mailtrap)
Mailtrap is awesome for development and testing. It was not built with sending emails in production in mind.
Mailtrap非常适合开发和测试。 它并不是在生产时就发送电子邮件的。
- Registier via https://mailtrap.io/register/signup 通过https://mailtrap.io/register/signup注册
- Verify if necessary 必要时验证
- Access your inboxes via https://mailtrap.io/inboxes 通过https://mailtrap.io/inboxes访问您的收件箱
- Store the SMTP credentials somewhere safe
山d (Mandrill)
Setup a domain name
设置域名
Go to settings from the left menu
从左侧菜单转到设置
Click Add API key
点击添加API密钥
Add Mandrill option in the
config/services.php
file在
config/services.php
文件中添加Mandrill选项
'mandrill' => [
'secret' => env('MANDRILL_KEY'),
],
There are several more options, including Amazon SES, but we will just focus on a few. They are all very similar to setup so let us just stick with learning with what we have.
还有更多选项,包括Amazon SES,但我们仅关注其中几个。 它们都与设置非常相似,因此让我们坚持学习已有的东西。
配置我们的服务 ( Configuring Our Services )
Our config/services.php
has all the configuration for major external services that are required for our application. It is also recommended that if Laravel does not provide any service, you should stick to the design pattern of using the services
config file to configure your application.
我们的config/services.php
具有应用程序所需的主要外部服务的所有配置。 还建议如果Laravel不提供任何服务,则应坚持使用services
config文件配置应用程序的设计模式。
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
'mandrill' => [
'secret' => env('MANDRILL_KEY'),
],
'ses' => [
'key' => env('SES_KEY'),