Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Fix broken App Engine mail and corrupt message body in SendMail
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Snape committed Oct 9, 2015
1 parent 0d8d52e commit 6afd416
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
60 changes: 60 additions & 0 deletions library/Midas/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class Midas_Mail extends Zend_Mail
/** @var array */
protected $_cc = array();

/** @var false|string */
protected $_unencodedBodyText = false;

/** @var false|string */
protected $_unencodedBodyHtml = false;

/**
* Add one or more "BCC" email addresses.
*
Expand Down Expand Up @@ -99,4 +105,58 @@ public function getTo()
{
return $this->_to;
}

/**
* Return the unencoded HTML message body.
*
* @return string unencoded HTML message body
*/
public function getUnencodedBodyHtml()
{
return $this->_unencodedBodyHtml;
}

/**
* Return the unencoded plain text message body.
*
* @return string unencoded plain text message body
*/
public function getUnencodedBodyText()
{
return $this->_unencodedBodyText;
}

/**
* Set the HTML message body.
*
* @param string $html HTML message body
* @param string $charset message character set
* @param string $encoding message encoding
* @return $this this mail instance
*/
public function setBodyHtml($html, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
{
parent::setBodyHtml($html, $charset, $encoding);

$this->_unencodedBodyHtml = $html;

return $this;
}

/**
* Set the plain text message body.
*
* @param string $text plain text message body
* @param string $charset charset
* @param string $encoding encoding
* @return $this this mail instance
*/
public function setBodyText($text, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
{
parent::setBodyText($text, $charset, $encoding);

$this->_unencodedBodyText = $text;

return $this;
}
}
10 changes: 8 additions & 2 deletions library/Midas/Service/AppEngine/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,17 @@ public function sendMail(Midas_Mail $mail)
$this->_client->addBcc($mail->getBcc());
$this->_client->addCc($mail->getCc());
$this->_client->addTo($mail->getTo());
$this->_client->setHtmlBody($mail->getBodyHtml(true));
$htmlBody = $mail->getUnencodedBodyHtml();
if ($htmlBody !== false) {
$this->_client->setHtmlBody($htmlBody);
}
$this->_client->setReplyTo($mail->getReplyTo());
$this->_client->setSender($mail->getFrom());
$this->_client->setSubject($mail->getSubject());
$this->_client->setTextBody($mail->getBodyText(true));
$textBody = $mail->getUnencodedBodyText();
if ($textBody !== false) {
$this->_client->setTextBody($textBody);
}

try {
$this->_client->send();
Expand Down
10 changes: 8 additions & 2 deletions library/Midas/Service/SendGrid/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,17 @@ public function sendMail(Midas_Mail $mail)
$email = new \SendGrid\Email();
$email->setBccs($mail->getBcc());
$email->setCcs($mail->getCc());
$email->setHtml($mail->getBodyHtml(true));
$html = $mail->getUnencodedBodyHtml();
if ($html !== false) {
$email->setHtml($html);
}
$email->setFrom($mail->getFrom());
$email->setReplyTo($mail->getReplyTo());
$email->setSubject($mail->getSubject());
$email->setText($mail->getBodyText(true));
$text = $mail->getUnencodedBodyText();
if ($text !== false) {
$email->setText($text);
}
$email->setTos($mail->getTo());

try {
Expand Down
4 changes: 3 additions & 1 deletion modules/mail/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public function handleSendMailMessage($params)
}

$mail = new Midas_Mail();
$mail->setFrom(htmlspecialchars($this->Setting->getValueByName(MAIL_FROM_ADDRESS_KEY, $this->moduleName), ENT_QUOTES, 'UTF-8'));
$from = htmlspecialchars($this->Setting->getValueByName(MAIL_FROM_ADDRESS_KEY, $this->moduleName), ENT_QUOTES, 'UTF-8');
$mail->setFrom($from);
$mail->setReplyTo($from);

if (isset($params['bcc'])) {
$mail->addBcc(htmlspecialchars($params['bcc'], ENT_QUOTES, 'UTF-8'));
Expand Down

0 comments on commit 6afd416

Please sign in to comment.