What is the difference between using $mail->setFrom and a Username-Password combination in PHPMailer?
When using PHPMailer, setting the From address using $mail->setFrom() allows you to specify the sender's email address without needing to provide a username and password combination. This method is useful when sending emails from a specific email address without authenticating with a mail server. On the other hand, using a Username-Password combination is necessary when sending emails through an SMTP server that requires authentication.
// Using $mail->setFrom() to set the sender's email address without authentication
$mail = new PHPMailer(true);
$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Subject';
$mail->Body = 'Message body';
$mail->send();
```
```php
// Using a Username-Password combination for authentication
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_username';
$mail->Password = 'your_password';
$mail->setFrom('sender@example.com', 'Sender Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Subject';
$mail->Body = 'Message body';
$mail->send();