Forums / Developer / how to send an object into email

how to send an object into email

Author Message

ludo thomas

Monday 04 July 2005 6:20:59 am

hi all,
I'm trying to send an obect (image+text) into an email.
problem is in the mail's body.

I try this: $body='blablablabla' ::It works fine
Now I want to connect a template to send the content of my objectID=305 (e.g)(I have already a template:newsletter.tpl)

How can I do this?
thanks in advance.

Daniel Beyer

Monday 04 July 2005 8:07:50 am

Hi,

we had a similar problem with html-tags in emails send out. This thread might help you:
http://ez.no/community/forum/developer/xhtml_tags_emails

At the moment the mail-lib of eZ publish doesn't support attachments (correct me if I'm wrong). So it will not be possible to attach an image to an email. But you could add a link to that image in the body of your mail or send out a html formated email that refers the image.

Daniel Beyer
_________________________________
YMC AG
Kreuzlingen, Switzerland
web: www.ymc.ch
____________________________________

ludo thomas

Monday 04 July 2005 9:29:11 am

Hi Daniel.
perhaps you could help me:

You said that you were using <b>attribute_view_gui</b>
so, I think It is in a template.

In my case, I'm working in a workflow (so in php),
and I use : <b>$mailResult = eZMailTransport::send( $mail )</b>

I dont know how to call for a template in my body var.

Daniel Beyer

Monday 04 July 2005 9:51:12 am

Hi there,

I thought you already made use of a template. Using templates for emails in eZ publish is quite easy.

Here is some example code:

//includes - remove them if you done that before
include_once( "kernel/common/template.php" );
include_once( 'lib/ezutils/classes/ezmail.php' );
include_once( 'lib/ezutils/classes/ezmailtransport.php' );

//init the template engine
$mail_tpl =& templateInit();

//set some variables for the template
$mail_tpl->setVariable( 'object', $just_a_content_object_for_demo );

//set template result
$mailTemplateResult =& $mail_tpl->fetch( 'design:path/to/newsletter.tpl' );

//some mail things...
$mail->setSender( "anybody@example.com" );
$mail->setReceiver( "anotherone@example.com );
$mail->setSubject( "Ignore this mail" );

//fill the mail's body with the template
$mail->setBody( $mailTemplateResult );

//send the mail
$mailResult = eZMailTransport::send( $mail );

Don't forget to change some things in this code like:
-"path/to/newsletter.tpl"
-"anybody@example.com"
-"$just_a_content_object_for_demo"
-...

Daniel Beyer
_________________________________
YMC AG
Kreuzlingen, Switzerland
web: www.ymc.ch
____________________________________

ludo thomas

Tuesday 05 July 2005 3:19:11 am

hello daniel.
thank you for your clear answer.
But It seems that I have a blank body in the email I receive.

I try this:

$node='217';
$mail_tpl =& templateInit();
$mail_tpl->setVariable( 'node', $node );
$mailTemplateResult =& $mail_tpl->fetch( 'design:newsletter/newsletter.tpl' );

Can you see any error ?

In my template I have:

<h1>{$node.object.data_map.titre.content|wash()}</h1>

Daniel Beyer

Tuesday 05 July 2005 4:01:42 am

Hi,

there are three possible reasons for that:
1. The path "newsletter/newsletter.tpl" to the template is wrong:
Check if everything is right with it. And your template is in design/YOURDESIGN/newsletter/newsletter.tpl. And the design "YOURDESIGN" is used by your siteaccess.

2. You didn't cleaned your cache:
You need to clean your cache everytime you add a new template to your system - and if you didn't disabled some parts of your cache, everytime your modified a template, too.

3. $node='217' isn't a object:
The variable "$node" that you set in your template is just a string. But you use it like a contentnode-object in it. This won't display anything. Fetch the node either in php or in the template.

Fetching in php:

$nodeID='217';

//include - remove it if you done that before
include_once( 'kernel/classes/ezcontentobjecttreenode.php' );

//fetch the node-object of $nodeID
$node =& eZContentObjectTreeNode::fetch( $nodeID );

$mail_tpl =& templateInit();
$mail_tpl->setVariable( 'node', $node );
$mailTemplateResult =& $mail_tpl->fetch( 'design:newsletter/newsletter.tpl' );

Fetch the node in the template:

First change your php (to avoid confusion):
$nodeID='217';
$mail_tpl =& templateInit();
$mail_tpl->setVariable( 'node_id', $nodeID );
$mailTemplateResult =& $mail_tpl->fetch( 'design:newsletter/newsletter.tpl' );


Than change your template:
{default node=fetch( 'content', 'node', hash( node_id, $node_id ) )}
<h1>{$node.object.data_map.titre.content|wash()}</h1>
{/default}

Daniel Beyer
_________________________________
YMC AG
Kreuzlingen, Switzerland
web: www.ymc.ch
____________________________________

ludo thomas

Tuesday 05 July 2005 7:14:05 am

here is my workflow extract of code(about email body)

$nodeID='217';
	$node =& eZContentObjectTreeNode::fetch( $nodeID );
			
	$mail_tpl =& templateInit();
	$mail_tpl->setVariable( 'node_id', $node );
	$mailTemplateResult =& $mail_tpl->fetch( 'design:newsletter/newsletter.tpl' );

an here the complete template code which is in:design/plain/override/template/newsletter/newsletter.tpl

{default node=fetch( 'content', 'node', hash( node_id, $node_id ) )}

<h1>{$node.object.data_map.titre.content|wash()}
blablablabla
</h1>
{/default}

I hope I could have at least "blablablabla" in my email (that I recieve).
but never nothing.....
It seems that the template is never read.

please help!

Daniel Beyer

Tuesday 05 July 2005 8:14:57 am

You still have some issues left:

1. Use this in your php:

$nodeID='217';
	$node =& eZContentObjectTreeNode::fetch( $nodeID );
			
	$mail_tpl =& templateInit();
	$mail_tpl->setVariable( 'node_id', $nodeID );
	$mail_tpl->setVariable( 'node', $node );
	$mailTemplateResult =& $mail_tpl->fetch( 'design:newsletter/newsletter.tpl' );

//debug - uncomment next 4 lines if you want to see what should be in the body
//echo "This is your Mailbody:<plaintext>";
//print_r( $mailTemplateResult );
//echo "</plaintext>";
//exit();

2. Use this in your template

Already fetched in php:
<h1>{$node.object.data_map.titre.content|wash()}</h1>

{default tpl_node=fetch( 'content', 'node', hash( node_id, $node_id ) )}
Fetch via Template:
<h1>{$tpl_node.object.data_map.titre.content|wash()}</h1>
{/default}

3. Move your template to the correct place:

From:
design/plain/override/template/newsletter/newsletter.tpl

To:
design/plain/templates/newsletter/newsletter.tpl

(BTW: You forgot the "s" after "template")
You shouldn't place your template in the override-folder as you don't make use of any override-rules for that template. Storing it at "design/plain/override/templates/newsletter/newsletter.tpl" might work, but this isn't the appropriate place for it, as long as you don't have a template in "design/plain/override/template/newsletter/newsletter.tpl", which you need to override for a specific case.
In fact the logical correct place for the template would be in "design/standard/templates/newsletter/newsletter.tpl", as eZ publish will use it then as fallback for all other designs you ever will use.
Then you can adapt this template for your specific design (or needs) by copying it to "/design/YOURDESIGN/templates/newsletter/newsletter.tpl". And if you then want to have even an other template for a e.g. specific node you can make use of the override-system (which has to be enabled on php-level in your code first). Only than "/design/YOURDESIGN/override/templates/..." would be the right place for the template.

4. Clean your cache

Daniel Beyer
_________________________________
YMC AG
Kreuzlingen, Switzerland
web: www.ymc.ch
____________________________________

ludo thomas

Tuesday 05 July 2005 9:21:25 am

I'm sorry, but I just receive
<b>Fatal error</b>: eZ publish did not finish its request

I put all my code here.
If somebody could have a look it will be great.

<?php 

include_once( "kernel/classes/ezworkflowtype.php" ); 
define( "EZ_WORKFLOW_TYPE_MAIL_INSCRIPTION", "mailinscription");
class mailInscriptionType extends eZWorkflowEventType
{
/*!
Constructor
*/
function mailInscriptionType()
{
$this->eZWorkflowEventType( EZ_WORKFLOW_TYPE_MAIL_INSCRIPTION,
"Hello User" );
}
function execute( &$process, &$event )
{

 $parameters = $process->attribute( 'parameter_list' );
        $objectID =& $parameters['object_id'];
		
      
	  $obj =& eZContentObject::fetch($parameters['object_id']);
		$classID=& $obj->attribute( 'contentclass_id' );

// if a new receiver
if($classID == '36'){
	$data_map=& $obj->attribute( 'data_map' );
	$email = $data_map['email']->content();

	include_once( 'lib/ezutils/classes/ezmail.php' );

	$mail = new eZMail();
 			
            include_once( 'lib/ezutils/classes/ezmailtransport.php' );
			include_once( "lib/ezutils/classes/ezhttptool.php" );
			include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
			include_once( "kernel/classes/ezcontentclassattribute.php" );
			include_once( "kernel/classes/ezcontentclass.php" );
			include_once( "kernel/classes/ezcontentobject.php" );
	
	 $emailSender ='info@bazaravenue.com';
  	 $subject = 'inscription à la newsletter';
	 $receiver = $email;
  	 $body='Nous avons le plaisir de vous annoncer votre inscription à notre newsletter';
            
            $mail->setSender( $emailSender );		
            $mail->setReceiver( $receiver );
            $mail->setSubject( $subject );
            $mail->setBody( $body );
            $mailResult = eZMailTransport::send( $mail );


$process->RedirectUrl = "/";
return EZ_WORKFLOW_TYPE_STATUS_REDIRECT ;
}


//if newsletter edit
elseif($classID == '40'){

	$listeAbonnes = eZContentObject::fetchList( true, 
  					array( 'contentclass_id' => 36,
         'status'          =>  EZ_CONTENT_OBJECT_STATUS_PUBLISHED) 
);
	
	
	

foreach($listeAbonnes as $Abonnes){

	$data_map=& $Abonnes->attribute( 'data_map' );
	$email = $data_map['email']->content();
			
			include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
			include_once( 'lib/ezutils/classes/ezmail.php' );
			 include_once( "kernel/common/template.php" );
             include_once( 'lib/ezutils/classes/ezmailtransport.php' );
			
			
			$mail = new eZMail();

 			     
					
////////////////////////////////////////////////////////////////////////
			$nodeID='217';// newsletter object node // object id=295//data_map=titre+corps
			
	$node =& eZContentObjectTreeNode::fetch( $nodeID );
			
	$mail_tpl =& templateInit();
	$mail_tpl->setVariable( 'node_id', $nodeID );
	$mail_tpl->setVariable( 'node', $node );
	$mailTemplateResult =& $mail_tpl->fetch( 'design:newsletter/newsletter.tpl' );


	////////////////////////////////////////////////////////////////////	
		
		 $emailSender ='info@bazaravenue.com';
  		 $receiver = $email;
		
			$mail->setBody( $mailTemplateResult);

            $mail->setSender( $emailSender );		
            $mail->setReceiver( $receiver );
            $subject = 'Nouvelle Newsletter';
           
            $mail->setSubject( $subject );
           
            $mailResult = eZMailTransport::send( $mail );
			
//debug - uncomment next 4 lines if you want to see what should be in the body
//echo "This is your Mailbody:<plaintext>";
//print_r( $mailTemplateResult );
//echo "</plaintext>";
//exit();

}
			
			
//return EZ_WORKFLOW_TYPE_STATUS_ACCEPTED;

$process->RedirectUrl = "/";
return EZ_WORKFLOW_TYPE_STATUS_REDIRECT ;
}
else{
return EZ_WORKFLOW_TYPE_STATUS_ACCEPTED;
}

}
}
eZWorkflowEventType::registerType( EZ_WORKFLOW_TYPE_MAIL_INSCRIPTION,
"mailinscriptiontype" );


and newsletter.tpl:

Already fetched in php:
<h1>{$node.object.data_map.titre.content|wash()}</h1>

{default tpl_node=fetch( 'content', 'node', hash( node_id, $node_id ) )}
Fetch via Template:
<h1>{$tpl_node.object.data_map.titre.content|wash()}</h1>
{/default}

Dark Dante

Wednesday 06 July 2005 1:24:03 pm

We can insert and image in the email of shop, whit similar code

<p>{* DO NOT EDIT THIS FILE! Use an override template instead. *} {set-block scope=root
variable=subject}{".subject"|i18n("design/standard/shop")}{/set-block}</p>
<p><a href="http://www.YOURDOMAIN.com"><img src="http://192.0.0.1/web/var/shop/storage/images/setup/FOLDER/IMAGE.jpg" alt="" width="780" height="121" border="0"></a></p>
<p align="center"><font color="#3300FF" size="4" face="Century Gothic"><strong>{"HEADER OF MAIL"|i18n("design/standard/shop")</strong></font><strong><font color="#3300FF" size="4">}</font></strong><br></p>
</p>

THIS CODE WAS PUT IN /var/www/html/yourweb/design/shop/override/templates/shop/ordermail.tpl, and both receiver and sender can view the image in any format.

this example is for the shop order mail.

eZ debug

Timing: Jan 18 2025 21:05:30
Script start
Timing: Jan 18 2025 21:05:30
Module start 'content'
Timing: Jan 18 2025 21:05:31
Module end 'content'
Timing: Jan 18 2025 21:05:31
Script end

Main resources:

Total runtime1.1278 sec
Peak memory usage4,096.0000 KB
Database Queries217

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0058 587.7188180.8281
Module start 'content' 0.00581.0018 768.5469730.7891
Module end 'content' 1.00760.1201 1,499.3359356.4297
Script end 1.1277  1,855.7656 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00390.3491210.0002
Check MTime0.00150.1300210.0001
Mysql Total
Database connection0.00080.068810.0008
Mysqli_queries1.014789.96502170.0047
Looping result0.00240.21372150.0000
Template Total1.089096.620.5445
Template load0.00280.244920.0014
Template processing1.086296.307720.5431
Template load and register function0.00010.009610.0001
states
state_id_array0.00120.103810.0012
state_identifier_array0.00180.162220.0009
Override
Cache load0.00260.23071030.0000
Sytem overhead
Fetch class attribute can translate value0.00190.166940.0005
Fetch class attribute name0.00100.0889120.0001
XML
Image XML parsing0.00140.120640.0003
class_abstraction
Instantiating content class attribute0.00010.0044190.0000
General
dbfile0.00740.6581330.0002
String conversion0.00000.000430.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
9content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
10content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
17content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
29content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
12content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 79
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs