lemonsoftware/lsmailer

用于从您的应用程序发送电子邮件的PHP库

dev-master 2016-04-08 09:20 UTC

This package is auto-updated.

Last update: 2024-08-29 04:19:52 UTC


README

什么是LSMailer?

LSMailer是一个现代高效的库,用于通过SMTP驱动程序发送电子邮件。它适用于需要简单发送电子邮件方式的项目(如联系表单、投票等)。

它具有非常直观的方法,如to(),cc(),bcc(),send()。此外,这些方法是可以链式的,因此您可以以优雅的方式与参数进行交互。

它与简单、TLS或SSL SMTP账户一起工作。还提供了调试选项(只需使用debug标志)。

要求

至少PHP 5.4。

可选,使用phpunit运行测试套件。

安装

如果您正在使用composer,则将其添加到您的composer.json中,并运行composer update

"lemonsoftware/lsmailer"

否则,在您的代码中,包含此包的autoload.php文件

require '<path>/<to>/<package>/vendor/autoload.php';

开始之前

您可能需要更新用于发送主机的内部值(由其完全限定的DNS主机名识别 - 例如 mail.example.com)。使用您选择的文本编辑器打开LSMailer/Drivers/LSSmtp.php

        $sending_host = 'localhost';

替换为

        $sending_host = 'mail.example.com';

配置

静态配置示例

配置非常直接,您需要在一个stdClass结构中提供服务器信息和凭证。例如,在您的代码中发送电子邮件之前,您必须具有以下内容:

	<?php

	....
	$conf = new \stdClass;
	$conf->host = 'mail.example.com';
	$conf->port = 465;
	$conf->secure = 'ssl';
	$conf->user = 'user';
	$conf->pass = 'secret';
	$conf->charset = 'ISO-8859-2';
	$conf->timezone = 'UTC+2';

参数说明

  • host - 邮件服务器所在的主机名(例如 localhost,mail.example.com)。如果使用ssl,不要使用ssl://前缀,而使用安全标志(见下文)。
  • port - (默认: 25) smtp服务运行的端口
  • secure - (默认: none) ssl|tls -检查您的邮件服务器以了解首选方式
  • user - 用户名(如果smtp需要授权 - 大多数情况下)
  • pass - 密码(如果smtp需要授权 - 大多数情况下)
  • charset - (默认: UTF-8) 消息的字符编码
  • timezone - (默认: Europe/Amsterdam) - 您的时区

在这些参数中,只有hostport是必需的,如果使用授权,则添加userpass

动态配置

可以动态更改消息的format()

可以是html|text (默认:text)

$mailer->format('html');

此外,出于测试目的,您可以使用debug()

$mailer->debug(1);

调试信息将输出到控制台。默认为0。

用法

在准备静态配置后,您可以像这样使用它

    <?php
    require 'vendor/autoload.php';

    use LSMailer\LSMail;

    $conf = new \stdClass;
    $conf->host = 'smtp.gmail.com';
    $conf->port = 465;
    $conf->secure = 'ssl';
    $conf->user = 'mytestuser';
    $conf->pass = 's93nfg2mss12';
    $conf->timezone = 'Europe/Dublin';

    $mailer = LSMail::factory('smtp', $conf);
    $mailer->format('html'); // dynamic configuration

    $mailer
        ->to(array('john@example.com', 'john.doe@example.com; John Doe'))
        ->to('another.joe@example.com; Another Joe')
        ->from('jane.doe@mailserver.com');

    $mailer
        ->cc('jane@mailserver.com; Jane')
        ->reply_to('jane.doe@lsmailer.com');

    $mailer->subject("LSMailer - nice piece of software");
    $mailer->format('html')->debug(1);

    $mailer->body("<strong>Text and roses.</strong>");
    $mailer
        ->attach('/path/file1')
        ->attach(array('/path/file2', '/path/file3'));
    $mailer->send();

         ...

所有方法都是可链式的,并且具有非常直观的名称(它们对应于电子邮件字段,如收件人:抄送:回复到:)。