扩展mailer类的问题

来源:6-7 建立计划任务定时离线发送队列中的邮件

铨_430

2017-07-13

老师,我在扩展邮件类的message类里面,使用$parts = $this->getSwiftMessage()->getChildren();得到的$parts得到的数组有两个元素,第一个$part->getContentType()是‘text/html’,第二个是‘text/plain,为什么不是像您一样只有一个是‘text/html’?

写回答

3回答

Jason

2017-07-16

发总邮件的代码,我看一下

0
0

铨_430

提问者

2017-07-14

<?php
namespace doctorjason\mailerqueue;
use Yii;

class Message extends \yii\swiftmailer\Message
{
	public function queue()
	{
		$redis = Yii::$app->redis;
		if(empty($redis)){
			throw new \Exception("reids not found in config", 1);
		}

		$mailer = Yii::$app->mailer;
		if(empty($mailer) || !$redis->select($mailer->db)){
			throw new \Exception("db not defined", 1);
		}
		$message = [];
		$message['from'] = array_keys($this->getFrom());
		$message['to'] = array_keys($this->getTo());
		$message['cc'] = array_keys($this->getCc());
		$message['bcc'] = array_keys($this->getBcc());
		$message['reply_to'] = array_keys($this->getReplyTo());
		$message['charset'] = array_keys($this->getCharset());
		$message['cubject'] = array_keys($this->getSubject());
		$parts = $this->getSwiftMessage()->getChildren();
		if(!is_array($parts) || !sizeof($parts)){
			$parts = [$this->getSwiftMessage()];
		}
		foreach ($parts as $key => $part) {
			if(!$part instanceof \Swift_Mime_Attachment){
				switch ($part->getContentType()) {
					case 'text/html':
						$message['html_body'] = $part->getBody();
						break;
					
					case 'text/plain':
						$message['text_body'] = $part->getBody();
						break;
				}

				
				if(!$message['charset']){
					$message['charset'] = $part->getCharset();
				}
			}
		}
		return $redis->rpush($mailer->key,json_encode($message));
	}
}

mail里面的代码:

<p>恭喜您,您的账号的已经申请成功!</p>

<p>欢迎加入慕课商城!</p>

<p>您的用户名为:<span style="color:red;font-weight:700"><?php echo $username; ?></span></p>

<p>您的密码为:<span style="color:red;font-weight:700"><?php echo $userpass; ?></span></p>

<p>您可以通过改收件箱邮箱地址或者用户名进行登录!</p>

<p>登录地址:<?php echo Yii::$app->urlManager->createAbsoluteUrl(['member/auth']); ?></p>

<p>该邮件是系统自动发送,请勿回复!</p>

mail里面的layout的代码:

<?php
use yii\helpers\Html;

/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html"; charset="<?= Yii::$app->charset ?>" />
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
    <?php $this->beginBody() ?>
    <?= $content ?>
    <?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>


0
0

Jason

2017-07-14

我看下你的代码

0
0

Yii2.0进阶版 高级组件ES/Redis/Sentry优化京东平台

进阶更高阶的Yii 2.0开发,获得2-3年yii 2.0实战经验

514 学习 · 210 问题

查看课程