What is the purpose of using the HTML Mime mail class in PHP for sending email attachments?

The purpose of using the HTML Mime mail class in PHP for sending email attachments is to easily include HTML content and attachments in the emails being sent. This class helps in formatting the email content in HTML format and attaching files such as images, documents, or any other files to the email.

<?php
require_once('Mail.php');
require_once('Mail/mime.php');

$from = "sender@example.com";
$to = "recipient@example.com";
$subject = "Email with attachment";
$body = "This is an email with an attachment.";

$host = "smtp.example.com";
$username = "username";
$password = "password";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$mime = new Mail_mime("\n");
$mime->setHTMLBody($body);

$file = "/path/to/attachment.pdf";
$mime->addAttachment($file, 'application/pdf');

$body = $mime->get();
$headers = $mime->headers($headers);

$smtp = Mail::factory('smtp', array(
    'host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password
));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo 'Error sending email: ' . $mail->getMessage();
} else {
    echo 'Email sent successfully!';
}
?>