grizzlylab/mailer-bundle

基本邮件器

安装次数: 14,048

依赖者: 0

建议者: 0

安全性: 0

星标: 3

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

2.0.2 2021-05-18 10:33 UTC

This package is auto-updated.

Last update: 2024-09-20 18:28:47 UTC


README

使用一行代码发送电子邮件的 Symfony 扩展包。它使用 "symfony/twig-bundle" 和 "symfony/swiftmailer-bundle"。

1. 要求

自 2.0 版本起,需要 PHP 7.4+,依赖项 "symfony/twig-bundle" 替代了 "symfony/templating"。这意味着此扩展包现在仅支持 Twig。

自 1.3 版本起,对 依赖项 进行了重要更改。

自 1.2.1 版本起,需要 PHP 7.1+。在此版本之前,最低要求是 PHP 5.4。

2. 安装

运行以下命令通过 composer 安装

composer require grizzlylab/mailer-bundle "~2.0"

然后在您的内核中启用它

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        //...
        new Grizzlylab\Bundle\MailerBundle\GrizzlylabMailerBundle()
        //...

3. 配置

配置 parameters.yml

#app/config/parameters.yml (advice, set it in "parameters.yml.dist" also)
    #...
    mailer_sender_address: noreply@domain.com #required, will be injected in mailer service
    mailer_sender_name: GrizzlyLab #required, will be injected in mailer service
    #...

配置 SwiftMailer(如果尚未完成)

#SwifMailer Configuration
swiftmailer:
    transport:      "%mailer_transport%"
    encryption:     "%mailer_encryption%"
    auth_mode:      "%mailer_auth_mode%"
    host:           "%mailer_host%"
    port:           "%mailer_port%"
    username:       "%mailer_user%"
    password:       "%mailer_password%"
    spool:          { type: memory }
    sender_address: "%mailer_sender_address%"

可选:您可以在 SwiftMailer 配置中使用您的新参数(在 parameters.yml 中定义)

这样,即使您不使用 "grizzlylab_mailer" 发送电子邮件,"sender_address" 也将相同。

#app/config/config.yml
#SwifMailer Configuration
swiftmailer:
    #...
    sender_address: "%mailer_sender_address%"
    #...

4. 使用

1. 内容
a) 默认情况下,$content 参数是 Twig 模板的位置
$container->get('grizzlylab_mailer')->send('@AcmeUser/Mail/awesome.txt.twig', $emails);

您的 Twig 模板中的规则

  • 使用第一行作为主题
  • 使用其余部分作为正文
b) 如果您只想使用简单的字符串,则将 $contentIsATemplate 参数设置为 false
$container->get('grizzlylab_mailer')->send('@AcmeUser/Mail/awesome.txt.twig', $emails, null, [], false);
2. 更多示例
//send($content, $addresses, $subject = null, array $templateParameters = [], $contentIsATemplate = true, array $sender = null)
//If the content is a template and if the subject is null, we use the first line of the template as the subject && the rest as the body

// for a single recipient, second arguement is a string (e.g. 'recipient@domain.com')
$this->container->get('grizzlylab_mailer')->send($content, $address);

// for multiple recipients, second arguement is an array
$this->container->get('grizzlylab_mailer')->send($content, $addresses);

// you can override the sender 
$sender = ['address' => adresse@domain.com, 'name' => 'GrizzlyLab'];
$this->container->get('grizzlylab_mailer')->send($content, $addresses, $sender);

// the return value is the number of recipients who were accepted for delivery
$numberOfAcceptedRecipients = $this->container->get('grizzlylab_mailer')->send($content, $addresses, $sender);

就这样!

许可

此扩展包位于 MIT 许可证之下。