[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/Emails/ -> mail.php (source)

   1  <?php
   2  /*********************************************************************************
   3  ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   *
  10   ********************************************************************************/
  11  
  12  
  13  require ("modules/Emails/class.phpmailer.php");
  14  
  15  /**   Function used to send email 
  16    *   $module         -- current module 
  17    *   $to_email     -- to email address 
  18    *   $from_name    -- currently loggedin user name
  19    *   $from_email    -- currently loggedin vtiger_users's email id. you can give as '' if you are not in HelpDesk module
  20    *   $subject        -- subject of the email you want to send
  21    *   $contents        -- body of the email you want to send
  22    *   $cc        -- add email ids with comma seperated. - optional 
  23    *   $bcc        -- add email ids with comma seperated. - optional.
  24    *   $attachment    -- whether we want to attach the currently selected file or all vtiger_files.[values = current,all] - optional
  25    *   $emailid        -- id of the email object which will be used to get the vtiger_attachments
  26    */
  27  function send_mail($module,$to_email,$from_name,$from_email,$subject,$contents,$cc='',$bcc='',$attachment='',$emailid='')
  28  {
  29  
  30      global $adb, $log;
  31      global $root_directory;
  32      global $HELPDESK_SUPPORT_EMAIL_ID, $HELPDESK_SUPPORT_NAME;
  33  
  34      $uploaddir = $root_directory ."/test/upload/";
  35  
  36      $adb->println("To id => '".$to_email."'\nSubject ==>'".$subject."'\nContents ==> '".$contents."'");
  37  
  38      //Get the email id of assigned_to user -- pass the value and name, name must be "user_name" or "id"(field names of vtiger_users vtiger_table)
  39      //$to_email = getUserEmailId('id',$assigned_user_id);
  40  
  41      //if module is HelpDesk then from_email will come based on support email id 
  42      if($from_email == '')//$module != 'HelpDesk')
  43          $from_email = getUserEmailId('user_name',$from_name);
  44  
  45      $contents = addSignature($contents,$from_name);
  46  
  47      $mail = new PHPMailer();
  48  
  49      setMailerProperties(&$mail,$subject,$contents,$from_email,$from_name,$to_email,$attachment,$emailid);
  50      setCCAddress(&$mail,'cc',$cc);
  51      setCCAddress(&$mail,'bcc',$bcc);
  52  
  53      $mail_status = MailSend(&$mail);
  54  
  55      if($mail_status != 1)
  56      {
  57          $mail_error = getMailError(&$mail,$mail_status,$mailto);
  58      }
  59      else
  60      {
  61          $mail_error = $mail_status;
  62      }
  63  
  64      return $mail_error;
  65  }
  66  
  67  /**    Function to get the user Email id based on column name and column value
  68    *    $name -- column name of the vtiger_users vtiger_table 
  69    *    $val  -- column value 
  70    */
  71  function getUserEmailId($name,$val)
  72  {
  73      global $adb;
  74      $adb->println("Inside the function getUserEmailId. --- ".$name." = '".$val."'");
  75      if($val != '')
  76      {
  77          //$sql = "select email1, email2, yahoo_id from vtiger_users where ".$name." = '".$val."'";
  78          //done to resolve the PHP5 specific behaviour
  79          $sql = "select email1, email2, yahoo_id from vtiger_users where ".$name." = ".$adb->quote($val);
  80          $res = $adb->query($sql);
  81          $email = $adb->query_result($res,0,'email1');
  82          if($email == '')
  83          {
  84              $email = $adb->query_result($res,0,'email2');
  85              if($email == '')
  86              {
  87                  $email = $adb->query_result($res,0,'yahoo_id');
  88              }
  89          }
  90          $adb->println("Email id is selected  => '".$email."'");
  91          return $email;
  92      }
  93      else
  94      {
  95          $adb->println("User id is empty. so return value is ''");
  96          return '';
  97      }
  98  }
  99  
 100  /**    Funtion to add the user's signature with the content passed
 101    *    $contents -- where we want to add the signature
 102    *    $fromname -- which user's signature will be added to the contents
 103    */
 104  function addSignature($contents, $fromname)
 105  {
 106      global $adb;
 107      $adb->println("Inside the function addSignature");
 108  
 109      $sign = $adb->query_result($adb->query("select signature from vtiger_users where user_name=".$adb->quote($fromname)),0,"signature");
 110      if($sign != '')
 111      {
 112          $contents .= '<br><br><font color=darkgrey>'.$sign.'</font>';
 113          $adb->println("Signature is added with the body => '.".$sign."'");
 114      }
 115      else
 116      {
 117          $adb->println("Signature is empty for the user => '".$fromname."'");
 118      }
 119      return $contents;
 120  }
 121  
 122  /**    Function to set all the Mailer properties
 123    *    $mail         -- reference of the mail object
 124    *    $subject    -- subject of the email you want to send
 125    *    $contents    -- body of the email you want to send
 126    *    $from_email    -- from email id which will be displayed in the mail
 127    *    $from_name    -- from name which will be displayed in the mail
 128    *    $to_email     -- to email address  -- This can be an email in a single string, a comma separated
 129    *               list of emails or an array of email addresses
 130    *    $attachment    -- whether we want to attach the currently selected file or all vtiger_files.
 131                    [values = current,all] - optional
 132    *    $emailid    -- id of the email object which will be used to get the vtiger_attachments - optional
 133    */
 134  function setMailerProperties($mail,$subject,$contents,$from_email,$from_name,$to_email,$attachment='',$emailid='')
 135  {
 136      global $adb;
 137      $adb->println("Inside the function setMailerProperties");
 138  
 139      $mail->Subject = $subject;
 140      $mail->Body = $contents;
 141      //$mail->Body = html_entity_decode(nl2br($contents));    //if we get html tags in mail then we will use this line
 142      $mail->AltBody = strip_tags(preg_replace(array("/<p>/i","/<br>/i","/<br \/>/i"),array("\n","\n","\n"),$contents));
 143  
 144      $mail->IsSMTP();        //set mailer to use SMTP
 145      //$mail->Host = "smtp1.example.com;smtp2.example.com";  // specify main and backup server
 146  
 147      setMailServerProperties(&$mail);    
 148  
 149      //Handle the from name and email for HelpDesk
 150      $mail->From = $from_email;
 151      $rs = $adb->query("select first_name,last_name from vtiger_users where user_name='".$from_name."'");
 152      if($adb->num_rows($rs) > 0)
 153          $from_name = $adb->query_result($rs,0,"first_name")." ".$adb->query_result($rs,0,"last_name");
 154  
 155      $mail->FromName = $from_name;
 156  
 157      if($to_email != '')
 158      {
 159          if(is_array($to_email)) {
 160              for($j=0,$num=count($to_email);$j<$num;$j++) {
 161                  $mail->addAddress($to_email[$j]);
 162              }
 163          } else {
 164              $_tmp = explode(",",$to_email);
 165              for($j=0,$num=count($_tmp);$j<$num;$j++) {
 166                  $mail->addAddress($_tmp[$j]);
 167              }
 168          }
 169      }
 170  
 171      $mail->AddReplyTo($from_email);
 172      $mail->WordWrap = 50;
 173  
 174      //If we want to add the currently selected file only then we will use the following function
 175      if($attachment == 'current')
 176      {
 177          addAttachment(&$mail,$_FILES['filename']['name'],$emailid);
 178      }
 179  
 180      //This will add all the vtiger_files which are related to this record or email
 181      if($attachment == 'all')
 182      {
 183          addAllAttachments(&$mail,$emailid);
 184      }
 185  
 186      $mail->IsHTML(true);        // set email format to HTML
 187  
 188      return;
 189  }
 190  
 191  /**    Function to set the Mail Server Properties in the object passed
 192    *    $mail -- reference of the mailobject
 193    */
 194  function setMailServerProperties($mail)
 195  {
 196      global $adb;
 197      $adb->println("Inside the function setMailServerProperties");
 198  
 199      $res = $adb->query("select * from vtiger_systems where server_type='email'");
 200      $server = $adb->query_result($res,0,'server');
 201          $username = $adb->query_result($res,0,'server_username');
 202          $password = $adb->query_result($res,0,'server_password');
 203      $smtp_auth = $adb->query_result($res,0,'smtp_auth');
 204  
 205      $adb->println("Mail server name,username & password => '".$server."','".$username."','".$password."'");
 206  
 207      $mail->SMTPAuth = $smtp_auth;    // turn on SMTP authentication
 208      $mail->Host = $server;        // specify main and backup server
 209      $mail->Username = $username ;    // SMTP username
 210          $mail->Password = $password ;    // SMTP password
 211  
 212      return;
 213  }
 214  
 215  /**    Function to add the file as attachment with the mail object
 216    *    $mail -- reference of the mail object
 217    *    $filename -- filename which is going to added with the mail
 218    *    $record -- id of the record - optional 
 219    */
 220  function addAttachment($mail,$filename,$record)
 221  {
 222      global $adb, $root_directory;
 223      $adb->println("Inside the function addAttachment");
 224      $adb->println("The file name is => '".$filename."'");
 225  
 226      //This is the file which has been selected in Email EditView
 227          if(is_file($filename) && $filename != '')
 228          {
 229                  $mail->AddAttachment($root_directory."test/upload/".$filename);
 230          }
 231  }
 232  
 233  /**     Function to add all the vtiger_files as attachment with the mail object
 234    *     $mail -- reference of the mail object
 235    *     $record -- email id ie., record id which is used to get the all vtiger_attachments from database
 236    */
 237  function addAllAttachments($mail,$record)
 238  {
 239      global $adb, $root_directory;
 240          $adb->println("Inside the function addAllAttachments");
 241  
 242      //Retrieve the vtiger_files from database where avoid the file which has been currently selected
 243      $sql = "select vtiger_attachments.* from vtiger_attachments inner join vtiger_seattachmentsrel on vtiger_attachments.attachmentsid = vtiger_seattachmentsrel.attachmentsid inner join vtiger_crmentity on vtiger_crmentity.crmid = vtiger_attachments.attachmentsid where vtiger_crmentity.deleted=0 and vtiger_seattachmentsrel.crmid=".$record;
 244      $res = $adb->query($sql);
 245      $count = $adb->num_rows($res);
 246  
 247      for($i=0;$i<$count;$i++)
 248      {
 249          $fileid = $adb->query_result($res,$i,'attachmentsid');
 250          $filename = $adb->query_result($res,$i,'name');
 251          $filepath = $adb->query_result($res,$i,'path');
 252          $filewithpath = $root_directory.$filepath.$fileid."_".$filename;
 253  
 254          //if the file is exist in test/upload directory then we will add directly
 255          //else get the contents of the file and write it as a file and then attach (this will occur when we unlink the file)
 256          if(is_file($filewithpath))
 257          {
 258              $mail->AddAttachment($filewithpath,$filename);
 259          }
 260      }
 261  }
 262  
 263  /**    Function to set the CC or BCC addresses in the mail
 264    *    $mail -- reference of the mail object
 265    *    $cc_mod -- mode to set the address ie., cc or bcc
 266    *    $cc_val -- addresss with comma seperated to set as CC or BCC in the mail
 267    */
 268  function setCCAddress($mail,$cc_mod,$cc_val)
 269  {
 270      global $adb;
 271      $adb->println("Inside the functin setCCAddress");
 272  
 273      if($cc_mod == 'cc')
 274          $method = 'AddCC';
 275      if($cc_mod == 'bcc')
 276          $method = 'AddBCC';
 277      if($cc_val != '')
 278      {
 279          $ccmail = explode(",",$cc_val);
 280          for($i=0;$i<count($ccmail);$i++)
 281          {
 282              if($ccmail[$i] != '')
 283                  $mail->$method($ccmail[$i]);
 284          }
 285      }
 286  }
 287  
 288  /**    Function to send the mail which will be called after set all the mail object values
 289    *    $mail -- reference of the mail object
 290    */
 291  function MailSend($mail)
 292  {
 293      global $log;
 294           $log->info("Inside of Send Mail function.");
 295      if(!$mail->Send())
 296          {
 297          $log->debug("Error in Mail Sending : Error log = '".$mail->ErrorInfo."'");
 298          return $mail->ErrorInfo;
 299          }
 300      else 
 301      {
 302           $log->info("Mail has been sent from the vtigerCRM system : Status : '".$mail->ErrorInfo."'");
 303          return 1;
 304      }
 305  }
 306  
 307  /**    Function to get the Parent email id from HelpDesk to send the details about the ticket via email
 308    *    $returnmodule -- Parent module value. Contact or Account for send email about the ticket details
 309    *    $parentid -- id of the parent ie., contact or vtiger_account
 310    */
 311  function getParentMailId($parentmodule,$parentid)
 312  {
 313      global $adb;
 314      $adb->println("Inside the function getParentMailId. \n parent module and id => ".$parentmodule."&".$parentid);
 315  
 316          if($parentmodule == 'Contacts')
 317          {
 318                  $tablename = 'vtiger_contactdetails';
 319                  $idname = 'contactid';
 320          $first_email = 'email';
 321          $second_email = 'yahooid';
 322          }
 323          if($parentmodule == 'Accounts')
 324          {
 325                  $tablename = 'vtiger_account';
 326                  $idname = 'accountid';
 327          $first_email = 'email1';
 328          $second_email = 'email2';
 329          }
 330      if($parentid != '')
 331      {
 332              //$query = 'select * from '.$tablename.' where '.$idname.' = '.$parentid;
 333              $query = 'select * from '.$tablename.' where '. $idname.' = '.$adb->quote($parentid);
 334              $mailid = $adb->query_result($adb->query($query),0,$first_email);
 335          $mailid2 = $adb->query_result($adb->query($query),0,$second_email);
 336      }
 337          if($mailid == '' && $mailid2 != '')
 338              $mailid = $mailid2;
 339  
 340      return $mailid;
 341  }
 342  
 343  /**    Function to parse and get the mail error
 344    *    $mail -- reference of the mail object
 345    *    $mail_status -- status of the mail which is sent or not
 346    *    $to -- the email address to whom we sent the mail and failes
 347    *    return -- Mail error occured during the mail sending process
 348    */
 349  function getMailError($mail,$mail_status,$to)
 350  {
 351      //Error types in class.phpmailer.php
 352      /*
 353      provide_address, mailer_not_supported, execute, instantiate, file_access, file_open, encoding, data_not_accepted, authenticate, 
 354      connect_host, recipients_failed, from_failed
 355      */
 356  
 357      global $adb;
 358      $adb->println("Inside the function getMailError");
 359  
 360      $error_info = explode(":",$mail_status);
 361      $msg = trim($error_info[1]);
 362      $adb->println("Error message ==> ".$msg);
 363  
 364      if($msg == 'connect_host')
 365      {
 366          $error_msg =  $msg;
 367      }
 368      elseif(strstr($msg,'from_failed'))
 369      {
 370          $error_msg = $msg;
 371      }
 372      elseif(strstr($msg,'recipients_failed'))
 373      {
 374          $error_msg = $msg;
 375      }
 376      else
 377      {
 378          $adb->println("Mail error is not as connect_host or from_failed or recipients_failed");
 379          //$error_msg = $msg;
 380      }
 381  
 382      $adb->println("return error => ".$error_msg);
 383      return $error_msg;
 384  }
 385  
 386  /**    Function to get the mail status string (string of sent mail status)
 387    *    $mail_status_str -- concatenated string with all the error messages with &&& seperation
 388    *    return - the error status as a encoded string
 389    */
 390  function getMailErrorString($mail_status_str)
 391  {
 392      global $adb;
 393      $adb->println("Inside getMailErrorString function.\nMail status string ==> ".$mail_status_str);
 394  
 395      $mail_status_str = trim($mail_status_str,"&&&");
 396      $mail_status_array = explode("&&&",$mail_status_str);
 397      $adb->println("All Mail status ==>\n".$mail_status_str."\n");
 398  
 399      foreach($mail_status_array as $key => $val)
 400      {
 401          $list = explode("=",$val);
 402          $adb->println("Mail id & status ==> ".$list[0]." = ".$list[1]);
 403          if($list[1] == 0)
 404          {
 405              $mail_error_str .= $list[0]."=".$list[1]."&&&";
 406          }
 407      }
 408      $adb->println("Mail error string => '".$mail_error_str."'");
 409      if($mail_error_str != '')
 410      {
 411          $mail_error_str = 'mail_error='.base64_encode($mail_error_str);
 412      }
 413      return $mail_error_str;
 414  }
 415  
 416  /**    Function to parse the error string
 417    *    $mail_error_str -- base64 encoded string which contains the mail sending errors as concatenated with &&&
 418    *    return - Error message to display
 419    */
 420  function parseEmailErrorString($mail_error_str)
 421  {
 422      //TODO -- we can modify this function for better email error handling in future
 423      global $adb, $mod_strings;
 424      $adb->println("Inside the parseEmailErrorString function.\n encoded mail error string ==> ".$mail_error_str);
 425  
 426      $mail_error = base64_decode($mail_error_str);
 427      $adb->println("Original error string => ".$mail_error);
 428      $mail_status = explode("&&&",trim($mail_error,"&&&"));
 429      foreach($mail_status as $key => $val)
 430      {
 431          $status_str = explode("=",$val);
 432          $adb->println('Mail id => "'.$status_str[0].'".........status => "'.$status_str[1].'"');
 433          if($status_str[1] != 1 && $status_str[1] != '')
 434          {
 435              $adb->println("Error in mail sending");
 436              if($status_str[1] == 'connect_host')
 437              {
 438                  $adb->println("if part - Mail sever is not configured");
 439                  $errorstr .= '<br><b><font color=red>Please Check the Mail Server Name...</font></b>';
 440                  break;
 441              }
 442              elseif($status_str[1] == '0')
 443              {
 444                  $adb->println("first elseif part - status will be 0 which is the case of assigned to vtiger_users's email is empty.");
 445                  $errorstr .= '<br><b><font color=red> Mail could not be sent to the assigned to user. Please check the assigned to user email id...</font></b>';
 446                  //Added to display the message about the CC && BCC mail sending status
 447                  if($status_str[0] == 'cc_success')
 448                  {
 449                                          $cc_msg = 'But the mail has been sent to CC & BCC addresses.';
 450                      $errorstr .= '<br><b><font color=purple>'.$cc_msg.'</font></b>';
 451                  }
 452              }
 453              elseif(strstr($status_str[1],'from_failed'))
 454              {
 455                  $adb->println("second elseif part - from email id is failed.");
 456                  $from = explode('from_failed',$status_str[1]);
 457                  $errorstr .= "<br><b><font color=red>Please check the from email id '".$from[1]."'</font></b>";
 458              }
 459              else
 460              {
 461                  $adb->println("else part - mail send process failed due to the following reason.");
 462                  $errorstr .= "<br><b><font color=red> Mail could not be sent to this email id '".$status_str[0]."'. Please check this mail id...</font></b>";    
 463              }
 464          }
 465      }
 466      $adb->println("Return Error string => ".$errorstr);
 467      return $errorstr;
 468  }
 469  ?>


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7