Wie oft habe ich schon die Email-Klasse verwendet, nur um festzustellen, dass sie nicht funktioniert? Sicher… zwei mal 🙂 Deshalb: Sie funktioniert nicht. Stattdessen verwende ich PHPMailer, bzw. folgende Dateien davon:
Eingebunden werden die ein einem Helper (/application/helpers/email_helper.php):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); if (!function_exists('sendEmail')) { function sendEmail($to, $subject, $message) { // CodeIgniter's mailer does not work: require_once(APPPATH . 'third_party/PHPMailer/class.phpmailer.php'); $ci =& get_instance(); $ci->config->load('email'); $mail = new PHPMailer(); $mail->CharSet = 'utf-8'; $mail->IsSMTP(); $mail->Host = $ci->config->item('smtp_host'); $mail->SMTPAuth = true; $mail->SMTPSecure = 'ssl'; $mail->Username = $ci->config->item('smtp_user'); $mail->Password = $ci->config->item('smtp_pass'); $mail->Port = $ci->config->item('smtp_port'); $mail->From = $ci->config->item('smtp_user'); $mail->FromName = $ci->config->item('from_name'); $mail->AddAddress($to); $mail->AddReplyTo($ci->config->item('reply_to'), $ci->config->item('from_name')); $mail->Subject = $subject; $mail->Body = $message; $mail->AltBody = str_replace('<br/>', '', $message); if (!$mail->Send()) { log_message('error', $mail->ErrorInfo); // throw new Exception($mail->ErrorInfo); return FALSE; } return TRUE; } } |
mit entsprechender Config (/application(config/email.php, wird automatisch eingebunden):
1 2 3 4 5 6 7 8 9 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); $config['protocol'] = 'smtp'; $config['smtp_host'] = 'mail.example.com'; $config['smtp_user'] = 'foo@example.com'; $config['smtp_pass'] = 'mypassword'; $config['smtp_port'] = '465'; $config['reply_to'] = 'bar@example.com'; $config['from_name'] = 'CodeIgniter Mailer'; |
Aufruf dann via:
1 2 3 |
$this->load->helper('email'); $sent = sendEmail('myrecipient@example.com', 'Hello subject!', 'My message'); echo $sent ? 'sent.' : 'NOT sent!'; |
HTH