# Microsoft Graph Mailer Usage Instructions

I've created a new `graphmailer.php` file that can replace the PHPMailer functionality in your system. Here's how to use it:

## File Created: `graphmailer.php`

This file provides the following functions that are compatible with your existing PHPMailer function signatures:

### Functions Available:

1. **`sendMail($email_message, $from_email, $from_name, $to_array, $subject)`**
   - Compatible with existing PHPMailer function
   - Sends email without attachments

2. **`sendMailAttachments($email_message, $from_email, $from_name, $to_array, $subject, $attachments)`**
   - Compatible with existing PHPMailer function
   - Sends email with attachments

3. **`sendSimpleMail($to, $subject, $message, $from = 'info@beaconhotel.co.uk')`**
   - New simplified function for basic emails

## How to Replace PHPMailer in Existing Files

### Example: Updating `email_invoice.php`

**Current code:**
```php
include ("includes/phpmailer.php");
```

**Replace with:**
```php
include ("graphmailer.php");
```

That's it! The function calls remain exactly the same:

```php
sendMailAttachments($emailbody, "info@beaconhotel.co.uk", $email_from, $to_array, "Invoice from Beacon ",$attachments );
```

## Configuration Requirements

The Microsoft Graph API credentials are already configured in your `config.php` file:
- `$tenant` - Azure tenant ID
- `$client` - Application (client) ID  
- `$secret` - Client secret

## Features

### Automatic Sender Override
Just like your PHPMailer setup, the sender email is automatically set to `info@beaconhotel.co.uk` regardless of what's passed to the function.

### Attachment Support
- Supports files up to ~2.2MB (same as existing system)
- Automatically handles file encoding and MIME types
- Error logging for missing or oversized files

### Error Handling
- Comprehensive error logging using PHP's `error_log()`
- Returns `true` on success, `false` on failure (same as PHPMailer functions)
- Handles Graph API errors gracefully

### Compatible Data Formats
The `$to_array` format remains the same:
```php
$to_array = array();
$to_array[] = array("to_email" => "guest@example.com", "to_name" => "Guest Name");
```

## Files That Can Be Updated

You can replace the PHPMailer include in these files:
- `email_invoice.php`
- `meal_only_email_invoice.php`
- `submitOrder.php`
- Any other files using `includes/phpmailer.php`

## Testing

The system includes comprehensive error logging, so check your PHP error logs if emails fail to send. The Graph API will return detailed error messages for troubleshooting.

## Benefits of Microsoft Graph

1. **Better Reliability** - Uses Microsoft's enterprise email infrastructure
2. **No SMTP Configuration** - No need to manage SMTP server settings
3. **Built-in Security** - OAuth 2.0 authentication with proper scoping
4. **Detailed Logging** - Better error reporting and troubleshooting
5. **Future-Proof** - Microsoft Graph is actively maintained and updated

## Migration Steps

1. **Test First**: Try the new `graphmailer.php` on a non-production system
2. **Update One File**: Start with one file like `email_invoice.php`
3. **Verify Functionality**: Send test emails and check logs
4. **Gradual Rollout**: Update other files one by one
5. **Monitor**: Watch error logs for any issues

## Troubleshooting

If emails fail to send:
1. Check PHP error logs for detailed error messages
2. Verify Microsoft Graph API permissions are correctly set
3. Ensure the sender email (`info@beaconhotel.co.uk`) is properly configured in your Microsoft 365 tenant
4. Check that attachment files exist and are readable

The new system maintains full backward compatibility with your existing code while providing the reliability and features of Microsoft Graph API.
