kisphp/simple-mail

发送电子邮件的快捷简单方法

2.0.0 2016-08-30 10:08 UTC

This package is not auto-updated.

Last update: 2024-09-22 05:51:51 UTC


README

Build Status codecov.io

Latest Stable Version Total Downloads License Monthly Downloads

使用 swift mailer 为您的网站快速发送电子邮件,实现方式非常简单。默认配置为通过谷歌发送。

安装

composer require kisphp/simple-mail

配置

如果您已经在项目中使用了 composer,则所需的库将自动包含。否则,您需要在 PHP 文件中包含自动加载器。

require_once '/path/to/vendor/autoload.php';

您需要做的第一步是创建一个实现 Kisphp\Mail\MailConfigInterface; 的配置类。

<?php

namespace Demo;

use Kisphp\Mail\MailConfigInterface;

class DemoMailConfig implements MailConfigInterface
{
    public function getHost()
    {
        return 'ssl://smtp.gmail.com';
    }

    public function getPort()
    {
        return 465;
    }

    public function getSenderUsername()
    {
        return 'your-email-address@gmail.com';
    }

    public function getSenderPassword()
    {
        return 'your account password';
    }

    public function getMailEncryptionType()
    {
        return null;
    }

    public function getFromEmail()
    {
        return 'no-reply@example.com';
    }

    public function getFromName()
    {
        return 'My website';
    }
}

接下来,您需要创建扩展 AbstractMailerFactory 类以使用您的配置。

class DemoMailerFactory extends AbstractMailerFactory
{
    /**
     * @return DemoMailConfig
     */
    public function createMailConfig()
    {
        return new DemoMailConfig();
    }
}

从这里开始,您就可以开始使用了。

<?php

$messenger = DemoMailerFactory::createMailer();

// recipients
$recipients = [
    'user_1@example.com' => 'User name 1',
    'user_2@example.com' => 'User name 2',
];

$subject = 'Testing mail';
$htmlMessage = 'this is my <b>message</b> for you';

// compose email
$messenger->createMailMessage($recipients, $subject, $htmlMessage);

// send the email and get the number of how many emails were sent
$emailsSent = $messenger->send();

更改邮件传输类型

要更改传输类型,您需要从 Messenger 类中扩展 createMailTransport 方法。

<?php

use Kisphp\Mail\Messenger;

class ProjectMessenger extends Messenger
{
    /**
     * @return $this
     */
    protected function createMailTransport()
    {
        $this->transport = \Swift_MailTransport::newInstance();
        
        return $this;
    }
}

class DemoMailerFactory extends AbstractMailerFactory
{
    
    // createMailConfig method here
    
    /**
     * @param MailConfigInterface $config
     *
     * @return MessengerInterface
     */
    public function createMessenger(MailConfigInterface $config)
    {
        return new ProjectMessenger($config);
    }
}

// and load this class in your project
$messenger = new ProjectMessenger($config);

更多详细信息请参见此处: SwiftMailer 发送