个性化阅读
专注于IT技术分析

如何在Laravel中发送电子邮件(Gmail,Outlook和Zoho)

本文概述

要从Laravel发送电子邮件, 我们将使用Swift Mailer库, 该库默认已包含在Laravel软件包中。 Swift Mailer是一个基于组件的库, 用于从PHP应用程序发送电子邮件。

在本文中, 你将学习如何使用Laravel的Mail类为最知名的电子邮件提供商(Gmail, Outlook和Zoho)发送电子邮件。此外, 如果你不想以laravel的方式进行操作, 还将学习如何使用普通的Swift Mailer发送电子邮件。

组态

我们需要一个从中发送电子邮件的帐户:一个电子邮件帐户, 密码, 其SMTP服务器地址及其使用的加密类型。通常使用SwiftMailer, 你可以在发送电子邮件时提供这些参数(请参阅本文末尾的”使用普通SwiftMailer”段落), 但是作为Laravel的一种好习惯, 我们将在.env文件中提供这些参数。 Laravel项目的根。

.env文件应默认包含以下值:

MAIL_DRIVER=null
MAIL_HOST=null
MAIL_PORT=null
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

根据你的电子邮件提供商的说法, 信息完全不同, 因此我们将为你提供最知名的电子邮件提供商的信息(不要忘记更改电子邮件和密码):

邮箱

注意:重要的是要知道, 有时, gmail帐户不允许使用swiftmailer发送邮件, 并且你需要在gmail帐户中取消选中”允许在不安全的设备上使用”属性。

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=MyGoogleAccount
MAIL_PASSWORD=MyGooglePassword
MAIL_ENCRYPTION=ssl

外表

与Gmail或Zoho不同, Outlook在端口587中使用TLS加密。

MAIL_DRIVER=smtp
MAIL_HOST=smtp-mail.outlook.com
MAIL_PORT=587
MAIL_USERNAME=MyOutlookAccount
MAIL_PASSWORD=MyOutlookPassword
MAIL_ENCRYPTION=tls

佐霍

MAIL_DRIVER=smtp
MAIL_HOST=smtp.zoho.com
MAIL_PORT=465
MAIL_USERNAME=MyZohoEmail
MAIL_PASSWORD=MyZohoPassword
MAIL_ENCRYPTION=ssl

其他提供者

下表列出了最知名的电子邮件提供商以及相应的SMTP地址。

供应商 网址 SMTP设置
1&1 1and1.com smtp.1and1.com
航空邮件 Airmail.net mail.airmail.net
美国在线 Aol.com Smtp.aol.com
美国电话电报公司 Att.net Outbound.att.net
蓝温 Bluewin.ch smtpauths.bluewin.ch
BT连接 Btconnect.com mail.btconnect.tom
康卡斯特 Comcast.net smtp.comcast.net
Earthlink Earthlink.net smtpauth.earthlink.net
邮箱 Gmail.com smtp.gmail.com
Gmx Gmx.net mail.gmx.net
流行音乐 Hotpop.com mail.hotpop.com
Libero 自由 mail.libero.it
莱科斯 Lycos.com smtp.lycos.com
O2 o2.com smtp.o2.com
橙子 Orange.net smtp.orange.net
Outlook.com(以前的Hotmail) Outlook.com smtp.live.com
Mail.tin.it
提斯卡利 Tiscali.co.uk smtp.tiscali.co.uk
威瑞森 Verizon.net Verizon.net
处女 Virgin.net smtp.virgin.net
瓦纳杜 Wanadoo.fr smtp.wanadoo.fr
雅虎 Yahoo.com smtp.mail.yahoo.com
佐霍 zoho.com/mail/ smtp.zoho.com

注意:你可以使用这些值来使用普通SwiftMailer发送电子邮件。

发送邮件

修改完.env文件后, 请不要忘记在Laravel项目中使用以下命令清除缓存:

php artisan cache:clear

现在, 我们可以开始在控制器中编写一些代码来发送电子邮件。 Laravel使用Swift Mailer发送电子邮件, 这样我们就可以轻松发送电子邮件。电子邮件模板的加载方式与视图相同, 因此你可以使用Blade语法并将数据注入模板中。

在这种情况下, 我们将在resources / views文件夹中创建一个电子邮件模板, 名称为email_template.blade.php。它将包含将在演示中发送的基本电子邮件模板。

纯文本

邮件包装程序使你可以轻松发送纯文本邮件, 只需使用Mail命名空间即可。

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Mail;

class DefaultController extends Controller
{
    /**
     * Send plain text email
     */
    public function mail()
    {
        $data = array('name'=>"Our Code World");
        // Path or name to the blade template to be rendered
        $template_path = 'email_template';

        Mail::send(['text'=> $template_path ], $data, function($message) {
            // Set the receiver and subject of the mail.
            $message->to('anyemail@emails.com', 'Receiver Name')->subject('Laravel First Mail');
            // Set the sender
            $message->from('mymail@mymailaccount.com', 'Our Code World');
        });

        return "Basic email sent, check your inbox.";
    }
}

和刀片模板内容:

<!-- 
    /resources/views/email_template.blade.php
-->
Some plain text that my user will receive, 1 > 2 = false

的HTML

通过SwiftMailer, 发送HTML多部分电子邮件比你想象的要容易, 在send方法中提供模板名称(而不是数组)作为第一个参数, 你就可以使用了。

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Mail;

class DefaultController extends Controller
{
    /**
     * Send HTML email
     */
    public function htmlmail()
    {
        $data = array('name'=>"Our Code World");
        // Path or name to the blade template to be rendered
        $template_path = 'email_template';

        Mail::send($template_path, $data, function($message) {
            // Set the receiver and subject of the mail.
            $message->to('anyemail@emails.com', 'Receiver Name')->subject('Laravel HTML Mail');
            // Set the sender
            $message->from('mymail@mymailaccount.com', 'Our Code World');
        });

        return "Basic email sent, check your inbox.";
    }
}

和刀片模板:

<!-- 
    /resources/views/email_template.blade.php
-->
<h1>Hey, how you doing bro?</h1>
I have some info for you, please visit my blog  <a href="http://ourcodeworld.com">here</a>.

使用普通的SwiftMailer

如果你不想修改.env环境, 或者使用多个电子邮件帐户, 则需要使用Swift Mailer类自己编写电子邮件发件人。但是不用担心, 它听起来并不复杂, 使用SwiftMailer既简单又有趣。

注意

<?php 
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Mail;
use View;

class DefaultController extends Controller
{
    public function email(){
        // Configuration
        $smtpAddress = 'smtp.zoho.com';
        $port = 465;
        $encryption = 'ssl';
        $yourEmail = 'mymail@emailaccount.com';
        $yourPassword = 'mypassword';

        // Prepare transport
        $transport = \Swift_SmtpTransport::newInstance($smtpAddress, $port, $encryption)
                ->setUsername($yourEmail)
                ->setPassword($yourPassword);
        $mailer = \Swift_Mailer::newInstance($transport);

        // Prepare content 
        $view = View::make('email_template', [
            'message' => '<h1>Hello World !</h1>'
        ]);

        $html = $view->render();
        
        // Send email
        $message = \Swift_Message::newInstance('Test')
             ->setFrom(['mymail@zoho.com' => 'Our Code World'])
             ->setTo(["mail@email.com" => "mail@mail.com"])
             // If you want plain text instead, remove the second paramter of setBody
             ->setBody($html, 'text/html');
             
        if($mailer->send($message)){
            return "Check your inbox";
        }

        return "Something went wrong :(";
    }
}

刀片模板的内容:

<!--
    /resources/views/email_template.blade.php
-->
This will be escaped
{{ $message }}

<br>

But this not {!! $message !!}

推荐建议

  • 你可以将所有邮件功能包装在命令中, 以使控制器尽可能地薄。
  • 你可能希望创建一个resources / views / emails目录来容纳所有电子邮件模板;但是, 你可以将它们随意放置在resources / views目录中的任何位置。

玩得开心 !

赞(0)
未经允许不得转载:srcmini » 如何在Laravel中发送电子邮件(Gmail,Outlook和Zoho)

评论 抢沙发

评论前必须登录!