Skip to content

Instantly share code, notes, and snippets.

@Doopin
Created September 16, 2016 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Doopin/6e33cd04d16010c9a4e3416341489243 to your computer and use it in GitHub Desktop.
Save Doopin/6e33cd04d16010c9a4e3416341489243 to your computer and use it in GitHub Desktop.
Zend raises an exception if my email subject contains non-ASCII charcters. How to solve it please?
/*
* Here is one method of my email service.
* When the email subject contains non ASCII characters, its raised and exeption "invalid subject detected"
* It works like a charm if subject only contains ASCII characters
*/
/**
* Send an email
* @param \xxxxx\Model\Interfaces\Messages\EmailInterface $emailObject
* @return boolean
*/
public function send(EmailInterface $emailObject) {
// return false;
// \Zend\Debug\Debug::dump($emailObject); exit;
$emailMessage = new Message(); // Zend\Mail\Message
$emailMessage->setEncoding('UTF-8');
$emailMessage->setSubject($emailObject->getEmailSubject());
$emailMessage->setFrom($emailObject->getEmailFromEmail(), $emailObject->getEmailFromName());
$emailMessage->setTo($emailObject->getEmailToEmail(), $emailObject->getEmailToName());
$emailMessage->setBody($this->getEmailBody($emailObject->getEmailBodyHtml()));
$transport = new SmtpTransport(); // Zend\Mail\Transport\Smtp as SmtpTransport
$transport->setOptions($this->smtpOptions);
$emailObject->setEmailSentStatus(Email::STATUS_PENDING);
$savedEmail = $this->mapper->addRow($emailObject);
if($savedEmail['status']) {
$emailObject = $savedEmail['object'];
try {
/*
* ZEND SMTP TRANSPORT doesn't return a result on success
* All we know is an exception is thrown on failed.
* This means the email status in the database will be set to 'failed' if an exception is raised
*/
$transport->send($emailMessage);
$emailObject->setEmailSentStatus(Email::STATUS_SENT);
} catch (\Exception $e) {
$emailObject->setEmailSentStatus(Email::STATUS_FAILED);
$emailObject->setEmailFailedReason($e->getMessage());
// Log error $e
}
$this->mapper->updateRow($emailObject, [
'updated' => Utils::timestamp(),
'email_sent_status' => $emailObject->getEmailSentStatus(),
'email_failed_reason' => $emailObject->getEmailFailedReason()
], [
Configs::CONFIG_SEARCH_CRITERIA_POSITION_EQUAL => [
'column' => $emailObject->tablePrimaryKey(),
'value' => $emailObject->getEId()
]
]);
return $emailObject->getEmailSentStatus() == Email::STATUS_SENT;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment