pyataks/sendmail

以不同方式发送消息。

1.0.8 2016-08-30 08:55 UTC

This package is not auto-updated.

Last update: 2024-09-22 08:08:47 UTC


README

安装

composer require pyataks/sendmail

示例

SMTP配置

$smtpConfig = [
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'username' => 'username@gmail.com',
    'password' => ''
];

Mandrill配置

$mandrillKey = 'mandrill API-key';

通过mail()发送带附件的消息

$mailer = new Mailer(new MailTransport());
try {
	$message = (new Message())
            ->to('emailto@example.com', 'Jon')
            ->from('emailfrom@example.com', 'Jon Mailer')
            ->subject('Test subject. Send mail php')
            ->body('<div style="color: red">Test content. Send mail php</div>', 'text/html')
            ->attach('full_filename', ['name' => 'main-logo.png', 'mime_type' => 'image\png']);
} catch (\InvalidArgumentException $e) {
	echo $e, PHP_EOL;
}

try {
	echo $mailer->send($message);
} catch (\pyatakss\sendmail\PSMailException $e) {
	echo $e, PHP_EOL;
}

创建不同的传输

$mailTransport = new MailTransport();
$smtpTransport = new SMTPTransport($smtpConfig);
$mandrillTransport = new MandrillTransport($mandrillKey);

为mail()创建传输

$mailer = new Mailer($mailTransport);

准备消息

$messageCyrillicText = (new Message())
    ->to('emailto1@example.com', 'Рома')
    ->to('emailto2@example.com', 'Маша')
    ->to('emailto3@example.com', 'Кирилл')
    ->from('emailfrom@example.com', 'Джон Сильвер')
    ->subject('Тема на кирилице. Send mail php')
    ->body('<div style="color: red">Текст письма.<br> Писмо отправелно без attach. <br> Send mail php</div>', 'text/html');
    
$messageSwift = (new SwiftMessageAdapter(new \Swift_Message()))
    ->to('emailto1@example.com', 'Рома')
    ->to('emailto2@example.com', 'Маша')
    ->to('emailto3@example.com', 'Кирилл')
    ->from('emailfrom@example.com', 'Джон Сильвер')
    ->subject('Subject English swift . Send mail php')
    ->body('<div style="color: green">Text of the letter.<br> Letter sent WITH attach. <br> Send mail php</div>', 'text/html')
    ->attach($listFiles[0])
    ->attach($listFiles[1]);

通过邮件发送

echo $mailer->send($messageCyrillicText);

将传输更改为SMTP,更改准备邮件的主题,最后发送

$mailer->setTransport($smtpTransport);

$messageEnglish->subject('Subject English. Send smtp php');
echo $mailer->send($messageEnglish);