I have a private function sendEmail
in my actions class.
private function sendEmail($args)
{
$mailer = sfContext::getInstance()->getMailer();
$message = $this->getMailer()->compose();
$address = $this->getFromAddress();
$message->setFrom(array($address['email'] => $address['fullname']));
$message->setTo('blah@blah.com');
$message->setSubject('Subject');
$message->setBody($this->getPartial('emailTemplate', array('args' => $args)), 'text/html');
$this->getMailer()->send($message);
}
I call that function from my Register
action:
public function executeRegister(sfRequest $request)
{
sfConfig::set('app_sfForkedApply_from', array(
'email' => 'blah@blah.com',
'fullname' => 'fullname'
));
//code for registering
try {
// send email
$this->sendEmail($args);
}
catch (Exception $e) {
$this->logMessage('Email not sent: ' . $e->getMessage(), 'err');
}
$out = array('status' => 'success', 'args' => args);
}
}
$this->getResponse()->setContentType('application/json');
$this->getResponse()->setContent(json_encode($out));
return sfView::NONE;
}
I now want to call the sendEmail
function from a different action, Register2
within the same action class.
public function executeRegister2(sfRequest $request)
{
sfConfig::set('app_sfForkedApply_from', array(
'email' => 'blah@blah.com',
'fullname' => 'fullname'
));
//code for registering #2
if ($var = true) {
try {
// sending different email
$this->sendVerificationMail($args);
$out = array(
'status' => 'success',
'message' => 'Verification email sent'
);
}
catch (Exception $e) {
$this->logMessage('Verification email not sent: ' . $e->getMessage(), 'err');
$out = array(
'status' => 'error',
'message' => 'Verification email not sent: ' . $e->getMessage()
);
}
}
else {
//do other stuff
$out = array(
'status' => 'success',
'message' => 'User activated'
);
}
//added following try-catch to send new email but its not working
try {
// send email
$this->sendEmail($args);
}
catch (Exception $e) {
$this->logMessage('Email not sent: ' . $e->getMessage(), 'err');
}
$this->getResponse()->setContentType('application/json');
$this->getResponse()->setContent(json_encode($out));
return sfView::NONE;
}
From the Register
action it works.
From the Register2
action, it returns the following error:
Email not sent: The template "_emailTemplate.json.php" does not exist or is unreadable in "".
How could it be that it finds the template if I call the function from one action but not from the other?
Does it always turn templates into json files? Can that be the problem?
sfView
use your Request Format to get the name of the template. So if your action executeRegister2
is Json, it looks for a .json.php
file.
You can cheat, in the sendMail method:
private function sendEmail($args)
{
$mailer = sfContext::getInstance()->getMailer();
// Added
$request = sfContext::getInstance()->getRequest();
$format = $request->getRequestFormat();
$request->setRequestFormat('html');
$message = $this->getMailer()->compose();
$address = $this->getFromAddress();
$message->setFrom(array($address['email'] => $address['fullname']));
$message->setTo('blah@blah.com');
$message->setSubject('Subject');
$message->setBody($this->getPartial('emailTemplate', array('args' => $args)), 'text/html');
$this->getMailer()->send($message);
// Revert the request format
$request->setRequestFormat($format);
}
That should do the trick (that being said, sending email while a request can take time, and mess up with your request format: one more reason to make them asynchronous).