PHPMailer::smtpClose PHP Method

smtpClose() public method

Close the active SMTP session if one exists.
public smtpClose ( ) : void
return void
    public function smtpClose()
    {
        if (is_a($this->smtp, 'SMTP')) {
            if ($this->smtp->connected()) {
                $this->smtp->quit();
                $this->smtp->close();
            }
        }
    }

Usage Example

Example #1
1
 public function smtp($to, $subject, $body, $option = null)
 {
     if (is_array($option)) {
         if (array_key_exists('name', $option)) {
             $this->name = $option['name'];
         }
         if (array_key_exists('debug', $option)) {
             $this->debug = $option['debug'];
         }
     }
     $mail = new PHPMailer(true);
     try {
         $mail->IsSMTP();
         // telling the class to use SMTP
         $mail->CharSet = 'UTF-8';
         $mail->XMailer = ' ';
         $mail->IsHTML(true);
         //$mail->SMTPSecure = 'tls';
         $mail->SMTPDebug = $this->debug;
         // enables SMTP debug information (for testing)
         // 1 = errors and messages
         // 2 = messages only
         $mail->Host = $this->host;
         // sets the SMTP server
         $mail->Port = 25;
         // set the SMTP port for the GMAIL server
         $mail->SMTPAuth = false;
         // enable SMTP authentication
         $mail->Username = $this->username;
         // SMTP account username
         $mail->Password = $this->password;
         // SMTP account password
         $mail->SetFrom($this->from, 'Webmaster');
         $mail->AddReplyTo($this->replyto, 'Webmaster');
         $mail->Subject = $subject;
         //$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
         //$body             = eregi_replace("[\]",'',$body);
         $mail->MsgHTML($body);
         $mail->ClearAddresses();
         $mail->AddAddress($to, $this->name);
         //$mail->AddAttachment("images/phpmailer.gif");      // attachment
         //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
         if (!$mail->Send()) {
             echo "Mailer Error: " . $mail->ErrorInfo;
         }
     } catch (phpmailerException $e) {
         echo $e->errorMessage();
         //Pretty error messages from PHPMailer
     } catch (Exception $e) {
         echo $e->getMessage();
         //Boring error messages from anything else!
     } finally {
         $mail->smtpClose();
     }
 }
All Usage Examples Of PHPMailer::smtpClose