I try to create emails for next sending with my cron php script.
And I use Zend_View for rendering email template.
I have 50k users but 3000 emails was created with 64MB memory limit and 7200 with 128MB.
Code of rendering emails
public function prepareEmailBody($template, $templates)
{
$view = new Zend_View(array('basePath' => './application/views'));
$template_file_name = $template . '.phtml';
foreach ($templates as $key => $value) {
$view->$key = $value;
}
$body = $view->render('mails/' . $template_file_name);
return $body
}
And use this method in
foreach ($users as $user) {
.....
$text = Mailer::getInstance()->prepareEmailBody($template, $vars);
.....
}
Please advice how optimize code.
You could try using one View object and the partial helper instead, this might improve things (or might make it slower).
public function getView()
{
if (!$this->_view) {
$this->_view = new Zend_View(array('basePath' => './application/views'));
}
return $this->_view;
}
public function prepareEmailBody($template, $templates)
{
$template_file_name = $template . '.phtml';
$body = $this->getView()->partial($template_file_name, $templates);
return $body
}
Related
Please check this code for the custom block to be placed in the dashboard. We want to display an HTML table for the records. But it does not add up to the custom block, rather it appears on the top page.
enter image description here
class block_scorecard extends block_base {
function init() {
$this->title = get_string('pluginname', 'block_scorecard');
}
function get_content() {
global $DB;
if ($this->content !== NULL) {
return $this->content;
}
$content = '';
$courses = $DB->get_records('scorm_scoes_track', ['element' => 'cmi.core.total_time']);
$table=new html_table();
$table->head=array('No of attempts','Time modified','status');
foreach ($courses as $course) {
$attempt=$course->attempt;
$timemodified=userdate($course->timemodified, get_string('strftimerecentfull'));
$status=$course->value;
$table->data[]=array($attempt, $timemodified, $status);
}
echo html_writer::table($table);
$this->content = new stdClass;
$this->content->text = $content;
}}
echo html_writer::table($table);
Should be
$content .= html_writer::table($table);
Im struggling to make a code that sends email to my subscribed users. I want to pass body to view according to users default language, can anyone help me ?
My code:
if($newsletter->save())
{
//get users to send to
$users = User::where('newsletter', '=', '1')->Where('activated', '=', '1')->get();
//Send to all users subscribed
foreach($users as $user)
{
//set info according to user default lang
if($user->default_lang == 'pt')
{
$body = $newsletter_pt;
$subject = Input::get('subject_PT');
}
elseif($user->default_lang == 'de')
{
$body = $newsletter_de;
Input::get('subject_DE');
}
$data = array(
'body' => $body,
'subject' => $subject
);
$from_name = Input::get('from_name');
$from_email = Input::get('from_email');
//QUEUE The Newsletters to send
Mail::queue('admin.newsletters.template1', $data, function($message) use ($user, $subject, $from_name, $from_email)
{
$message->from($from_email, $from_name);
$message->bcc($user->email, $user->name);
$message->subject($subject);
});
} //end foreach
return Redirect::To('admin/newsletters')->with('message', array("1" => "Newsletter enviada com sucesso !"));
}
Thanks a lot ;)
Ensure that the $data array has the information you expect to pass into the view before you call Mail::queue(). It should pass both a $body and $subject variable to your view.
You might also try using Mail::send() instead of Mail::queue() and see if it makes a difference (if you're not using a queueing system). Sometimes the queue() method can trip up when serializing Eloquent models, so if $newsletter_pt or $newsletter_de are Eloquent models they might not make it to the view intact.
In fuelphp, we can render template from controller. But I want prevent render template from package.
Example:
Step 1: fuelphp run controlelr -> render template
Step 2: run package -> have a command to clear all data in step 1. and
render blank page.
Result with a blank page
$this->template->content = ...
\Package::removeTemplate();
I tried with
\Event::forge(array('shutdown'));
\Fuel::finish();
But it is not success. How can I do it?
You can always modify your template, inside the controller in every function just use
$this->template
Example
class Controller_lorem extends Controller_Main {
public $template = 'template_default';
public function action_ipsum()
{
//use customize template other than default
$this->template = View::forge('template_custom');
}
I found a solution. Rewrite \Fuel::finish()
public static function finishS()
{
if (\Config::get('caching', false))
{
\Finder::instance()->write_cache('FuelFileFinder');
}
if (static::$profiling and ! static::$is_cli)
{
// Grab the output buffer and flush it, we will rebuffer later
$output = ob_get_clean();
$headers = headers_list();
$show = true;
foreach ($headers as $header)
{
if (stripos($header, 'content-type') === 0 and stripos($header, 'text/html') === false)
{
$show = false;
}
}
if ($show)
{
\Profiler::mark('End of Fuel Execution');
if (preg_match("|</body>.*?</html>|is", $output))
{
$output = preg_replace("|</body>.*?</html>|is", '', $output);
$output .= \Profiler::output();
$output .= '</body></html>';
}
else
{
$output .= \Profiler::output();
}
}
// Restart the output buffer and send the new output
ob_start();
**/// Remove this line echo $output;**
}
}
I'm trying to add some flash messages after user registered
$sharedManager->attach('ZfcUser\Service\User', 'register.post', function($e) use($serviceManager) {
$user = $e->getParam('user');
$mail = new Mail\ActivateAccount($serviceManager, $user);
$mail->send($user->getEmail());
$flash = $serviceManager->get('ControllerPluginManager')->get('flashMessenger');
$flash->addSuccessMessage('Check your mailbox, please.');
});
However, after I was redirected to login page I didn't see any message. Anybody knows the reason?
There is view helper that displays messages
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\View\Helper\FlashMessenger;
class ShowMessages extends AbstractHelper
{
public function __invoke()
{
$messenger = new FlashMessenger();
$error_messages = $messenger->getErrorMessages();
$messages = $messenger->getSuccessMessages();
$result = '';
if (count($error_messages)) {
foreach ($error_messages as $message) {
$result .= '<p class="alert alert-danger">' . $message . '</p>';
}
}
if (count($messages)) {
foreach ($messages as $message) {
$result .= '<p class="alert alert-success">' . $message . '</p>';
}
}
return $result;
}
}
On other pages flash messages works fine.
Thanks!
I found my mistake. I created new messenger instead of getting it throuhg service locator.
$messenger = new FlashMessenger();
should be
$messenger = $this->sm->getServiceLocator()->get('ControllerPluginManager')->get('flashMessenger');
Is there any class in the Zend Framework that allows me to easily read emails?
The Zend_Mail class does allow me to easy get headers, subject and the content body. But transferring everything to UTF-8 and human-readable format is still a pain.
Or am I doing something wrong? As far as I can tell, Zend Framework does not allow me to easily get UTF-8 strings that I can just use, I still have to do some post-processing. Right?
The key thing is that you need to iterate over the parts within the Message and find the text. Once you have it, then you can use quoted_printable_decode to get the text itself in a useful way.
This is some rough and ready code that reads IMAP email boxes with Zend_Mail:
<?php
$mail = new Zend_Mail_Storage_Imap(array(
'host' => EMAIL_ACCOUNT_HOST,
'user' => EMAIL_ACCOUNT_USERNAME,
'password' => EMAIL_ACCOUNT_PASSWORD,
));
echo (int)$mail->countMessages() . " messages found\n";
foreach ($mail as $message) {
$from = $message->getHeader('from');
$subject = trim($message->subject);
$to = trim($message->to);
$body = getBody($message);
// do something with message here
}
function getBody(Zend_Mail_Message $message)
{
// find body
$part = $message;
$isText = true;
while ($part->isMultipart()) {
$foundPart = false;
$iterator = new RecursiveIteratorIterator($message);
foreach ($iterator as $part) {
// this detection code is a bit rough and ready!
if (!$foundPart) {
if (strtok($part->contentType, ';') == 'text/html') {
$foundPart = $part;
$isText = false;
break;
} else if (strtok($part->contentType, ';') == 'text/plain') {
$foundPart = $part;
$isText = true;
break;
}
}
}
if($foundPart) {
$part = $foundPart;
break;
}
}
$body = quoted_printable_decode($part->getContent());
}