background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Sending a CCK node in an HTML email when its created using PEAR libraries

Eric.London's picture

Although I am a big fan of the Drupal Forms API, sometimes I'll use CCK node forms as a quick and flexible alternative. This allows you to store the user submitted data as well as process the form. Here is a code snippet you can add to send the html generated from the node in an HTML email...

<?php
function MYMODULE_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if (
$node->type == 'MYNODETYPE' && $op=='insert') {
   
$emailTo = variable_get('site_mail', 'MYEMAILADDRESS');
   
$emailFrom = 'info@' . $_SERVER['HTTP_HOST'];
       
   
// send email
   
_MYMODULE_send_node_email($node, $emailTo, $emailFrom);
       
  }
   
}

function
_MYMODULE_send_node_email(&$node, $emailTo, $emailFrom) {

  require_once(
'Mail.php');
  require_once(
'Mail/mime.php');

 
// create node html
 
$html = node_view($node, TRUE, FALSE, FALSE);
   
 
// get a list of node types
 
$nodeTypes = node_get_types();
   
 
$crlf = "\n";
 
$hdrs = array(
   
'From'    => $emailFrom,
   
'Subject' => $nodeTypes[$node->type]->name . ' Submitted',
  );
   
 
$mime = new Mail_mime($crlf);
 
//$mime->setTXTBody($text);  //TODO: you should also include a text version of your email
 
$mime->setHTMLBody($html);
 
$body = $mime->get();
 
$hdrs = $mime->headers($hdrs);
 
$mail =& Mail::factory('mail');
 
$mail->send($emailTo, $hdrs, $body);  

}
?>