Email Framework
General Information
The OpenDXP Email Framework provides an easy way to send/create emails with OpenDXP.
For this you have several components:
- Document\Email
- OpenDxp\Mail
OpenDXP provides a OpenDxp\Mail Class which extends the \Symfony\Component\Mime\Email Class.
If email settings are configured in your config/config.yaml then on initializing
OpenDxp\Mail object, these settings applied automatically
It is recommended to configure email settings in config/config.yaml file:
opendxp:
email:
sender:
name: 'OpenDXP Demo'
email: [email protected]
return:
name: ''
email: ''
and debug email addresses should be configured in Admin Settings > System > Debug > Debug Email Addresses.
If the Debug Mode is enabled, all emails will be sent to the Debug Email recipients defined in Settings > System > Debug > Debug Email Addresses. Additionally the debug information (to whom the email would have been sent) is appended to the email and the Subject contains the prefix "Debug email:".
This is done by extending Symfony Mailer, with injected service RedirectingPlugin, which calls beforeSendPerformed before mail is sent and sendPerformed immediately after email is sent.
Emails are sent via transport and \OpenDxp\Mailer requires transports: main for sending emails and opendxp_newsletter for sending newsletters(if newsletter specific settings are used and OpenDXPNewsletterBundle is enabled and installed), which needs to be configured in your config.yaml e.g.,
framework:
mailer:
transports:
main: smtp://user:[email protected]:port
opendxp_newsletter: smtp://user:[email protected]:port
Please refer to the Transport Setup for further details on how this can be set up.
OpenDXP provides a Document Email type where you can define the recipients ... (more information
here) and Twig variables.
To send a email you just create a Email Document in the OpenDXP Backend UI, define the subject,
recipients, add Dynamic Placeholders... and pass this document to the OpenDxp\Mail object. All
nasty stuff (creating valid URLs, embedding CSS, compile Less files, rendering the document..) is
automatically handled by the OpenDxp\Mail object.
In the Settings section of the Email Document you can use Full Username <[email protected]> or Full Username ([email protected]) to set full username.
Usage Example
Lets assume that we have created a Email Document in the OpenDXP Backen UI (/email/myemaildocument)
which looks like this:
To send this document as email we just have to write the following code-snippet in our controller action:
//dynamic parameters
$params = array('firstName' => 'Pim',
'lastName' => 'Core',
'product' => \OpenDxp\Model\DataObject::getById(73613)
);
//sending the email
$mail = new \OpenDxp\Mail();
$mail->to('[email protected]');
$mail->setDocument('/email/myemaildocument');
$mail->setParams($params);
$mail->send();
you can access the parameters in your mail content.
Hello {{ firstName }} {{ lastName }}
Regarding the product {{ product.getName() }} ....
Sending a Plain Text Email:
$mail = new \OpenDxp\Mail();
$mail->to('[email protected]');
$mail->text("This is just plain text");
$mail->send();
Sending a Rich Text (HTML) Email:
$mail = new \OpenDxp\Mail();
$mail->to('[email protected]');
$mail->bcc("[email protected]");
$mail->html("<b>some</b> rich text");
$mail->send();
Sandbox Restrictions
Sending mails renders user controlled twig templates in a sandbox with restrictive security policies for tags, filters & functions. Please use following configuration to allow more in template rendering:
opendxp:
templating_engine:
twig:
sandbox_security_policy:
tags: ['if']
filters: ['upper']
functions: ['include', 'path']