Why is it recommended to use a "fertigen" Mail-Class instead of the mail() function in PHP for sending emails?

Using a "fertigen" Mail-Class instead of the mail() function in PHP is recommended because it provides a more robust and secure way to send emails. The Mail-Class handles common email tasks such as creating headers, formatting the message, and handling attachments more efficiently. It also helps to prevent common email vulnerabilities such as header injection attacks.

// Example code using a "fertigen" Mail-Class to send an email
require 'Mail.php';

$from = 'sender@example.com';
$to = 'recipient@example.com';
$subject = 'Hello from PHP Mail-Class';
$body = 'This is a test email sent using PHP Mail-Class.';

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

$mail = Mail::factory('smtp', array(
    'host' => 'smtp.example.com',
    'port' => 25,
    'auth' => true,
    'username' => 'username',
    'password' => 'password'
));

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