PHP sending emails to multiple recipients with AJAX (client side).
# message header $headers = "From: " . $_POST['to'] . "\r\n"; if(!empty($_POST['cc'])){ $headers .= "CC: " . $_POST['cc'] . "\r\n"; } if(!empty($_POST['bcc'])){ $headers .= "BCC: " . $_POST['bcc'] . "\r\n"; } if($_POST['type'] == 'html') { $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; }if(mail($_POST['to'], $_POST['subject'], $_POST['compose'], $headers)) { $result['status'] = 'success'; } else { $result['status'] = 'error'; }
# config SMTP $config['protocol'] = 'smtp'; $config['smtp_host'] = 'mail.host.com'; $config['smtp_port'] = 123; $config['smtp_crypto'] = 'tls'; $config['smtp_user'] = '[email protected]'; $config['smtp_pass'] = 'xxxxxxx'; if($_POST['type'] == 'html'){ $config['mailtype'] = 'html'; $config['charset'] = 'iso-8859-1'; } $this->load->library('email', $config);$this->email->from('[email protected]', 'iBacor'); $this->email->to($_POST['to']); if(!empty($_POST['cc'])){ $this->email->cc($_POST['cc']); } if(!empty($_POST['bcc'])){ $this->email->bcc($_POST['bcc']); } $this->email->subject($_POST['subject']); $this->email->message($_POST['compose']); if($_POST['type'] == 'html'){ $this->email->set_mailtype("html"); }
if($this->email->send()) { $result['status'] = 'success'; } else { $result['status'] = 'error'; }
# config SMTP require 'PHPMailerAutoload.php'; $mail = new PHPMailer; $mail->isSMTP(); $mail->Host = 'smtp1.example.com;smtp2.example.com'; $mail->SMTPAuth = true; $mail->Username = '[email protected]'; $mail->Password = 'secret'; $mail->SMTPSecure = 'tls'; $mail->Port = 587;- bootstrap - fontawesome - select2 - tinymce$mail->setFrom('[email protected]', 'iBacor'); $mail->addReplyTo($_POST['to']); if(!empty($_POST['cc'])){ $mail->addCC($_POST['cc']); } if(!empty($_POST['bcc'])){ $mail->addBCC($_POST['bcc']); } if($_POST['type'] == 'html'){ $mail->isHTML(true);
} $mail->Subject = $_POST['subject']; if($_POST['type'] == 'html'){ $mail->Body = $_POST['compose']; }else{ $mail->AltBody = $_POST['compose']; }if(!$mail->send()) { $result['status'] = 'success'; } else { $result['status'] = 'error'; }