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( "[email protected]" );
$mail->setReceiver( "[email protected] );
$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"
-"[email protected]"
-"$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 ='[email protected]';
  	 $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 ='[email protected]';
  		 $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.