cracksalad/phpmailer-pgp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cracksalad/phpmailer-pgp

Composer 安装命令:

composer require cracksalad/phpmailer-pgp

包简介

PHPMailerPGP enables PHPMailer to send OpenPGP/GPG encrypted and signed e-mails

README 文档

README

Latest Stable Version Total Downloads License PHP Version Require Psalm Type Coverage codecov

This project is based on ravisorg/PHPMailer and replaced PHPMailer inside the repository with PHPMailer as a dependency. It also adds Composer support and includes minor changes to the code itself.

See the main PHPMailer page for all the features PHPMailer supports. This page will document only the PGP additions.

Class Features

  • Uses the PHP GnuPG extension for encryption / signing
  • Encrypt and/or sign outgoing emails with PGP to one or multiple recipients (signs first, then encrypts when both are enabled)
  • Automatically selects the proper keys based on sender / recipients (or manually specify them)
  • Use keys in the GPG keychain or from a specified file
  • Supports file attachments (and encrypts/signs them)
  • Builds PGP/MIME emails so that attachments are encrypted (and signed) as well as the email bodies
  • Supports optional Memory Hole protected email headers (for verified/encrypted subjects, and verified from, to, and cc recipients)
  • Uses standard PHPMailer functions so that, in theory, any email you can create with PHPMailer can be encrypted/signed with PHPMailerPGP
  • Adheres to PHPMailer's coding standards
  • (Mostly) built generically so that other encryption systems (S/MIME) could use the same syntax in their classes

Why you might need it

In an ideal world, users would provide you with their PGP keys and you could use this to send secure emails to them. More realistically: because your server sends emails with lots of sensitive information in them, and you should be encrypting them.

License

This software is distributed under the LGPL 2.1 license. Please read LICENSE for information on the software availability and distribution.

Installation

Add this package to your composer.json like this:

composer require cracksalad/phpmailer-pgp

Dependencies

A Simple Example

Set up your PHPMailer like you would normally:

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

use PHPMailer\PHPMailerPGP\PHPMailerPGP;

$mailer = new PHPMailerPGP();

//$mailer->SMTPDebug = 3;                               // Enable verbose debug output

$mailer->isSMTP();                                      // Set mailer to use SMTP
$mailer->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mailer->SMTPAuth = true;                               // Enable SMTP authentication
$mailer->Username = 'user@example.com';                 // SMTP username
$mailer->Password = 'secret';                           // SMTP password
$mailer->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mailer->Port = 587;                                    // TCP port to connect to

$mailer->setFrom('from@example.com', 'Mailer');
$mailer->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mailer->addAddress('ellen@example.com');               // Name is optional
$mailer->addReplyTo('info@example.com', 'Information');
$mailer->addCC('cc@example.com');
$mailer->addBCC('bcc@example.com');

$mailer->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mailer->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mailer->isHTML(true);                                  // Set email format to HTML

$mailer->Subject = 'Here is the subject';
$mailer->Body    = 'This is the HTML message body <b>in bold!</b>';
$mailer->AltBody = 'This is the body in plain text for non-HTML mail clients';

...but then before sending, specify a file with the keys you want to use (optional) and the encryption / signing options you want to use:

use PHPMailer\PHPMailerPGP\PGPKeyManager;

$manager = new PGPKeyManager();

// Optionally specify a file that contains the keys you want to use.
// Not necessary if the key was already imported into gnupg previously (or manually).
$manager->importKeyFile('/path/to/my-gpg-keyring.asc');

// Optionally check if there is an encryption key for the given recipient(s).
// People not knowing about OpenPGP might be confused by OpenPGP signed mails, 
// so putting `pgpSign()` in an if-statement might be a good idea.
if (count($manager->getKeys('joe@example.net', 'encrypt')) === 1) {

    // Turn on encryption for your email
    $mailer->encrypt(true);
    
    // Turn on signing for your email
    $mailer->pgpSign(true);
}

// Turn on protected headers for your email (not supported by all OpenPGP supporting clients)
$mailer->protectHeaders(true);

...and then continue normal PHPMailer operation:

// Send!
if (!$mailer->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mailer->ErrorInfo;
} else {
    echo 'Message has been sent';
}

Key Lookup and Import

$manager = new PGPKeyManager();
$errCode = 0;
$key = $manager->lookupKeyServer('test@example.com', 'keys.openpgp.org', $errCode);
if ($errCode === PGPKeyManager::LOOKUP_ERR_OK) {
    $manager->importKey($key);
} // else: not found or error occurred

// now you can send encrypted e-mails to test@example.com

cracksalad/phpmailer-pgp 适用场景与选型建议

cracksalad/phpmailer-pgp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.04k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mailer」 「gpg」 「open-pgp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cracksalad/phpmailer-pgp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cracksalad/phpmailer-pgp 我们能提供哪些服务?
定制开发 / 二次开发

基于 cracksalad/phpmailer-pgp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-2.1-only
  • 更新时间: 2023-04-24