Add view variables in Aimeos\Client\Html\Email\Account\Html

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
timothy_truckle
Posts: 10
Joined: 12 Mar 2018, 14:00

Add view variables in Aimeos\Client\Html\Email\Account\Html

Post by timothy_truckle » 08 Jul 2021, 14:55

Add view variables in Aimeos\Client\Html\Email\Account\Html

Laravel: Laravel Framework 6.20.27
Aimeos: aimeos/aimeos-core 2020.10.25
PHP: PHP 7.4.3
OS: 20.04.1-Ubuntu

I wanted to add some view variables in my custom extension to add (inline)
images to my html-email template.

So copied the lines in:
"ext/extensionname/client/html/src/Client/Html/Email/Account/Html/Standard.php"

from the public function addData() to include some more images in the view.

Code: Select all

$file = $view->config( 'client/html/email/logo', 'client/html/themes/extensionname/media/extension_logo.png' );
                                     
if( file_exists( $file ) && ( $content = file_get_contents( $file ) ) !== false )
{
  $finfo = new \finfo( FILEINFO_MIME_TYPE );
  $mimetype = $finfo->file( $file );

  $view->extension_htmlLogo = $view->mail()->embedAttachment( $content, $mimetype, basename( $file ) );
}
The logo code is just like the original code, i just repeated this code fragment
for some more images using distinct viewVar names and reusing the variable
"$file" after assignment to the view.
Not elegant, but should be working.

I tried to include the images in the template:
"ext/extensionname/client/html/templates/email/account/html-body-standard.php"
like so:

Code: Select all

img .... src="<?= $this->get( 'extension_htmlLogo'); ?>
No errors show up on sending the mail.

None of these images show up in the rendered html template. The src attributes
in the image tags are empty.

What i did before testing:

Code: Select all

php artisan vendor:publish --tag=public --force
php artisan cache:clear
php artisan aimeos:clear
php artisan view:cache
Any ideas? Thank you.

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: Add view variables in Aimeos\Client\Html\Email\Account\Html

Post by aimeos » 11 Jul 2021, 16:16

timothy_truckle wrote: 08 Jul 2021, 14:55

Code: Select all

$file = $view->config( 'client/html/email/logo', 'client/html/themes/extensionname/media/extension_logo.png' );
if( file_exists( $file ) && ( $content = file_get_contents( $file ) ) !== false )
Pretty sure that "client/html/themes/extensionname/media/extension_logo.png" isn't found because it's a relative path and it's relative to the directory you are executing the "php artisan aimeos:jobs" command. Guess, it doesn't exisist. This is also often a problem when a custom logo should be included in e-mails. In you case, you should use:

Code: Select all

$file = $view->config( 'client/html/email/extlogo' );
and in your config/shop.php:

Code: Select all

'client' => [
	'html' => [
		'email' => [
			'extlogo' => public_path( 'media/extension_logo.png' )
		]
	]
]
timothy_truckle wrote: 08 Jul 2021, 14:55

Code: Select all

img .... src="<?= $this->get( 'extension_htmlLogo'); ?>
No errors show up on sending the mail.
If the view variable "extension_htmlLogo" isn't defined this code will return NULL so it's empty.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

timothy_truckle
Posts: 10
Joined: 12 Mar 2018, 14:00

Re: Add view variables in Aimeos\Client\Html\Email\Account\Html

Post by timothy_truckle » 20 Jul 2021, 23:09

Problem is solved!

- This works for aimeos 2010 and laravel 6 (most likely also for 7 and 8).
- I am using a local composer.phar in the aimeos directory.
- The name of my extension is mms.
- I am working on linux and use some scripts during development, i will leave
them here for convenience. To help others and maybe myself in the future, i want
elaborate on the matter:


I. Create an aimeos extension as described in the online help.

See: https://aimeos.org/docs/2020.x/developer/extensions/

Place the extension in your aimeos installation in the ext directory.


II. Edit the composer.json

of the extension, adapt the section classmap, here the extensions name is "mms"
like so:

Code: Select all

"classmap": [
  "ext/mms/client/html/src",
  "ext/mms/client/jsonapi/src",
  "ext/mms/admin/jqadm/src",
  "ext/mms/admin/jsonadm/src",
  "ext/mms/controller/common/src",
  "ext/mms/controller/frontend/src",
  "ext/mms/controller/jobs/src",
  "ext/mms/lib/custom/src"
]
After that, cd to the base directory of your aimeos installation and issue:

Code: Select all

php composer.phar dump-autoload
to update ./vendor/composer/autoload_classmap.php and
./vendor/composer/autoload_static.php. This makes sure the classes defined in
the directories in composer.json will be found by aimeos.


III. Prepare image(s) and path

To add one or more images to your html emails, you obviously need to place them
in the filesystem. I wanted to use a short path to the images, so i used the
laravel artisan cli command, in the base directory of your aimeos installation

Code: Select all

php artisan storage:link
This creates a symbolic link ./public/storage pointing to ./storage/app/public/

Place blueglow.jpg in this directory, this is the image that we want to show up
in the html email.


IV. Create Class and Template

I demonstrate the process by changing the class and template used for customer
account creation notification email. This email will be sent to the new customer
if one places an order in the shop and checks the option to create a new user
account in the last checkout step.

To use adapted classes and templates for emails, you need to to create the
related classes and templates.

Lets first copy the Standard Html Email Class to a new file

Still in the base directory of the aimeos installation issue:

Code: Select all

cp ext/mms/client/html/src/Client/Html/Email/Account/Html/Standard.php ext/mms/client/html/src/Client/Html/Email/Account/Html/Basemms.php
Now, for the template use:

Code: Select all

cp ext/mms/client/html/templates/email/account/html-body-standard.php ext/mms/client/html/templates/email/account/html-body-Basemms.php 

V. Edit Class

I wanted to add images to the html email, so now we need to edit
ext/mms/client/html/src/Client/Html/Email/Account/Html/Basemms.php

Change the Classname in the newly created file:
ext/mms/client/html/src/Client/Html/Email/Account/Html/Basemms.php
to read

Code: Select all

/**
 * Default implementation of account creation e-mail html HTML client.
 *
 * @package Client
 * @subpackage Html
 */
class Basemms
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
{
further in:

Code: Select all

public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
add:

Code: Select all

/** client/html/email/extblue
 * Path to the blueglow image displayed in HTML e-mails
 *
 * The path can either be a relative (to the aimeos installation directory) or an URL to a file on a
 * remote server. If the file is stored on a remote server, "allow_url_fopen"
 * must be enabled. See {@link http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen php.ini allow_url_fopen}
 * documentation for details.
 *
 * @param string Relative file system path to the blueglow image
 * @since 2020.10 mms-extension
 * @category User
 * @see client/html/email/from-email
 * @config string client/html/email/extblue
 */
$fileconf = $view->config( 'client/html/email/extblue', 'public/storage/blueglow.jpg' );
$file = base_path().DIRECTORY_SEPARATOR.$fileconf;
if( file_exists( $file ) && ( $content = file_get_contents( $file ) ) !== false )
{
  $finfo = new \finfo( FILEINFO_MIME_TYPE );
  $mimetype = $finfo->file( $file );
  $view->extBlue = $view->mail()->embedAttachment( $content, $mimetype, basename( $file ) );
}
You can change the image used in ./config/shop.php, see VII.


VI. Edit Template

To display the image in the html Mail add in
./ext/mms/client/html/templates/email/account/html-body-Basemms.php

for example:

Code: Select all

<img align="center" border="0" class="center fixedwidth" 
  src="<?= $this->get( 'extBlue' ); ?>" 
  style="text-decoration: none; -ms-interpolation-mode: bicubic; height: auto; border: 0; width: 100%; max-width: 150px; display: block;" width="150" 
/>

VII. Edit configuration

Now adapt your ./config/shop.php. I tuned the numbers in controller/frontend
to not run into time limits during development. You may want to delete the
relevant section or use original numbers after the extension is done and you go
into production with the shop.

I'll show the relevant sections:

Code: Select all


'client' => [
  'html' => [
    'common' => [
      'template' => [
         'baseurl' => 'packages/aimeos/shop/themes/mms',
      ],
    ],
    'email' => [
      'from-email' => 'demo@mms.io',
      'from-name' => 'Demo MMS Shop',
      // 'extblue' => 'public/storage/blueglow.jpg',
      'account' => [
        'html' => [
          'name' => 'Basemms',
          'standard' => [
            'template-body' => 'email/account/html-body-Basemms',
          ],
        ],
      ],
    ],
  ],
],

'controller' => [
  'frontend' => [
    // limits the purchases one ip can place (limit-count) 
    // in the timeframe (limit-seconds)
    'basket' => [
      'limit-seconds' => 60, // original 900 
      'limit-count' =>  20,  // original 5
    ],
    // limits the customers one ip can create (limit-count) 
    // in the timeframe (limit-seconds)
    'customer' => [
      'limit-seconds' => 60, // original 14400 
      'limit-count' =>  20,  // original 3
    ],
    // limits the orders/invoices one ip can create (limit-count) 
    // in the timeframe (limit-seconds)
    'order' => [
      'limit-seconds' => 60, // original 900 
      'limit-count' =>  20,  // original 3
    ],
  ],
],

VIII. Closing Remarks, scripts, crontab

after making changes to classes, css and templates, its a good idea to clear
caches, i use the following script, placed in the aimeos base directory. This
will clear all caches and re-publish your extension.

Code: Select all

#!/bin/bash
php artisan config:cache;
php artisan cache:clear; 
php artisan aimeos:clear; 
php artisan view:cache; 
php artisan vendor:publish --tag=public --force;
crontab, assuming aimeos (base directory) is at /home/mms/www/shopnew.live:

Code: Select all

# m h  dom mon dow   command
# every 5 minutes start the laravel queues
*/5 * * * * /usr/bin/php /home/mms/www/shopnew.live/artisan queue:work database --tries=3 --stop-when-empty
# Aimeos queues
# every minute
* * * * * /usr/bin/php /home/mms/www/shopnew.live/artisan aimeos:jobs "order/export/csv order/email/delivery order/email/payment order/email/voucher order/service/delivery subscription/export/csv customer/email/account"
# every hour at half the hour
30 * * * * /usr/bin/php /home/mms/www/shopnew.live/artisan aimeos:jobs "customer/email/watch order/cleanup/unfinished order/service/async order/service/payment"
# once a day at 01:00
0 1 * * * /usr/bin/php /home/mms/www/shopnew.live/artisan aimeos:jobs "admin/cache admin/log catalog/import/csv order/cleanup/unpaid product/import/csv product/bought index/rebuild index/optimize product/export/sitemap subscription/process/begin subscription/process/renew subscription/process/end"
Hope this helps, thanks Aimeos for the great package and thanks for reading!

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: Add view variables in Aimeos\Client\Html\Email\Account\Html

Post by aimeos » 21 Jul 2021, 06:57

Instead of extending the HTML client class, you can also create a decorator which is less code and can be combined with other decorators adding more features:
https://aimeos.org/docs/latest/frontend ... omponents/
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply