# Sending email using PHPMailer

> Install PHPMailer with Composer or Git and use it to send HTML email with attachments and SMTP authentication.

Source: https://helpcenter.ruachost.com/developers/php/sending-email-using-phpmailer/

**PHPMailer** is a popular PHP library that makes it easier to send e‑mail messages with advanced features such as SMTP authentication, HTML formatting, and attachments. On **ruachost.com**, PHPMailer is supported and can be installed via **Composer** or **Git**.

## ???? Important Hosting Restrictions

- On **Shared Hosting**, you **cannot** use external SMTP servers.
- You must send e‑mail through **ruachost.com  servers**.
- Other hosting packages (Managed VPS, Dedicated) allow external SMTP servers.

## Installing PHPMailer

### Option 1: Using Composer

```bash
composer require phpmailer/phpmailer
```

### Option 2: Using Git

```bash
git clone https://github.com/PHPMailer/PHPMailer.git
```

## Example: Sending E‑Mail with PHPMailer

```
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';

$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = gethostname();   // Use ruachost.com servers for shared hosting/WordPress hosting
$mail->SMTPAuth = true;
$mail->Username = 'sender@example.com';
$mail->Password = 'password';
$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the body.';

if(!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
?>
```

## Features of PHPMailer

- **SMTP authentication** for secure delivery.
- **HTML e‑mail support** with inline images.
- **Attachments** for files.
- **Error handling** with detailed feedback.

## Best Practices

- Always set a valid `From` address.
- Use SMTP authentication when available.
- Test with different mail clients to ensure formatting.
- Avoid sending bulk mail directly from PHP scripts; use mailing services for campaigns.
