Hello,
I've been trying to send email notification through code but it always sets the email to "Archived" not being send.
My code is as follows:
After trying to execute it, it doesn't showing any error code or log, the email is just being archived and not sent. The email template is working as I debug it.
I've been trying to send email notification through code but it always sets the email to "Archived" not being send.
My code is as follows:
PHP Code:
private function sendEmailNotification(&$user) {
$emailAddress = $user->get('emailAddress');
if (!$emailAddress) return;
try {
$email = $this->getEntityManager()->getEntity('Email');
$subject = 'default subject';
$body = "default body";
$isHtml = true;
$attachments = array();
$emailTemplate = $this->getEntityManager()->getRepository('EmailTemplate')->where(array('name'=>'MyTemplate'))->findOne();
if ($emailTemplate) {
$templateParams = array(
'parent' => $user
);
$emailData = $this->getEmailTemplateService()->parseTemplate($emailTemplate, $templateParams);
$subject = $emailData['subject'];
$body = $emailData['body'];
$isHtml = $emailData['isHtml'];
$attachments = $emailTemplate->get('attachments');
}
$email->set(array(
'subject' => $subject,
'body' => $body,
'isHtml' => $isHtml,
'from'=> $this->getConfig()->get('outboundEmailFromAddress'),
'to' => $emailAddress,
'isSystem' => true,
'parentId' => $user->id,
'parentType' => 'User'
));
$params = array(
'fromName'=>$this->getConfig()->get('outboundEmailFromName'),
'fromAddress'=>$this->getConfig()->get('outboundEmailFromAddress'),
'replyToName'=>'None',
'replyToAddress'=>'None'
);
$message = null;
$this->getEntityManager()->saveEntity($email);
if ($this->getConfig()->get('smtpServer')) {
$this->getMailSender()->useGlobal()->send($email, $params, $message, $attachments);
} else {
$this->getMailSender()->useSmtp(array(
'server' => $this->getConfig()->get('internalSmtpServer'),
'port' => $this->getConfig()->get('internalSmtpPort'),
'auth' => $this->getConfig()->get('internalSmtpAuth'),
'username' => $this->getConfig()->get('internalSmtpUsername'),
'password' => $this->getConfig()->get('internalSmtpPassword'),
'security' => $this->getConfig()->get('internalSmtpSecurity'),
'fromAddress' => $this->getConfig()->get('internalOutboundEmailFromAddress', $this->getConfig()->get('outboundEmailFromAddress'))
))->send($email, $params, $message, $attachments);
}
}
catch (\Exception $e) {
$GLOBALS['log']->error("Failed to send email: ['".$e->getCode()."'] ".$e->getMessage());
}
}
Comment