Saturday, March 26, 2011

Send mail in Drupal 7 & Drupal 6

<?php

function example_notify($accounts) {
$language = language_default();
$params = array();
$from ="admin@testsite.com"
('example', 'notice','kumarit84@gmail.com',$language ,$params,$from);
}

function example_mail($key, &$message, $params) {
$data['user'] = $params['account'];
$options['language'] = $message['language'];
user_mail_tokens($variables, $data, $options);
switch($key) {
case 'notice':
$langcode = $message['language']->language;
$message['subject'] = t('Notification from !site', $variables, array('langcode' => $langcode));
// Make emails HTML friendly
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
$message['body'][] = t("Dear !username\n\nThere is new content available on the site.", $variables, array('langcode' => $langcode));
break;
}
}

?>

Create cck using custom module

1. Create an table with for fields other than title & body.Here i have created an table employee with three fields vid,nid,age.

2.Create an custom module with module folder name employee.

3.Copy the above code & paste in your module file.

4.Go through the code once.

<?php
/**
* Implementation of hook_node_info().
*/
function employee_node_info() {
return array(
'employee' => array(
'name' => t('Employee'),
'module' => 'employee',
'description' => "Create an Employee",
)
);
}

/**
* Implementation of hook_perm().
*/
function employee_perm() {
return array('create employee', 'edit own employee');
}

/**
* Implementation of hook_access().
*/
function employee_access($op, $node) {
global $user;

if ($op == 'create') {
// Only users with permission to do so may create this node type.
return user_access('create employee');
}

// Users who create a node may edit or delete it later, assuming they have the
// necessary permissions.
if ($op == 'update' || $op == 'delete') {
if (user_access('edit own employee') && ($user->uid == $node->uid)) {
return TRUE;
}
}
}

/**
* Implementation of hook_form().
*/
function employee_form(&$node, $form_state) {
$type = node_get_types('type', $node);

// We need to define form elements for the node's title and body.
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);

$form['age'] = array(
'#type' => 'textfield',
'#title' => 'Age',
'#required' => TRUE,
'#default_value' => $node->age,
'#weight' => -4
);
// We want the body and filter elements to be adjacent. We could try doing
// this by setting their weights, but another module might add elements to the
// form with the same weights and end up between ours. By putting them into a
// sub-array together, we're able force them to be rendered together.
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => check_plain($type->body_label),
'#default_value' => $node->body,
'#required' => FALSE
);
$form['body_filter']['filter'] = filter_form($node->format);

// NOTE in node_example there is some addition code here not needed for this simple node-type

return $form;
}

/**
*Load the values of custom fields in CCK
*/
function employee_load($node) {
$additions = db_fetch_object(db_query('SELECT age FROM {employee} WHERE vid = %d', $node->vid));
return $additions;
}

/**
*Insert the values of custom field of CCK into an custom table created
*/
function employee_insert($node) {
db_query("INSERT INTO {employee} (vid, nid, age) VALUES (%d, %d, '%d')", $node->vid, $node->nid, $node->age);
}

/**
*Delete the node from the custom table
*/
function employee_delete($node) {
// Notice that we're matching all revision, by using the node's nid.
db_query('DELETE FROM {employee} WHERE nid = %d', $node->nid);
}


/**
*Update the node into the custom table
*/
function employee_update($node) {
// if this is a new node or we're adding a new revision,
if ($node->revision) {
employee_insert($node);
}
else {
db_query("UPDATE {employee} SET age = %d WHERE vid = %d", $node->age, $node->vid);
}
}


/**
* Implementation of hook_help().
*/
function employee_help($path, $arg) {
switch ($path) {
case 'admin/help/employee':
return '

' . t('Help Custom CCK') . '

';
break;
}
}

?>

Drupal mail with attachment

* You have to set the smtp username ,password,host in the admin settings page

<?php

/*
* This function send mail with attachement
*
*/
function drupal_mail_wrapper_attachment($mailkey, $to, $subject, $body, $from, $header,$attachment="") {
$mail = new phpmailer(); //Create a new phpmailer object.
$username = variable_get('smtp_username', '');
$password = variable_get('smtp_password', '');

//Set the default name emails should be from. If value is not defined in settings use site_name.
if (variable_get('smtp_fromname', '') != ''){
$from_name = variable_get('smtp_fromname', '');
}
else{
$from_name = variable_get('site_name', '');
}

//If from email address is blank use smtp_from config option
if ($from == '' && variable_get('smtp_from', '') != ''){
$from = variable_get('smtp_from', '');
}

//Decide whether to use SMTP Authorization. Do so if username and password are given.
if ($username != '' and $password != '') {
$auth = TRUE;
} else {
$auth = FALSE;
}


//Take care of the email headers.
foreach ($header as $key => $value) {
//watchdog('error', 'Key: ' . $key . ' Value: ' . $value);
if (strtolower($key) == 'from') {
if ($from == NULL or $from == '') {
$from = $value; //If a from value was already given then set based on header.
}
}
else if (strtolower($key) == 'content-type' && strpos(strtolower($value),'text/html') !== FALSE) {
$mail->IsHTML(TRUE);
}
else if (strtolower($key) == 'content-type' && strpos(strtolower($value), 'multipart/mixed') !== FALSE) {
//$body passed to smtp should already be formatted. add multipart header and tell phpmailer to leave it alone
$mail->AddCustomHeader($key . ": " . $value);
$mail->message_type = "pre";
}
else if (strtolower($key) == 'reply-to') {
$mail->AddReplyTo = $value;
}
else if (strtolower($key) == 'return-path') {
if (trim($value) != '') {
$mail->Sender = $value;
}
}
else if (strtolower($key) == 'content-transfer-encoding') {
$mail->Encoding = $value;
}
else if (strtolower($key) == 'mime-version') {
// just ommit MIME-Version it since it will be set by PHP-Mailer
}
else if (strtolower($key) == 'bcc') {
$bccrecipients = split(",", $value);
foreach ($bccrecipients as $bccrecipient) {
if ( strpos($bccrecipient, '<') !== false ) {
$bccparts = explode(" <", $bccrecipient);

$bccname = $bccparts[0];
$bccaddr = rtrim($bccparts[1], ">");
}
else {
$bccname = "";
$bccaddr = $bccrecipient;
}
$mail->AddBCC($bccaddr, $bccname);
}
}
else { //Else the header key is not special, just add it.
$mail->AddCustomHeader($key . ": " . $value); //Add header line.
}
}

//If no from address has been set than use a default.
if ($from == '' or $from == NULL){
$from = variable_get('site_mail', 'test@example.com'); //If no from can be found use site_mail variable.
}

//Set the correct protocol prefix to append to the smtp host.
switch(variable_get('smtp_protocol', 'standard')){
case "ssl":
$mail->Protocol = 'ssl://';
break;
case "tls":
$mail->Protocol = 'tls://';
break;
case "standard":
$mail->Protocol = '';
}

$mail->Host = variable_get('smtp_host', '') . ';' . variable_get('smtp_hostbackup', '');
$mail->Port = variable_get('smtp_port', '25');
$mail->Mailer = "smtp";
$mail->SMTPAuth = $auth;
$mail->Username = $username;
$mail->Password = $password;
$mail->CharSet = 'utf-8';

$mail->From = $from;
$mail->FromName = $from_name;

$torecipients = split(",", $to);
foreach ($torecipients as $torecipient) {
if (strpos($torecipient, '<') !== false) {
$toparts = explode(" <", $torecipient);
$toname = $toparts[0];
$toaddr = rtrim($toparts[1], ">");
}
else {
$toname = "";
$toaddr = $torecipient;
}
$mail->AddAddress($toaddr, $toname);
}

$mail->Subject = $subject;
$mail->Body = $body;
if(count($attachment)) {
foreach ($attachment as $attachments) {
//$target_path_pdf = file_directory_path().'/bill/new/meha.pdf';
$mail->AddAttachment($attachments); // attachment
}
}
watchdog('smtp', "Sending mail to: $to");
//Try to send email, if it fails set watchdog entry.
if(!$mail->Send()) {
watchdog("smtp", "Error sending email: '". $mail->ErrorInfo ."' From: '$from' To: '$to'", WATCHDOG_ERROR);
return false;
}
$mail->SmtpClose();
return true;
}

?>