ILIAS  release_4-4 Revision
ilMimeMail Class Reference

this class encapsulates the PHP mail() function. More...

+ Collaboration diagram for ilMimeMail:

Public Member Functions

 __construct ()
 Mail contructor. More...
 
 autoCheck ($bool)
 activate or desactivate the email addresses validator ex: autoCheck( true ) turn the validator on by default autoCheck feature is on More...
 
 Subject ($subject, $a_add_prefix=false)
 Define the subject line of the email. More...
 
 From ($from)
 set the sender of the mail More...
 
 ReplyTo ( $address)
 set the Reply-to header More...
 
 Receipt ()
 add a receipt to the mail ie. More...
 
 To ( $to)
 set the mail recipient More...
 
 Cc ($cc)
 Cc() cc : email address(es), accept both array and string. More...
 
 Bcc ( $bcc)
 Bcc() set the Bcc headers ( blank carbon copy ). More...
 
 Body ( $body, $charset="")
 Body( text [, charset] ) set the body (message) of the mail define the charset if the message contains extended characters (accents) default to us-ascii $mail->Body( "m�l en fran�ais avec des accents", "iso-8859-1" );. More...
 
 Organization ( $org)
 Organization( $org ) set the Organization header. More...
 
 Priority ( $priority)
 Priority( $priority ) set the mail priority $priority : integer taken between 1 (highest) and 5 ( lowest ) ex: $mail->Priority(1) ; => Highest. More...
 
 Attach ( $filename, $filetype="", $disposition="inline", $display_name=null)
 Attach a file to the mail. More...
 
 BuildMail ()
 Build the email message public. More...
 
 Send ()
 fornat and send the mail public More...
 
 Get ()
 return the whole e-mail , headers + message can be used for displaying the message in plain text or logging it More...
 
 ValidEmail ($address)
 check an email address validity public More...
 
 CheckAdresses ( $aad)
 check validity of email addresses return if unvalid, output an error message and exit, this may -should- be customized More...
 
 _build_attachement ()
 check and encode attach file(s) . More...
 
 _mimeEncode ($a_string)
 

Data Fields

 $sendto = array()
 
 $acc = array()
 
 $abcc = array()
 
 $aattach = array()
 
 $xheaders = array()
 
 $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' )
 
 $charset = "utf-8"
 
 $ctencoding = "8bit"
 
 $receipt = 0
 

Detailed Description

this class encapsulates the PHP mail() function.

implements CC, Bcc, Priority headers include "libmail.php"; $m= new Mail; // create the mail $m->From( "leo@isp.com" ); $m->To( "destination@somewhere.fr" ); $m->Subject( "the subject of the mail" ); $message= "Hello world!\nthis is a test of the Mail class\nplease ignore\nThanks."; $m->Body( $message); // set the body $m->Cc( "someone@somewhere.fr"); $m->Bcc( "someoneelse@somewhere.fr"); $m->Priority(4) ; // set the priority to Low m->Attach( "/home/leo/toto.gif", "image/gif" ) ; // attach a file of type image/gif $m->Send(); // send the mail echo "the mail below has been sent:<br><pre>", $m->Get(), "</pre>";

Author
Leo West - lwest.nosp@m.@fre.nosp@m.e.fr
Version
$Id$

Definition at line 26 of file class.ilMimeMail.php.

Constructor & Destructor Documentation

◆ __construct()

ilMimeMail::__construct ( )

Mail contructor.

Definition at line 74 of file class.ilMimeMail.php.

References autoCheck().

75  {
76  $this->autoCheck( false);
77  $this->boundary= "--" . md5( uniqid("myboundary") );
78  }
autoCheck($bool)
activate or desactivate the email addresses validator ex: autoCheck( true ) turn the validator on by ...
+ Here is the call graph for this function:

Member Function Documentation

◆ _build_attachement()

ilMimeMail::_build_attachement ( )

check and encode attach file(s) .

internal use only private

Definition at line 425 of file class.ilMimeMail.php.

References $filename, and exit.

Referenced by BuildMail().

426  {
427 
428  $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
429 
430  $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
431  $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n".
432  $this->body ."\n";
433 
434  $sep= chr(13) . chr(10);
435 
436  $ata= array();
437  $k=0;
438 
439  // for each attached file, do...
440  for( $i=0; $i < count( $this->aattach); $i++ ) {
441 
442  $filename = $this->aattach[$i];
443  $basename = basename($filename);
444  $ctype = $this->actype[$i]; // content-type
445  $disposition = $this->adispo[$i];
446  $display_name = $this->adisplay[$i];
447  if(!$display_name)
448  {
449  $display_name = $basename;
450  }
451 
452  if( ! file_exists( $filename) ) {
453  echo "Class Mail, method attach : file $filename can't be found"; exit;
454  }
455  $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding:".
456  "base64\nContent-Disposition: $disposition;\n filename=\"$display_name\"\n\n";
457  $ata[$k++] = $subhdr;
458  // non encoded line length
459  $linesz= filesize( $filename)+1;
460  $fp= fopen( $filename, 'r' );
461  $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
462  fclose($fp);
463  }
464  $this->fullBody .= implode($sep, $ata);
465  }
exit
Definition: login.php:54
$filename
Definition: buildRTE.php:89
+ Here is the caller graph for this function:

◆ _mimeEncode()

ilMimeMail::_mimeEncode (   $a_string)

Definition at line 467 of file class.ilMimeMail.php.

Referenced by ilMail\addFullname(), ilMail\getIliasMailerAddress(), and Subject().

468  {
469  $encoded = '=?utf-8?b?';
470  $encoded .= base64_encode($a_string);
471  $encoded .= '?=';
472 
473  return $encoded;
474  }
+ Here is the caller graph for this function:

◆ Attach()

ilMimeMail::Attach (   $filename,
  $filetype = "",
  $disposition = "inline",
  $display_name = null 
)

Attach a file to the mail.

Parameters
stringfilename : path of the file to attach
stringfiletype : MIME-type of the file. default to 'application/x-unknown-content-type'
stringdisposition : instruct the Mailclient to display the file if possible ("inline") or always as a link ("attachment") possible values are "inline", "attachment"
string$display_name,filename to use in email (if different from source file)

Definition at line 285 of file class.ilMimeMail.php.

References $filename.

286  {
287  // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
288  if( $filetype == "" )
289  $filetype = "application/octet-stream";
290 
291  $this->aattach[] = $filename;
292  $this->actype[] = $filetype;
293  $this->adispo[] = $disposition;
294  $this->adisplay[] = $display_name;
295  }
$filename
Definition: buildRTE.php:89

◆ autoCheck()

ilMimeMail::autoCheck (   $bool)

activate or desactivate the email addresses validator ex: autoCheck( true ) turn the validator on by default autoCheck feature is on

Parameters
booleanset to true to turn on the auto validation public

Definition at line 88 of file class.ilMimeMail.php.

Referenced by __construct().

89  {
90  if( $bool )
91  $this->checkAddress = true;
92  else
93  $this->checkAddress = false;
94  }
+ Here is the caller graph for this function:

◆ Bcc()

ilMimeMail::Bcc (   $bcc)

Bcc() set the Bcc headers ( blank carbon copy ).

bcc : email address(es), accept both array and string

Parameters
stringbcc

Definition at line 209 of file class.ilMimeMail.php.

References CheckAdresses().

210  {
211  if( is_array($bcc) ) {
212  $this->abcc = $bcc;
213  } else {
214  $this->abcc[]= $bcc;
215  }
216 
217  if( $this->checkAddress == true )
218  $this->CheckAdresses( $this->abcc );
219  }
CheckAdresses( $aad)
check validity of email addresses return if unvalid, output an error message and exit, this may -should- be customized
+ Here is the call graph for this function:

◆ Body()

ilMimeMail::Body (   $body,
  $charset = "" 
)

Body( text [, charset] ) set the body (message) of the mail define the charset if the message contains extended characters (accents) default to us-ascii $mail->Body( "m�l en fran�ais avec des accents", "iso-8859-1" );.

Parameters
stringbody
stringcharset (optional)

Definition at line 231 of file class.ilMimeMail.php.

232  {
233  $this->body = $body;
234 
235  if( $charset != "" ) {
236  $this->charset = strtolower($charset);
237  if( $this->charset == "us-ascii" )
238  $this->ctencoding = "7bit";
239  }
240  }

◆ BuildMail()

ilMimeMail::BuildMail ( )

Build the email message public.

Definition at line 301 of file class.ilMimeMail.php.

References $ctencoding, and _build_attachement().

Referenced by Get(), and Send().

302  {
303 
304  // build the headers
305  $this->headers = "";
306  // $this->xheaders['To'] = implode( ", ", $this->sendto );
307 
308  if( count($this->acc) > 0 )
309  $this->xheaders['CC'] = implode( ", ", $this->acc );
310 
311  if( count($this->abcc) > 0 )
312  $this->xheaders['BCC'] = implode( ", ", $this->abcc );
313 
314 
315  if( $this->receipt ) {
316  if( isset($this->xheaders["Reply-To"] ) )
317  $this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
318  else
319  $this->xheaders["Disposition-Notification-To"] = $this->xheaders['From'];
320  }
321 
322  if( $this->charset != "" ) {
323  $this->xheaders["Mime-Version"] = "1.0";
324  $this->xheaders["Content-Type"] = "text/plain; charset=$this->charset";
325  $this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
326  }
327 
328  $this->xheaders["X-Mailer"] = "Php/libMailv1.3";
329 
330  // include attached files
331  if( count( $this->aattach ) > 0 ) {
332  $this->_build_attachement();
333  } else {
334  $this->fullBody = $this->body;
335  }
336 
337  reset($this->xheaders);
338  while( list( $hdr,$value ) = each( $this->xheaders ) ) {
339  if( $hdr != "Subject" )
340  $this->headers .= "$hdr: $value\n";
341  }
342 
343 
344  }
_build_attachement()
check and encode attach file(s) .
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Cc()

ilMimeMail::Cc (   $cc)

Cc() cc : email address(es), accept both array and string.

Parameters
stringcc set the CC headers ( carbon copy )

Definition at line 188 of file class.ilMimeMail.php.

References CheckAdresses().

189  {
190  if( is_array($cc) )
191  $this->acc= $cc;
192  else
193  $this->acc[]= $cc;
194 
195  if( $this->checkAddress == true )
196  $this->CheckAdresses( $this->acc );
197 
198  }
CheckAdresses( $aad)
check validity of email addresses return if unvalid, output an error message and exit, this may -should- be customized
+ Here is the call graph for this function:

◆ CheckAdresses()

ilMimeMail::CheckAdresses (   $aad)

check validity of email addresses return if unvalid, output an error message and exit, this may -should- be customized

Parameters
arrayaad -

Definition at line 409 of file class.ilMimeMail.php.

References exit, and ValidEmail().

Referenced by Bcc(), Cc(), and To().

410  {
411  for($i=0;$i< count( $aad); $i++ ) {
412  if( ! $this->ValidEmail( $aad[$i]) ) {
413  echo "Class Mail, method Mail : invalid address $aad[$i]";
414  exit;
415  }
416  }
417  }
exit
Definition: login.php:54
ValidEmail($address)
check an email address validity public
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ From()

ilMimeMail::From (   $from)

set the sender of the mail

Parameters
stringfrom should be an email address

Definition at line 122 of file class.ilMimeMail.php.

References exit.

Referenced by ilPurchaseBaseGUI\__sendBill().

123  {
124 
125  if( ! is_string($from) ) {
126  echo "Class Mail: error, From is not a string";
127  exit;
128  }
129 
130  // base64_encode fullname but not email
131 
132  $this->xheaders['From'] = $from;
133  }
exit
Definition: login.php:54
+ Here is the caller graph for this function:

◆ Get()

ilMimeMail::Get ( )

return the whole e-mail , headers + message can be used for displaying the message in plain text or logging it

Definition at line 374 of file class.ilMimeMail.php.

References BuildMail().

375  {
376  $this->BuildMail();
377  $mail = "To: " . $this->strTo . "\n";
378  $mail .= $this->headers . "\n";
379  $mail .= $this->fullBody;
380  return $mail;
381  }
BuildMail()
Build the email message public.
+ Here is the call graph for this function:

◆ Organization()

ilMimeMail::Organization (   $org)

Organization( $org ) set the Organization header.

Parameters
stringorganization

Definition at line 248 of file class.ilMimeMail.php.

249  {
250  if( trim( $org != "" ) )
251  $this->xheaders['Organization'] = $org;
252  }

◆ Priority()

ilMimeMail::Priority (   $priority)

Priority( $priority ) set the mail priority $priority : integer taken between 1 (highest) and 5 ( lowest ) ex: $mail->Priority(1) ; => Highest.

Parameters
integerpriority

Definition at line 262 of file class.ilMimeMail.php.

263  {
264  if( ! intval( $priority ) )
265  return false;
266 
267  if( ! isset( $this->priorities[$priority-1]) )
268  return false;
269 
270  $this->xheaders["X-Priority"] = $this->priorities[$priority-1];
271 
272  return true;
273 
274  }

◆ Receipt()

ilMimeMail::Receipt ( )

add a receipt to the mail ie.

a confirmation is returned to the "From" address (or "ReplyTo" if defined) when the receiver opens the message.

Warning
this functionality is not a standard, thus only some mail clients are compliants.

Definition at line 156 of file class.ilMimeMail.php.

157  {
158  $this->receipt = 1;
159  }

◆ ReplyTo()

ilMimeMail::ReplyTo (   $address)

set the Reply-to header

Parameters
stringaddress should be an email address

Definition at line 139 of file class.ilMimeMail.php.

140  {
141 
142  if( ! is_string($address) )
143  return false;
144 
145  $this->xheaders["Reply-To"] = $address;
146 
147  }

◆ Send()

ilMimeMail::Send ( )

fornat and send the mail public

Definition at line 350 of file class.ilMimeMail.php.

References $ilSetting, $res, and BuildMail().

351  {
352  #global $ilLog;
353  global $ilSetting;
354 
355  $this->BuildMail();
356 
357  $this->strTo = implode( ", ", $this->sendto );
358 
359  // envoie du mail
360  if(!(int)$ilSetting->get('prevent_smtp_globally'))
361  {
362  $res = @mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
363  }
364  #$ilLog->write($this->strTo.' '. $this->xheaders['Subject'].' '. $this->fullBody.' '. $this->headers);
365  }
BuildMail()
Build the email message public.
global $ilSetting
Definition: privfeed.php:40
+ Here is the call graph for this function:

◆ Subject()

ilMimeMail::Subject (   $subject,
  $a_add_prefix = false 
)

Define the subject line of the email.

Parameters
stringsubject any monoline string

Definition at line 101 of file class.ilMimeMail.php.

References _mimeEncode(), and ilMail\getSubjectPrefix().

102  {
103  if($a_add_prefix)
104  {
105  // #9096
106  include_once "Services/Mail/classes/class.ilMail.php";
107  $prefix = ilMail::getSubjectPrefix();
108  if(trim($prefix))
109  {
110  $subject = trim($prefix)." ".$subject;
111  }
112  }
113  $this->xheaders['Subject'] = ilMimeMail::_mimeEncode(strtr($subject,"\r\n"," "));
114  }
_mimeEncode($a_string)
static getSubjectPrefix()
Get text that will be prepended to auto generated mails.
+ Here is the call graph for this function:

◆ To()

ilMimeMail::To (   $to)

set the mail recipient

Parameters
stringto email address, accept both a single address or an array of addresses

Definition at line 167 of file class.ilMimeMail.php.

References CheckAdresses().

168  {
169 
170  // TODO : test validit� sur to
171  if( is_array( $to ) )
172  $this->sendto= $to;
173  else
174  $this->sendto[] = $to;
175 
176  if( $this->checkAddress == true )
177  $this->CheckAdresses( $this->sendto );
178 
179  }
CheckAdresses( $aad)
check validity of email addresses return if unvalid, output an error message and exit, this may -should- be customized
+ Here is the call graph for this function:

◆ ValidEmail()

ilMimeMail::ValidEmail (   $address)

check an email address validity public

Parameters
stringaddress : email address to check
Returns
boolean true if email adress is ok

Definition at line 391 of file class.ilMimeMail.php.

Referenced by CheckAdresses().

392  {
393  if( ereg( ".*<(.+)>", $address, $regs ) ) {
394  $address = $regs[1];
395  }
396  if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
397  return true;
398  else
399  return false;
400  }
+ Here is the caller graph for this function:

Field Documentation

◆ $aattach

ilMimeMail::$aattach = array()

Definition at line 48 of file class.ilMimeMail.php.

◆ $abcc

ilMimeMail::$abcc = array()

Definition at line 42 of file class.ilMimeMail.php.

◆ $acc

ilMimeMail::$acc = array()

Definition at line 37 of file class.ilMimeMail.php.

◆ $charset

ilMimeMail::$charset = "utf-8"

Definition at line 66 of file class.ilMimeMail.php.

◆ $ctencoding

ilMimeMail::$ctencoding = "8bit"

Definition at line 67 of file class.ilMimeMail.php.

Referenced by BuildMail().

◆ $priorities

ilMimeMail::$priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' )

Definition at line 60 of file class.ilMimeMail.php.

◆ $receipt

ilMimeMail::$receipt = 0

Definition at line 68 of file class.ilMimeMail.php.

◆ $sendto

ilMimeMail::$sendto = array()

Definition at line 32 of file class.ilMimeMail.php.

◆ $xheaders

ilMimeMail::$xheaders = array()

Definition at line 54 of file class.ilMimeMail.php.


The documentation for this class was generated from the following file: