PageRenderTime 4ms CodeModel.GetById 2ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/mail/cfEMail.class.php

https://bitbucket.org/cyberfox/cyberfox-php-framework
PHP | 610 lines | 235 code | 52 blank | 323 comment | 32 complexity | c0c3eaaa4d4fb0746f91a36e6e79eb84 MD5 | raw file
  1<?php
  2/**
  3 -----------------------------------------------------------------------------
  4* eMAIL BASE SENDER
  5*
  6* Based on PEAR::Mail this class provides you to generate and send an email over sendmail or smtp.
  7* @link http://pear.php.net/manual/en/package.mail.mail.intro.php
  8* @link http://pear.php.net/manual/en/package.mail.mail-mime.php
  9* @link http://www.faqs.org/rfcs/rfc822 RFC822
 10*
 11 -----------------------------------------------------------------------------
 12 -----------------------------------------------------------------------------
 13* @copyright (C) 2011 Cyberfox Software Solutions e.U.
 14* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License version 3 (LGPLv3)
 15* @author Christian Graf <christian.graf@cyberfox.at>
 16 -----------------------------------------------------------------------------
 17 -----------------------------------------------------------------------------
 18* @package redfox
 19* @subpackage messaging
 20* @category communication
 21 -----------------------------------------------------------------------------
 22 -----------------------------------------------------------------------------
 23* @version	$Id: cfEMail.class.php 105 2012-06-28 06:55:45Z cgraf $
 24* @date	$Date: 2012-06-28 08:55:45 +0200 (Do, 28 Jun 2012) $
 25* @svnauthor	$Author: cgraf $
 26 -----------------------------------------------------------------------------
 27*/
 28
 29/**
 30 * Include the PEAR::Mail class to work with.
 31 * @link http://pear.php.net/manual/en/package.mail.mail.php
 32 */
 33require_once('Mail.php'); //PEAR CLASS
 34
 35/**
 36 * Include the PEAR::Mail_mime class to work with.
 37 * @link http://pear.php.net/manual/en/package.mail.mail-mime.php
 38 */
 39require_once('Mail/mime.php'); //PEAR CLASS
 40
 41class cfEMail
 42{
 43	/**
 44	 * CRLF = Carriage Return Line Feed
 45	 *
 46	 * @var char
 47	 * @access public
 48	 */
 49	const CRLF = "\n";
 50
 51	/**
 52	 * The current mime object
 53	 * @link http://pear.php.net/manual/en/package.mail.mail-mime.php
 54	 *
 55	 * @var Mail_mime
 56	 * @access private
 57	 */
 58	private $mimeObj = null;
 59
 60	/**
 61	 * The current mailer object
 62	 * @link http://pear.php.net/manual/en/package.mail.mail.php
 63	 *
 64	 * @var Mail
 65	 * @access private
 66	 */
 67	private $mailObj = null;
 68
 69	/**
 70	 * The current factory (sending type).
 71	 * Only sendmail or smtp currently supported
 72	 *
 73	 * @var string
 74	 * @access private
 75	 */
 76	private $factory = null;
 77
 78	/**
 79	 * List of supported sending types.
 80	 * Only sendmail or smtp currently supported
 81	 *
 82	 * @var array
 83	 * @access private
 84	 */
 85	private $availableFactories = array('sendmail', 'smtp', 'mail', 'null');
 86
 87	/**
 88	 * List of available recipients.
 89	 * Add it with the methods addBcc() or addCc()
 90	 *
 91	 * @var array
 92	 * @access private
 93	 */
 94	private $recipients = array();
 95
 96	/**
 97	 * Constructor
 98	 *
 99	 * Creates a new instance from PEAR::Mail_mime
100	 * @link http://pear.php.net/manual/en/package.mail.mail-mime.php
101	 *
102	 * @return void
103	 * @access public
104	 */
105	public function __construct()
106	{
107		$this->mimeObj = new Mail_mime(self::CRLF);
108		$this->factory = 'sendmail';
109	}
110
111	/**
112	 * setFactory()
113	 *
114	 * Set the "sending" type of the email.
115	 * Only supported factories are allowed. Current there are sending via sendmail or smtp
116	 *
117	 * @return void
118	 * @access public
119	 */
120	public function SetFactory($Factory)
121	{
122		if(!empty($Factory) && in_array(strtolower($Factory), $this->availableFactories) )
123			$this->factory = strtolower($Factory);
124
125		return;
126	}
127
128	/**
129	 * GetFactory()
130	 *
131	 * Returns the current set factory
132	 *
133	 * @return string The current set factory
134	 * @access public
135	 */
136	public function GetFactory()
137	{
138		if(empty($this->factory)) return 'null';
139
140		return $this->factory;
141	}
142
143	/**
144	 * getAvailableFactories()
145	 *
146	 * Returns the list of all supported email factories
147	 *
148	 * @return array Assoc array of all supported email factories
149	 * @access public
150	 */
151	public function GetAvailableFactories()
152	{
153		if(empty($this->availableFactories)) return array('null');
154
155		return $this->availableFactories;
156	}
157
158	/**
159	 * GetMimeObj()
160	 *
161	 * Get the current mime object as reference.
162	 * @link http://pear.php.net/manual/en/package.mail.mail-mime.mail-mime.php
163	 *
164	 * @return Mail_mime The current mime object as reference
165	 * @access public
166	 */
167	public function &GetMimeObj()
168	{
169		return $this->mimeObj;
170	}
171
172	/**
173	 * GetMailObj()
174	 *
175	 * Get the current mailer object as reference.
176	 * @link http://pear.php.net/manual/en/package.mail.mail.factory.php
177	 *
178	 * @return Mail The current mailer object as reference.
179	 * @access public
180	 */
181	public function &GetMailObj()
182	{
183		if(is_null($this->mailObj))
184		{
185			switch($this->GetFactory())
186			{
187				case 'smtp':
188					{
189						$settings = $this->GetSmtpSettings();
190						$this->mailObj =& Mail::factory('smtp', $settings);
191					}
192					break;
193				case 'sendmail':
194					{
195						$settings = $this->GetSendmailSettings();
196						$this->mailObj =& Mail::factory('sendmail', $settings);
197					}
198					break;
199				case 'mail':
200					{
201						$this->mailObj =& Mail::factory('mail', array());
202					}
203				default:
204					{
205						$this->mailObj =& Mail::factory('null', array());
206					}
207					break;
208			}
209		}
210
211		return $this->mailObj;
212	}
213
214	/**
215	 * setFrom()
216	 *
217	 * Set the "from" part of the eMail.
218	 *
219	 * @param string $From The email address where the email is came from.
220	 * @return void
221	 * @access public
222	 */
223	public function SetFrom($From)
224	{
225		$this->mimeObj->setFrom($From);
226	}
227
228	/**
229	 * addCc()
230	 *
231	 * Add an "carbon copy (cc)" receptient to the eMail.
232	 *
233	 * Wrapper for Mail_mime::addCc()
234	 * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodaddCc
235	 *
236	 * Note: No email syntax verification implemented yet.
237	 *
238	 * @param string $Email The email address of the receptient
239	 * @return void
240	 * @access public
241	 */
242	public function AddCc($Email)
243	{
244		$this->mimeObj->addCc($Email);
245		$this->recipients[] = $Email;
246	}
247
248	/**
249	 * addBcc()
250	 *
251	 * Add an "blind carbon copy (bcc)" receptient to the eMail.
252	 *
253	 * Wrapper for Mail_mime::addBcc()
254	 * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodaddBcc
255	 *
256	 * Note: No email syntax verification implemented yet.
257	 *
258	 * @param string $Email The email address of the receptient
259	 * @return void
260	 * @access public
261	 */
262	public function AddBcc($Email)
263	{
264		$this->mimeObj->addBcc($Email);
265		$this->recipients[] = $Email;
266	}
267
268	/**
269	 * setSubject()
270	 *
271	 * Set the "subject" part of the eMail.
272	 *
273	 * Wrapper for Mail_mime::setSubject()
274	 * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodsetSubject
275	 *
276	 * @param string $Subject The designated subject of the email
277	 * @return void
278	 * @access public
279	 */
280	public function SetSubject($Subject)
281	{
282		$this->mimeObj->setSubject($Subject);
283	}
284
285	/**
286	 * setTextBody()
287	 *
288	 * Set the plain text part of the eMail.
289	 *
290	 * Wrapper for Mail_mime::setTXTBody()
291	 * @link http://pear.php.net/manual/en/package.mail.mail-mime.settxtbody.php
292	 *
293	 * If there is an error an exception will be thrown.
294	 *
295	 * @param string $Body The text to set or, if $Isfile is TRUE a valid filename. An URL as argument is not allowed.
296	 * @param bool $IsFile If TRUE, the content of given file $Body is used as message text.
297	 * @param bool $Append If TRUE, the content will be appended to the existing one.
298	 * @return bool TRUE if the body could be set successfully
299	 * @access public
300	 */
301	public function SetTextBody($Body, $IsFile = false, $Append = false)
302	{
303		$status = $this->mimeObj->setTXTBody($Body, $IsFile, $Append);
304		if(PEAR::isError($status))
305			throw new cfException(__METHOD__ .': Could not set the mail body (text) =>  ' .$status->getMessage());
306
307		return $status;
308	}
309
310	/**
311	 * setHtmlBody()
312	 *
313	 * Set the html part of the eMail
314	 *
315	 * Wrapper for Mail_mime::setHTMLBody()
316	 * @link http://pear.php.net/manual/en/package.mail.mail-mime.sethtmlbody.php
317	 *
318	 * @param string $Body The text to set or, if $Isfile is TRUE a valid filename. An URL as argument is not allowed.
319	 * @param bool $IsFile If TRUE, the content of given file $Body is used as message text.
320	 * @param bool $Append If TRUE, the content will be appended to the existing one.
321	 * @return bool TRUE if the body could be set successfully
322	 * @access public
323	 */
324	public function SetHtmlBody($Body, $IsFile = false, $Append = false)
325	{
326		$status = $this->mimeObj->setHTMLBody($Body, $IsFile, $Append);
327		if(PEAR::isError($status))
328			throw new cfException(__METHOD__ .': Could not set the mail body (html) => ' .$status->getMessage());
329
330		return $status;
331	}
332
333	/**
334	 * addHeaders()
335	 *
336	 * Build an array with the headers needed to prepend to the email (MIME-Version and Content-Type).
337	 *
338	 * Wrapper for Mail_mime::headers()
339	 * @link http://pear.php.net/manual/en/package.mail.mail-mime.headers.php
340	 *
341	 * @param array $Headers Additional headers, the format is $Headers["header-name"] = "header-value"
342	 * @param boolen $Override Overwrite already existing headers. When FALSE, the values already set are kept.
343	 * @return void
344	 * @access public
345	 */
346	public function AddHeaders($Headers)
347	{
348		$this->mimeObj->headers($Headers, false);
349	}
350
351	/**
352	 * getHeaders()
353	 *
354	 * Returns an array with the headers needed to prepend to the email (MIME-Version and Content-Type).
355	 *
356	 * Wrapper for Mail_mime::headers()
357	 * @link http://pear.php.net/manual/en/package.mail.mail-mime.headers.php
358	 *
359	 * @return array The current defined header lines
360	 * @access public
361	 */
362	public function GetHeaders()
363	{
364		return $this->mimeObj->headers();
365	}
366
367	/**
368	 * getPreparedHeaders()
369	 *
370	 * Returns an array with the headers needed to prepend to the email (MIME-Version and Content-Type).
371	 *
372	 * @return array The current defined header lines
373	 * @access public
374	 * @deprecated
375	 */
376	public function GetPreparedHeaders($Headers = null)
377	{
378		if(empty($Headers)) $Headers = $this->getHeaders();
379		$pHeaders = $this->GetMailObj()->prepareHeaders($Headers);
380		if(!is_array($pHeaders) || sizeof($pHeaders) != 2)
381			throw new cfException(__METHOD__ .': Could not prepare headers.');
382
383		return $pHeaders;
384	}
385
386	/**
387	 * addAttachment()
388	 *
389	 * Adds a file to the list of attachments.
390	 *
391	 * Wrapper for Mail_mime::addAttachment()
392	 * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodaddAttachment
393	 *
394	 * @param string $File The file name of the file to attach OR the file contents itself
395	 * @param string $CType The content type. Defaults to "application/octet-stream"
396	 * @param string $Name The filename of the attachment. Only use if $File is the contents
397	 * @param bool $IsFile Whether $File is a filename or not. Defaults to true
398	 * @param string $Encoding The type of encoding to use. Defaults to base64. Possible values: 7bit, 8bit, base64 or quoted-printable.
399	 * @param string $Charset The character set used in the filename of this attachment.
400	 * @return bool Status
401	 * @access public
402	 */
403	public function AddAttachment($File, $CType = 'application/octet-stream', $Name = '', $IsFile = true, $Encoding = 'base64', $Charset = '')
404	{
405		$status = $this->mimeObj->addAttachment($File, $CType, $Name, $IsFile, $Encoding, '', $Charset);
406		if(PEAR::isError($status))
407			throw new cfException(__METHOD__ .': Could not add attachment => ' .$status->getMessage());
408
409		return $status;
410	}
411
412	/**
413	 * addHTMLImage()
414	 *
415	 * Adds an image to the list of embedded images.
416	 *
417	 * Wrapper for Mail_mime::addHTMLImage()
418	 * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodaddHTMLImage
419	 *
420	 * @param string $File The file name of the file to attach OR the file contents itself
421	 * @param string $CType The content type. Defaults to "application/octet-stream"
422	 * @param string $Name The filename of the attachment. Only use if $File is the contents
423	 * @param bool $IsFile Whether $File is a filename or not. Defaults to true
424	 * @return bool Status
425	 * @access public
426	 */
427	public function AddHTMLImage($File, $CType = 'application/octet-stream', $Name = '', $IsFile = true)
428	{
429		$status = $this->mimeObj->addHTMLImage($File, $CType, $Name, $IsFile);
430		if(PEAR::isError($status))
431			throw new cfException(__METHOD__ .': Could not add HTML image => ' .$status->getMessage());
432
433		return $status;
434	}
435
436	/**
437	 * GetSmtpSettings()
438	 *
439	 * Get SMPT settings from the config or set it to defaults to send via localhost.
440	 *
441	 * @return array Assoc array of the current defined settings
442	 * @access private
443	 */
444	public function GetSmtpSettings()
445	{
446		$settings=array(
447			'host' => 'localhost',
448			'port' => 25,
449			'auth' => true,
450			'username' => '',
451			'password' => '',
452			'localhost' => 'localhost'
453		);
454
455		if(cfConfig::IsEntrySet('smtp_host', 'application'))
456		{
457			$settings['host'] = cfConfig::GetEntry('smtp_host', 'application');
458		}
459		if(cfConfig::IsEntrySet('smtp_port', 'application'))
460		{
461			$settings['port'] = cfConfig::GetEntry('smtp_port', 'application');
462		}
463		if(cfConfig::IsEntrySet('smtp_auth', 'application'))
464		{
465			$settings['auth'] = cfConfig::GetEntry('smtp_auth', 'application');
466		}
467		if(cfConfig::IsEntrySet('smtp_username', 'application'))
468		{
469			$settings['username'] = cfConfig::GetEntry('smtp_username', 'application');
470		}
471		if(cfConfig::IsEntrySet('smtp_password', 'application'))
472		{
473			$settings['password'] = cfConfig::GetEntry('smtp_password', 'application');
474		}
475		if(cfConfig::IsEntrySet('smtp_localhost', 'application'))
476		{
477			$settings['localhost'] = cfConfig::GetEntry('smtp_localhost', 'application');
478		}
479
480		if(empty($settings['username']) && empty($settings['password']) && ($settings['host'] == 'localhost' || $settings['host'] == '127.0.0.1'))
481		{
482			$settings['auth'] = false;
483		}
484
485		return $settings;
486	}
487
488	/**
489	 * GetSendmailSettings()
490	 *
491	 * Get sendmail settings from the config or set it to defaults.
492	 *
493	 * @return array Assoc array of the current defined settings
494	 * @access private
495	 */
496	public function GetSendmailSettings()
497	{
498		$settings=array(
499			'sendmail_path' => '/usr/sbin/sendmail',
500			'sendmail_args' => '-i'
501		);
502
503		if(cfConfig::IsEntrySet('sendmail_path', 'application'))
504		{
505			$settings['sendmail_path'] = cfConfig::GetEntry('sendmail_path', 'application');
506		}
507		if(cfConfig::IsEntrySet('sendmail_args', 'application'))
508		{
509			$settings['sendmail_args'] = cfConfig::GetEntry('sendmail_args', 'application');
510		}
511
512		return $settings;
513	}
514
515	/**
516	 * getBody()
517	 *
518	 * Builds the multipart message and returns the mime content.
519	 *
520	 * Wrapper for Mail_mime::get()
521	 * @link http://pear.php.net/package/Mail_Mime/docs/latest/Mail_Mime/Mail_mime.html#methodget
522	 *
523	 * @return string The MIME message content string
524	 * @access public
525	 */
526	public function GetBody()
527	{
528		$options = array(
529			'text_charset' => 'utf-8',
530			'html_charset' => 'utf-8',
531			'head_charset' => 'utf-8'
532		);
533		return $this->GetMimeObj()->get($options);
534	}
535
536	/**
537	 * After_send()
538	 *
539	 * OVERLOAD ME: Here you are able to put some additional actions after sending a mail.
540	 *
541	 * @param bool $Status Send status
542	 * @param Mail $MailObj The current mailer object
543	 * @param array $Options Additional options
544	 * @return void
545	 * @access protected
546	 */
547	protected function After_send($Status, &$MailObj, $Options = array())
548	{
549		return;
550	}
551
552	/**
553	 * send()
554	 *
555	 * Send the eMail.
556	 *
557	 * If a error occured, an exception will be thrown.
558	 *
559	 * @param string $To eMail address of the receiptient. Format name@domain.com or "My Name <name@domain.com>"
560	 * @param string $Factory Send over sendmail or smtp
561	 * @param array $Options (optional) Additional options
562	 * @return bool Send status
563	 * @access public
564	 */
565	public function Send($To, $Factory = null, $Options = array())
566	{
567		if(empty($To))
568			throw new cfException(__METHOD__ .': Could not send the email => Invalid eMail address "' .$To .'"');
569
570		if(!empty($Factory))
571			$this->SetFactory(trim($Factory));
572
573		$returnTo = '';
574		$replyTo = '';
575		if(isset($Options['return_to']))
576		{
577			$returnTo = $Options['return_to'];
578		}
579		if(isset($Options['reply_to']))
580		{
581			$replyTo = $Options['reply_to'];
582		}
583		$this->AddHeaders(array('Return-Path' => $returnTo, 'Reply-To' => $replyTo, 'To' => $To));
584		$mailObj =& $this->GetMailObj();
585
586		//SEND MAIL
587		$recipients = array();
588		$recipients = explode(',', $To);
589		$recipients = array_merge($recipients, $this->recipients);
590
591		$eMailBody = $this->GetBody();
592		$eMailHeaders = $this->GetHeaders();
593
594		cfLogger::Log('cfFramework')->Info('Try to send a email to: ' .print_r($recipients, true));
595		$status = $mailObj->Send($recipients, $eMailHeaders, $eMailBody, $Options);
596
597		if(PEAR::isError($status))
598		{
599			cfLogger::Log('cfFramework')->Error('Unable to send the email => ' .$status->getMessage());
600			throw new cfException(__METHOD__ .': Unable to send the email => ' .$status->getMessage());
601		}
602		else
603			cfLogger::Log('cfFramework')->Info('Send-Status: ' .print_r($status, true));
604
605		$this->After_send($status, $this, $Options); //for additional actions
606		return $status;
607	}
608}
609
610?>