PHP code example of tourze / smtp-mailer-bundle

1. Go to this page and download the library: Download tourze/smtp-mailer-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / smtp-mailer-bundle example snippets


// 注入服务
use Tourze\SMTPMailerBundle\Service\SMTPMailerService;

class MyController extends AbstractController
{
    public function sendEmail(SMTPMailerService $mailerService)
    {
        $mailerService->send(
            '[email protected]',
            '邮件主题',
            '邮件内容',
            [
                'from' => '[email protected]',
                'isHtml' => true,
                'cc' => ['[email protected]'],
            ]
        );
        
        // 或者安排稍后发送
        $mailerService->send(
            '[email protected]',
            '定时邮件',
            '这是一封定时发送的邮件',
            [
                'scheduledAt' => new \DateTime('+1 hour')
            ]
        );
    }
}

// 使用特定ID的SMTP配置
$mailerService->sendWithConfig(
    $smtpConfigId,
    '[email protected]',
    '邮件主题',
    '邮件内容'
);

// 使用优先级策略选择SMTP
$mailerService->send(
    '[email protected]',
    '邮件主题',
    '邮件内容',
    [
        'strategy' => 'priority'
    ]
);
bash
# 处理定时邮件
php bin/console smtp-mailer:process-scheduled-mails

# 如果使用异步处理,需要运行 messenger worker
php bin/console messenger:consume async