shuchkin/simplemail

PHP 邮件/SMTP 框架和邮件发送类

0.7.13 2024-01-15 13:48 UTC

This package is auto-updated.

Last update: 2024-09-15 15:23:09 UTC


README

一个简单的邮件构造器,phpmailer 的替代品,SMTP 客户端。
支持 UTF-8 HTML 消息和附件。

基本用法

$mail = new Shuchkin\SimpleMail();
$mail->setFrom('example@example.com')
	->setTo('sergey.shuchkin@gmail.com')
	->setSubject('Test SimpleMail')
	->setText('Hi, Sergey!')
	->send();

安装

推荐通过 Composer 安装此库。 Composer 新手?

这将安装最新支持的版本

$ composer require shuchkin/simplemail

或在此处下载类 here

Fabric

// setup mail
$mail = new Shuchkin\SimpleMail();
$mail->setFrom('example@example.com', 'Example');
 
// fabric method to( $toEmail )
$mail->to('sergey.shuchkin@gmail.com')
	->setSubject('Account activation')
	->setHTML('<html><body><p><b>Your account has activated!</b></p></body></html>', true)
	->send();

// fabric method compose( $toEmail, $subject, $text )	
$mail->compose('admin@example.com', 'New Account', 'https://example.com/useradmin/123')->send();

SMTP

$mail = new Shuchkin\SimpleMail('smtp', [
	'host' => 'ssl://smtp.yandex.ru',
    'port'     => 465,
    'username' => 'test@yandex.ru',
    'password' => 'test'
]);

$mail->setFrom('test@yandex.ru)
	->setTo('sergey.shuchkin@gmail.com')
	->setSubject('Test SMTP')
	->setText('Yandex SMTP secured server')
	->send();

附件与回复

$mail = new Shuchkin\SimpleMail();
$mail->setFrom('example@example.com')
	->setTo('sergey.shuchkin@gmail.com')
	->setSubject('Test attachments')
	->setHTML('<html><body><p>See attached price list.</p><p><img src="logo.jpg" /> Logo</p></body></html>')
	->attach( __DIR__.'/doc/PriceList.pdf')
	->attach( __DIR__.'/images/logo400x300.jpg', 'logo.jpg')
	->setReply('manager@example.com')
	->send();

优先级与自定义头部

$mail = new Shuchkin\SimpleMail();
$mail->setFrom('example@example.com')
	->setTo('sergey.shuchkin@gmail.com')
	->setSubject('WARNING!')
	->setText('SERVER DOWN!')
	->setPriority('urgent')
	->setCustomHeaders(['Cc' => 'admin@exmple.com'])
	->send();

自定义传输

$mail = new Shuchkin\SimpleMail( function( $mail, $encoded ) {
	print_r( $encoded );	
});
$mail->setFrom('example@example.com')
	->setTo('sergey.shuchkin@gmail.com')
	->setSubject('WARNING!')
	->setText('SERVER DOWN!')
	->send();

/*
Array
(
    [from] => example@example.com
    [to] => sergey.shuchkin@gmail.com
    [subject] => =?UTF-8?B?V0FSTklORyE=?=
    [message] => SERVER DOWN!
    [headers] => To: sergey.shuchkin@gmail.com
Subject: =?UTF-8?B?V0FSTklORyE=?=
X-Mailer: PHP/7.2.14
MIME-Version: 1.0
From: example@example.com
Reply-To: example@example.com
Date: Mon, 18 Feb 2019 13:17:28 +0000
Content-Type: text/plain; charset="UTF-8"
)
*/

导出与导入

SimpleMail::toArray() - export to array
SimpleMail::fromArray( $data ) - import from assoc array (fabric)
SimpleMail::toJSON() - export to JSON
SimpleMail::fromJSON( $json ) - import from json (fabric)

历史

0.7.13 (2023-01-15) 所有属性现在是公开的,0.7.12 (2022-02-04) 支持 PHP/5.3
0.7.11 (2019-02-18) 首次发布