programster/phpmailer-wrapper

PHPMailer 的包装器,使其更容易使用。

1.2.1 2023-08-08 08:08 UTC

This package is auto-updated.

Last update: 2024-09-08 10:17:48 UTC


README

此包使使用 PHPMailer 发送更高级的电子邮件变得更加容易。

如果您只想发送基本的电子邮件,并使用多种可能的驱动程序,那么您可能对 emailers 包 感兴趣。

安装

可以通过运行以下命令使用 Composer 安装此包:

composer require programster/phpmailer-wrapper

示例用法

以下是如何使用此工具将发票作为电子邮件附件发送给另一人的示例。请确保提供 HTML 消息和纯文本替代方案,以防收件人无法查看 HTML 电子邮件。在此示例中,我们还抄送了账户的电子邮件地址。

<?php

use Programster\Phpmailer\Attachment;
use Programster\Phpmailer\AttachmentCollection;
use Programster\Phpmailer\Contact;
use Programster\Phpmailer\ContactCollection;
use Programster\Phpmailer\PhpMailerEmailer;
use Programster\Phpmailer\SecurityProtocol;

require_once(__DIR__ . '/vendor/autoload.php');

# Create the emailer. We can use this to send() any number of emails.
$mailer = new PhpMailerEmailer(
    smtpHost: 'smtp.gmail.com', 
    smtpUser: 'my.email@gmail.com', 
    smtpPassword: 'myPasswordGoesHere', 
    securityProtocol: SecurityProtocol::TLS,
    from: new \Programster\Phpmailer\Contact('myEmail@gmail.com', 'My Name'),
    smtpPort: 587
);

# Send an email
$mailer->send(
    subject: 'Latest Invoice',
    plaintextMessage: "Please find attached my latest invoice.",
    htmlMessage: "<p>Please find attached my latest invoice.</p>",
    to: new ContactCollection(new Contact("to.email@company.domain", "Client Name")),
    cc: new ContactCollection(new Contact("accounts@company.domain", "Accounts")),
    attachments: new AttachmentCollection(new Attachment(__DIR__ . '/invoice.pdf', "my-invoice.pdf"));
);