PageRenderTime 64ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Module/Mail/PhpMailer.php

https://github.com/DerDu/AIO-System
PHP | 197 lines | 137 code | 5 blank | 55 comment | 14 complexity | 95f6fa4ffdb456c9e89bede535e668de MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * PhpMail
  4. *
  5. // ---------------------------------------------------------------------------------------
  6. * LICENSE (BSD)
  7. *
  8. * Copyright (c) 2011, Gerd Christian Kunze
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are
  13. * met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. *
  22. * * Neither the name of Gerd Christian Kunze nor the names of the
  23. * contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. // ---------------------------------------------------------------------------------------
  38. *
  39. * @package AIOSystem\Module
  40. * @subpackage Mail
  41. */
  42. namespace AIOSystem\Module\Mail;
  43. use \AIOSystem\Api\Event;
  44. /**
  45. * @package AIOSystem\Module
  46. * @subpackage Mail
  47. */
  48. interface InterfacePhpMailer
  49. {
  50. public static function phpmailer_send();
  51. public static function phpmailer_subject( $string_subject_text );
  52. public static function phpmailer_body( $string_body_text );
  53. public static function phpmailer_attachment( $string_filename );
  54. public static function phpmailer_from( $string_from_mail, $string_from_name = '' );
  55. public static function phpmailer_to( $string_to_mail, $string_to_name = '' );
  56. public static function phpmailer_reply( $string_reply_mail, $string_reply_name = '' );
  57. public static function phpmailer_smtp( $string_hostname, $string_username, $string_password, $string_smtpport = 25 );
  58. }
  59. /**
  60. * @package AIOSystem\Module
  61. * @subpackage Mail
  62. */
  63. class ClassPhpMailer implements InterfacePhpMailer
  64. {
  65. /** @var \PHPMailer $phpmailer_instance */
  66. private static $phpmailer_instance = null;
  67. public function __construct() {
  68. }
  69. // EXECUTION------------------------------------------------------------------------------------
  70. public static function phpmailer_send()
  71. {
  72. ob_start(); self::phpmailer_instance()->Send(); $Status = ob_get_contents();ob_end_clean();
  73. if( self::phpmailer_instance()->IsError() ) {
  74. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  75. self::phpmailer_instance()->error_count = 0;
  76. self::phpmailer_instance()->ErrorInfo = null;
  77. }
  78. self::$phpmailer_instance = null;
  79. return $Status;
  80. }
  81. // CONTENT -------------------------------------------------------------------------------
  82. public static function phpmailer_subject( $string_subject_text )
  83. {
  84. ob_start(); self::phpmailer_instance()->Subject = $string_subject_text; ob_end_clean();
  85. if( self::phpmailer_instance()->IsError() ) {
  86. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  87. self::phpmailer_instance()->error_count = 0;
  88. self::phpmailer_instance()->ErrorInfo = null;
  89. }
  90. }
  91. public static function phpmailer_body( $string_body_text, $isHtml = true ) {
  92. ob_start(); self::phpmailer_instance()->IsHTML( $isHtml ); ob_end_clean();
  93. ob_start(); self::phpmailer_instance()->MsgHTML( $string_body_text ); ob_end_clean();
  94. if( self::phpmailer_instance()->IsError() ) {
  95. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  96. self::phpmailer_instance()->error_count = 0;
  97. self::phpmailer_instance()->ErrorInfo = null;
  98. }
  99. }
  100. public static function phpmailer_attachment( $string_filename )
  101. {
  102. ob_start(); if( file_exists( $string_filename ) ) self::phpmailer_instance()->AddAttachment( $string_filename ); ob_end_clean();
  103. if( self::phpmailer_instance()->IsError() ) {
  104. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  105. self::phpmailer_instance()->error_count = 0;
  106. self::phpmailer_instance()->ErrorInfo = null;
  107. }
  108. }
  109. // ADDRESS -------------------------------------------------------------------------------
  110. public static function phpmailer_from( $string_from_mail, $string_from_name = '' )
  111. {
  112. ob_start(); self::phpmailer_instance()->SetFrom( $string_from_mail, $string_from_name ); ob_end_clean();
  113. if( self::phpmailer_instance()->IsError() ) {
  114. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  115. self::phpmailer_instance()->error_count = 0;
  116. self::phpmailer_instance()->ErrorInfo = null;
  117. }
  118. }
  119. public static function phpmailer_check_to( $string_to_mail ) {
  120. ob_start(); self::phpmailer_instance()->ValidateAddress( $string_to_mail ); $Status = ob_get_contents();ob_end_clean();
  121. if( self::phpmailer_instance()->IsError() ) {
  122. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  123. self::phpmailer_instance()->error_count = 0;
  124. self::phpmailer_instance()->ErrorInfo = null;
  125. }
  126. return $Status;
  127. }
  128. public static function phpmailer_to( $string_to_mail, $string_to_name = '' )
  129. {
  130. ob_start(); self::phpmailer_instance()->AddAddress( $string_to_mail, $string_to_name ); ob_end_clean();
  131. if( self::phpmailer_instance()->IsError() ) {
  132. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  133. self::phpmailer_instance()->error_count = 0;
  134. self::phpmailer_instance()->ErrorInfo = null;
  135. }
  136. }
  137. public static function phpmailer_to_cc( $string_to_mail, $string_to_name = '' )
  138. {
  139. ob_start(); self::phpmailer_instance()->AddCC( $string_to_mail, $string_to_name ); ob_end_clean();
  140. if( self::phpmailer_instance()->IsError() ) {
  141. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  142. self::phpmailer_instance()->error_count = 0;
  143. self::phpmailer_instance()->ErrorInfo = null;
  144. }
  145. }
  146. public static function phpmailer_to_bcc( $string_to_mail, $string_to_name = '' )
  147. {
  148. ob_start(); self::phpmailer_instance()->AddBCC( $string_to_mail, $string_to_name ); ob_end_clean();
  149. if( self::phpmailer_instance()->IsError() ) {
  150. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  151. self::phpmailer_instance()->error_count = 0;
  152. self::phpmailer_instance()->ErrorInfo = null;
  153. }
  154. }
  155. public static function phpmailer_reply( $string_reply_mail, $string_reply_name = '' )
  156. {
  157. ob_start(); self::phpmailer_instance()->AddReplyTo( $string_reply_mail, $string_reply_name ); ob_end_clean();
  158. if( self::phpmailer_instance()->IsError() ) {
  159. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  160. self::phpmailer_instance()->error_count = 0;
  161. self::phpmailer_instance()->ErrorInfo = null;
  162. }
  163. }
  164. // AUTHENTICATION ------------------------------------------------------------------------
  165. public static function phpmailer_smtp( $string_hostname, $string_username, $string_password, $string_smtpport = 25 )
  166. {
  167. ob_start();
  168. self::phpmailer_instance()->IsSMTP();
  169. //self::phpmailer_instance()->SMTPDebug = 2;
  170. self::phpmailer_instance()->SMTPAuth = true;
  171. self::phpmailer_instance()->Host = $string_hostname;
  172. self::phpmailer_instance()->Username = $string_username;
  173. self::phpmailer_instance()->Password = $string_password;
  174. self::phpmailer_instance()->Port = $string_smtpport;
  175. ob_end_clean();
  176. if( self::phpmailer_instance()->IsError() ) {
  177. Event::Error( 0, self::phpmailer_instance()->ErrorInfo, __FILE__, __LINE__ );
  178. self::phpmailer_instance()->error_count = 0;
  179. self::phpmailer_instance()->ErrorInfo = null;
  180. }
  181. }
  182. // ---------------------------------------------------------------------------------------
  183. private static function phpmailer_instance()
  184. {
  185. if( self::$phpmailer_instance === null ) {
  186. if( !class_exists('PHPMailer') ) require_once(dirname(__FILE__) . '/PhpMailer/class.phpmailer.php');
  187. self::$phpmailer_instance = new \PHPMailer();
  188. }
  189. return self::$phpmailer_instance;
  190. }
  191. }
  192. ?>