Skip to content

Instantly share code, notes, and snippets.

@standa
Created March 16, 2014 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save standa/9589905 to your computer and use it in GitHub Desktop.
Save standa/9589905 to your computer and use it in GitHub Desktop.
Send Email Using CodeIgniter
private function sendEmail() {
$this->load->library('email');
$this->config->load('email');
$tos = explode(',', $this->input->post('emaily'));
// print_r($tos);
if (empty($tos)) {
// echo('sendEmail(): Tos empty.');
return;
}
foreach ($tos as $to) {
$this->email->bcc($to);
$body = urldecode($this->input->post('email_text'));
if ($this->input->post('predmet')) {
$subject = $this->input->post('predmet');
} else {
$subject = '';
}
$this->email->from(
$this->config->item('from_email'),
$this->config->item('from_name')
);
$ret['from'] = $this->config->item('from_name')
.' <'.$this->config->item('from_email').'>';
if (empty($subject)) {
$this->email->subject($this->config->item('default_subject'));
$ret['subject'] = $this->config->item('default_subject');
} else {
$this->email->subject($subject);
$ret['subject'] = $subject;
}
$this->email->message($body);
$ret['body'] = $body;
if (!empty($_FILES)) {
$ret['attachments'] = array();
if (is_array($_FILES['soubor']['tmp_name'])) {
foreach ($_FILES['soubor']['tmp_name'] as $k => $u) {
$n = str_replace(basename($u), $_FILES['soubor']['name'][$k], $u);
if (file_exists($u)) {
@rename($u, $n);
$this->email->attach($n);
$ret['attachments'][] = $n;
}
}
} else if (file_exists($_FILES['soubor']['tmp_name'])) {
$n = str_replace(basename($u), $_FILES['soubor']['name'], $_FILES['soubor']['tmp_name']);
@rename($_FILES['soubor']['tmp_name'], $n);
$this->email->attach($n);
$ret['attachments'][] = $n;
}
}
$this->email->send();
} // foreach
$ret['to'] = $tos;
$ret['user'] = $this->session->userdata('username');
// $ret['debug'] = $this->email->print_debugger();
$z = array(
'objekt' => 'email',
'objekt_id' => time(),
'zprava' => json_encode($ret)
);
$this->zaznam_model->insert($z);
if (!empty($ret['attachments'])) {
foreach ($ret['attachments'] as $a) {
if (file_exists($a)) {
@unlink($a);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment