[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 /** 4 * \defgroup Mail 5 * 6 * Module that allows to easily send email messages from within pLog. The actual email sending 7 * is carried out via the PHPMailer package (http://phpmailer.sourceforge.net/) and the classes of 8 * this module are just commodity wrappers around PHPMailer. 9 * 10 * The EmailMessage class is an abstract representation of an email message, while the EmailService 11 * class takes care of sending out the messages. 12 */ 13 14 15 lt_include( PLOG_CLASS_PATH."class/config/config.class.php" ); 16 lt_include( PLOG_CLASS_PATH."class/mail/emailmessage.class.php" ); 17 lt_include( PLOG_CLASS_PATH."class/mail/phpmailer/class.phpmailer.php" ); 18 19 /** 20 * \ingroup Mail 21 * 22 * Provides services to send emails via PHPs built-in smtp capabilities. 23 * 24 * This service can be enabled or disabled by using the "email_service_enabled" from 25 * the configuration file. It also requires PHP to be built with the smtp handling 26 * libraries and those must be enabled. 27 * 28 * An example of how to send an email message is as follows: 29 * 30 * <pre> 31 * $message = new EmailMessage(); 32 * $message->addTo( "address1@domain.com" ); 33 * $message->addFrom( "myself@myself.com" ); 34 * $message->setSubject( "This is a sample message" ); 35 * $message->setBody( "this is the body of the message" ); 36 * $service = new EmailService(); 37 * if( $service->sendMessage( $message )) 38 * print( "message sent ok!" ); 39 * else 40 * print( "error sending message" ); 41 * </pre> 42 * 43 * The EmailService class hides all the details about how the message is sent, the mechanism, etc. 44 * It wil in fact use certain configuration parameters such as: 45 * 46 * - email_service_enabled to determine whether emails should be sent at all. 47 * - email_service_type can be one of these four values: 48 * # php (requires PHP's mail() function) 49 * # smtp (see below) 50 * # qmail 51 * # sendmail 52 * 53 * If email_service_type is set as <b>smtp</b>, the following settings are also required: 54 * 55 * - smtp_host name of the host used for sending the messages 56 * - smtp_port port where the SMTP is listening, 25 by default 57 * - smtp_use_authentication Wether we should perform some basic authentication agains the host 58 * 59 * If smtp_use_authentication is set to yes, the following settings will determine the right 60 * username and password to use: 61 * 62 * - smtp_username 63 * - smtp_password 64 */ 65 class EmailService 66 { 67 68 var $_config; 69 var $_lastErrorMessage; 70 71 /** 72 * Constructor 73 */ 74 function EmailService() 75 { 76 $this->_config =& Config::getConfig(); 77 $this->_lastErrorMessage = ""; 78 } 79 80 /** 81 * Sends the given message. 82 * 83 * @param message Object from the EmailMessage class that encapsulates all the different fields 84 * an email can have (quite basic, though) 85 * @return Returns true if operation was successful or false otherwise. 86 */ 87 function sendMessage( $message ) 88 { 89 // quit if the service has been disabled 90 if( !$this->_config->getValue( "email_service_enabled" )) 91 return false; 92 93 $this->_lastErrorMessage = ""; 94 95 // create a phpmailer object 96 $mail = new PHPMailer(); 97 98 // need to set PluginDir if we use lt_include() 99 $mail->PluginDir = PLOG_CLASS_PATH."class/mail/phpmailer/"; 100 101 // set a few fields 102 $mail->ContentType = $message->getMimeType(); 103 $mail->From = $message->getFrom(); 104 $mail->FromName = $message->getFromName(); 105 $mail->Subject = $message->getSubject(); 106 $mail->Body = $message->getBody(); 107 // set the destination addresses 108 foreach( $message->getTo() as $to ) 109 $mail->AddAddress( $to ); 110 foreach( $message->getCc() as $cc ) 111 $mail->AddCC( $cc ); 112 foreach( $message->getBcc() as $bcc ) 113 $mail->AddBCC( $bcc ); 114 115 // set the character set of the message 116 $mail->CharSet = $message->getCharset(); 117 118 // set the language for error reporting 119 $mail->SetLanguage( 'en', PLOG_CLASS_PATH."class/mail/phpmailer/language/" ); 120 121 // 122 // phpmailer supports 123 // php built-in mail() function 124 // qmail 125 // sendmail, using the $SENDMAIL variable 126 // smtp 127 // If using smtp, we need to provide a valid smtp server, and maybe a username 128 // and password if supported. 129 $mailServiceType = $this->_config->getValue( "email_service_type" ); 130 131 if( $mailServiceType == "php" ) { 132 $mail->IsMail(); 133 } 134 elseif( $mailServiceType == "qmail" ) { 135 $mail->IsQmail(); 136 } 137 elseif( $mailServiceType == "sendmail" ) { 138 $mail->IsSendmail(); 139 } 140 elseif( $mailServiceType == "smtp" ) { 141 $mail->IsSMTP(); 142 143 $useAuthentication = $this->_config->getValue( "smtp_use_authentication" ); 144 145 // check if we should use authentication 146 if( $useAuthentication ) { 147 $smtpUsername = $this->_config->getValue( "smtp_username" ); 148 $smtpPassword = $this->_config->getValue( "smtp_password" ); 149 if( $smtpUsername == "" || $smtpPassword == "" ) { 150 // this is a severe error 151 throw( new Exception( "Please provide a username and a password if you wish to use SMTP authentication" )); 152 $mail->SMTPAuth = false; 153 } 154 else { 155 // if the checks went ok, set the authentication 156 $mail->SMTPAuth = true; 157 $mail->Username = $smtpUsername; 158 $mail->Password = $smtpPassword; 159 } 160 } 161 else { 162 $mail->SMTPAuth = false; 163 } 164 165 // set the server 166 $smtpHost = $this->_config->getValue( "smtp_host" ); 167 $smtpPort = $this->_config->getValue( "smtp_port" ); 168 if( $smtpPort == "" ) 169 $smtpPort = 25; 170 171 if( $smtpHost == "" ) { 172 throw( new Exception( "You should specify an SMTP server in order to send emails." )); 173 return false; 174 } 175 else { 176 $mail->Host = $smtpHost; 177 $mail->Port = $smtpPort; 178 } 179 } 180 else { 181 // if none of the above is the right one, then let's use 182 // php's very own mail() as the fallback plan 183 $mail->IsMail(); 184 throw( new Exception( "Unrecognized value of the email_service_type setting. Reverting to PHP built-in mail() functionality" )); 185 } 186 187 // we have set up everything, send the mail 188 $result = $mail->Send(); 189 190 if( !$result ) 191 $this->_lastErrorMessage = $mail->ErrorInfo; 192 193 return( $result ); 194 } 195 196 /** 197 * Returns the last error message 198 * 199 * @return A string containing the last error message, if any 200 */ 201 function getLastErrorMessage() 202 { 203 return( $this->_lastErrorMessage ); 204 } 205 } 206 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |