Authentication php pear class mail smtp

Here is an example of a PHP class that uses PEAR's Mail and Net_SMTP classes to send an email with authentication:

<?php

require_once 'Mail.php';
require_once 'Net/SMTP.php';

class SMTPMailer {
    private $host;
    private $port;
    private $username;
    private $password;
    private $from;
    private $to;
    private $subject;
    private $body;

    public function __construct($host, $port, $username, $password, $from, $to, $subject, $body) {
        $this->host = $host;
        $this->port = $port;
        $this->username = $username;
        $this->password = $password;
        $this->from = $from;
        $this->to = $to;
        $this->subject = $subject;
        $this->body = $body;
    }

    public function send() {
        $mail = Mail::factory('smtp', array(
            'host' => $this->host,
            'port' => $this->port,
            'auth' => true,
            'username' => $this->username,
            'password' => $this->password
        ));

        $message = $mail->build(array(
            'from' => $this->from,
            'to' => $this->to,
            'subject' => $this->subject,
            'body' => $this->body
        ));

        $result = $mail->send($message);

        if (PEAR::isError($result)) {
            echo $result->getMessage();
        } else {
            echo "Email sent successfully!";
        }
    }
}

// Example usage:
$mailer = new SMTPMailer('smtp.example.com', 587, 'your_username', 'your_password', '[email protected]', '[email protected]', 'Subject', 'Body');
$mailer->send();

?>

This class uses the Mail class from PEAR to create an SMTP mailer object, and the Net_SMTP class to connect to the SMTP server and send the email.

The __construct method sets the properties of the class, such as the host, port, username, password, from, to, subject, and body of the email.

The send method uses the Mail class to build the email message and send it using the Net_SMTP class. The auth parameter is set to true to enable authentication, and the username and password parameters are used to authenticate with the SMTP server.

You can customize the class to fit your specific needs, such as adding additional headers or attachments to the email.