如何使用MailSo库通过SMTP发送电子邮件(PHP)

时间:2018-01-10 20:10:14

标签: php email smtp

我很难使用名为MailSo的PHP库通过SMTP发送多部分MIME邮件。只提供了两个例子。没有关于如何创建标题,邮件正文,多部分MIME邮件本身然后发送它的消息。

目前的网络邮件(Rainloop)正在MailSo上运行,我想避免在MailSo上使用第三方库。继续前进所有电子邮件操作都存储在Rainloop Actions.php文件中。

基于创建多部分MIME消息,我应该创建$oMessage对象(\MailSo\Mime\Message),我可以部分地这样做,就像添加主题,消息ID,自定义标题,消息正文但是进一步我无法设置MIME边界(将原始邮件正文存储为边界以及将其他内容类型存储为text / plain),而不是通过SMTP发送$oMessage对象。

到目前为止,这是我的测试代码:

include 'lib/MailSo/MailSo.php';

echo '<pre>';
$oLogger = \MailSo\Log\Logger::SingletonInstance()
    ->Add(\MailSo\Log\Drivers\Inline::NewInstance("\r\n", true))
;

$sToEmails = 'Me As Tester <tester@test.com>';
$oToEmails = \MailSo\Mime\EmailCollection::NewInstance($sToEmails);

$sFromEmails = 'Baba Ganush <no-replay@test.com>';
$oFromEmails = \MailSo\Mime\Email::NewInstance($sFromEmails);

$oMessage = \MailSo\Mime\Message::NewInstance();
$oMessage->RegenerateMessageId();

$oMessage->SetXMailer('RainLoop/1.0.0');

$oMessage->SetCustomHeader('test-header','test-header-value');

$oMessage->setSubject("Test message");
$oMessage->AddText('Generated message body goes here...');

$oMessage->SetFrom($oFromEmails);
$oMessage->SetTo($oToEmails);

$oLogger->WriteDump($oMessage);

1 个答案:

答案 0 :(得分:0)

好吧,我已经想出如何发送使用MailSo库创建的电子邮件(暂时没有附件)

以下示例代码

        if($oMessage){
            $rMessageStream = \MailSo\Base\ResourceRegistry::CreateMemoryResource();
            $iMessageStreamSize = \MailSo\Base\Utils::MultipleStreamWriter($oMessage->ToStream(true), array($rMessageStream), 8192, true, true, true);
        }

        $aToCollection = $oMessage->GetTo();
        if ($aToCollection && $oFrom)
        {
            $sRawBody = @stream_get_contents($rMessageStream);

            if (!empty($sRawBody))
            {
                $sMailTo = trim($aToCollection->ToString(true));
                $sMailSubject = trim($oMessage->GetSubject());
                $sMailSubject = 0 === strlen($sMailSubject) ? '' : \MailSo\Base\Utils::EncodeUnencodedValue(\MailSo\Base\Enumerations\Encoding::BASE64_SHORT, $sMailSubject);
                $sMailHeaders = $sMailBody = '';
                list($sMailHeaders, $sMailBody) = explode("\r\n\r\n", $sRawBody, 2);
                unset($sRawBody);
                $sMailHeaders = \MailSo\Base\Utils::RemoveHeaderFromHeaders($sMailHeaders, array(\MailSo\Mime\Enumerations\Header::TO_,\MailSo\Mime\Enumerations\Header::SUBJECT));
                mail($sMailTo, $sMailSubject, $sMailBody, $sMailHeaders);
            }
        }