// Einfache E-Mail
$mail = rex_mailer::factory();
$mail->setFrom('absender@example.com', 'Absender Name');
$mail->addTo('empfaenger@example.com', 'Empfänger Name');
$mail->setSubject('Test-E-Mail');
$mail->setBody('<h1>Hallo</h1><p>Dies ist eine Test-E-Mail.</p>');
$mail->setAltBody('Hallo, dies ist eine Test-E-Mail.'); // Plain-Text
if ($mail->send()) {
echo 'E-Mail erfolgreich gesendet';
} else {
echo 'Fehler: ' . $mail->getError();
}
// Mehrere Empfänger
$mail = rex_mailer::factory();
$mail->setFrom('noreply@example.com');
$mail->addTo('user1@example.com', 'User 1');
$mail->addTo('user2@example.com', 'User 2');
$mail->addCC('manager@example.com', 'Manager');
$mail->addBCC('admin@example.com'); // Ohne Name
$mail->setSubject('Newsletter');
$mail->setBody('Newsletter-Inhalt...');
$mail->send();
// Mit Anhang (Datei)
$mail = rex_mailer::factory();
$mail->setFrom('info@example.com');
$mail->addTo('kunde@example.com');
$mail->setSubject('Rechnung');
$mail->setBody('Anbei finden Sie Ihre Rechnung.');
$mail->addAttachment(rex_path::media('rechnung.pdf'), 'Rechnung.pdf');
$mail->send();
// Mit Anhang (String/Content)
$csv = "Name,Email\nMax,max@example.com\nAnna,anna@example.com";
$mail = rex_mailer::factory();
$mail->setFrom('export@example.com');
$mail->addTo('admin@example.com');
$mail->setSubject('CSV-Export');
$mail->setBody('Export als CSV im Anhang.');
$mail->addStringAttachment($csv, 'export.csv', 'text/csv');
$mail->send();
// Reply-To
$mail = rex_mailer::factory();
$mail->setFrom('noreply@example.com', 'System');
$mail->addReplyTo('support@example.com', 'Support');
$mail->addTo('kunde@example.com');
$mail->setSubject('Kontaktanfrage erhalten');
$mail->setBody('Ihre Anfrage wurde erfasst.');
$mail->send();
// HTML-E-Mail mit Bildern
$logo_url = rex_url::media('logo.png');
$html = '
<!DOCTYPE html>
<html>
<head><style>body { font-family: Arial; }</style></head>
<body>
<img src="' . $logo_url . '" alt="Logo">
<h1>Willkommen</h1>
<p>Vielen Dank für Ihre Registrierung.</p>
</body>
</html>
';
$mail = rex_mailer::factory();
$mail->setFrom('welcome@example.com');
$mail->addTo('neuer-user@example.com');
$mail->setSubject('Willkommen');
$mail->setBody($html);
$mail->setAltBody('Willkommen! Vielen Dank für Ihre Registrierung.');
$mail->send();
// E-Mail aus Template
$template = rex_file::get(rex_path::addon('project', 'email_templates/welcome.php'));
$replacements = [
'###NAME###' => 'Max Mustermann',
'###EMAIL###' => 'max@example.com',
'###URL###' => 'https://example.com/activate/abc123'
];
$body = str_replace(array_keys($replacements), array_values($replacements), $template);
$mail = rex_mailer::factory();
$mail->addTo('max@example.com');
$mail->setSubject('Aktivierung erforderlich');
$mail->setBody($body);
$mail->send();
// Fehlerbehandlung
$mail = rex_mailer::factory();
$mail->addTo('invalid-email'); // Ungültig
$mail->setSubject('Test');
$mail->setBody('Test');
if (!$mail->send()) {
rex_logger::logError('email', 'E-Mail-Versand fehlgeschlagen: ' . $mail->getError());
echo 'E-Mail konnte nicht gesendet werden';
}
// Mit YForm Action kombinieren
// In YForm Modul-Eingabe:
$yform = rex_yform::factory();
$yform->setActionField('email', [
'empfaenger@example.com',
'Kontaktformular',
'Name: ###name###\nEmail: ###email###\nNachricht: ###message###',
'absender@example.com',
'Website'
]);
// Custom Mailer für Profil
$mail = rex_mailer::factory();
// Profil aus DB laden (rex_mailer_profile addon)
$profile_id = 2;
$sql = rex_sql::factory();
$sql->setQuery('SELECT * FROM rex_mailer_profile WHERE id = ?', [$profile_id]);
if ($sql->getRows()) {
$mail->setFrom($sql->getValue('from'), $sql->getValue('fromname'));
}
$mail->addTo('kunde@example.com');
$mail->setSubject('Nachricht');
$mail->setBody('Ihre Nachricht...');
$mail->send();
// Batch-Versand (Newsletter)
$recipients = rex_sql::factory();
$recipients->setQuery('SELECT email, name FROM rex_newsletter_subscribers WHERE active = 1');
$mail = rex_mailer::factory();
$mail->setFrom('newsletter@example.com', 'Newsletter');
$mail->setSubject('Neuer Newsletter');
$html = rex_file::get(rex_path::addon('project', 'newsletter_template.html'));
foreach ($recipients as $row) {
$body = str_replace('###NAME###', $row->getValue('name'), $html);
$mail->clearAddresses();
$mail->addTo($row->getValue('email'), $row->getValue('name'));
$mail->setBody($body);
if (!$mail->send()) {
rex_logger::logError('newsletter', 'Failed to send to: ' . $row->getValue('email'));
}
sleep(1); // Rate limiting
}
// Transaktions-E-Mail (Bestellbestätigung)
function sendOrderConfirmation($order_id) {
$order = rex_yform_manager_dataset::get($order_id, 'rex_orders');
$mail = rex_mailer::factory();
$mail->setFrom('shop@example.com', 'Example Shop');
$mail->addTo($order->customer_email, $order->customer_name);
$mail->setSubject('Bestellbestätigung #' . $order_id);
$html = '<h1>Bestellbestätigung</h1>';
$html .= '<p>Vielen Dank für Ihre Bestellung.</p>';
$html .= '<p>Bestellnummer: ' . $order_id . '</p>';
$html .= '<h2>Artikel:</h2><ul>';
foreach ($order->getItems() as $item) {
$html .= '<li>' . $item->product_name . ' - ' . $item->quantity . 'x ' . $item->price . ' €</li>';
}
$html .= '</ul><p><strong>Gesamt: ' . $order->total . ' €</strong></p>';
$mail->setBody($html);
// PDF-Rechnung anhängen
$invoice_path = rex_path::addonData('shop', 'invoices/invoice_' . $order_id . '.pdf');
if (file_exists($invoice_path)) {
$mail->addAttachment($invoice_path, 'Rechnung.pdf');
}
return $mail->send();
}
// E-Mail mit eingebettetem Bild (CID)
$mail = rex_mailer::factory();
$mail->addTo('empfaenger@example.com');
$mail->setSubject('Embedded Image');
$logo_path = rex_path::media('logo.png');
$cid = $mail->addEmbeddedImage($logo_path, 'logo');
$html = '<img src="cid:' . $cid . '" alt="Logo"><h1>Hallo</h1>';
$mail->setBody($html);
$mail->send();
// Debug-Modus
$mail = rex_mailer::factory();
$mail->SMTPDebug = 2; // 0=off, 1=client, 2=client+server
$mail->Debugoutput = function($str, $level) {
rex_logger::factory()->log($level, $str);
};
$mail->setFrom('test@example.com');
$mail->addTo('empfaenger@example.com');
$mail->setSubject('Debug-Test');
$mail->setBody('Test');
$mail->send();