public function send() { $status = 'Failed to send mail. '; $from = $this->from(); $subject = $this->subject(); $attachments = $this->_attachments; $eol = $this->config('eol'); $mime_boundary = md5(time()); //Build Headers $this->headers = array(); if ($this->_attachments) { $this->headers['MIME-Version'] = "1.0"; $this->headers['Content-Type'] = "multipart/mixed; boundary=\"mixed-{$mime_boundary}\""; } elseif ($this->sendHTML()) { $this->headers['MIME-Version'] = "1.0"; $this->headers['Content-Type'] = "multipart/related; boundary=\"mixed-{$mime_boundary}\""; } else { $this->headers['Content-Type'] = "text/plain; charset=iso-8859-1"; } if ($from != '' && self::validateAddress($this->from())) { $this->headers['From'] = "{$from}"; $this->headers['Reply-To'] = "{$from}"; $this->headers['Return-Path'] = "{$from}"; $this->headers['Message-ID'] = "<" . time() . "-{$from}>"; } $rcpts = $this->getRecipients(); $cc = $this->getRecipients(self::$CC); $bcc = $this->getRecipients(self::$BCC); if (count($cc) > 0) { $this->_headers["Cc"] = implode(', ', $cc); } if (count($bcc) > 0) { $this->_headers["Bcc"] = implode(', ', $bcc); } $this->headers['X-Mailer'] = "PHP/" . phpversion(); //Compile mail data foreach ($this->headers() as $a => $b) { $headers = "{$a}: {$b}"; } $header_info = implode($eol, $this->_headers); $message = $this->message(); $message = wordwrap($message, 70); $body = ""; if ($attachments) { foreach ($attachments as $file) { if (is_file($file["file"])) { if (file_exists($file["file"])) { $file_name = substr($file["file"], strrpos($file["file"], "/") + 1); $handle = fopen($file["file"], 'rb'); $f_contents = fread($handle, filesize($file["file"])); $f_contents = chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode(); fclose($handle); // Attach $body .= "--mixed-{$mime_boundary}{$eol}"; $body .= "Content-Type: {$file["type"]}; name=\"{$file_name}\"{$eol}"; $body .= "Content-Transfer-Encoding: base64{$eol}"; $body .= "Content-Disposition: attachment; filename=\"{$file_name}\"{$eol}{$eol}"; // !! This line needs TWO end of lines !! IMPORTANT !! $body .= $f_contents . $eol . $eol; } } } $body .= "--mixed-" . $mime_boundary . $eol; } // Begin message text if ($this->sendHTML() === true) { $body .= "Content-Type: multipart/alternative; boundary: \"alt-{$mime_boundary}\"{$eol}"; // HTML Text $body .= "--alt-" . $mime_boundary . $eol; $body .= "Content-Type: text/html; charset=iso-8859-1{$eol}"; $body .= "Content-Transfer-Encoding: 8bit{$eol}{$eol}"; $body .= $message . $eol . $eol; // Ready plain text headers $body .= "--alt-" . $mime_boundary . $eol; $body .= "Content-Type: text/plain; charset=iso-8859-1{$eol}"; $body .= "Content-Transfer-Encoding: 8bit{$eol}{$eol}"; } // Plain Text $body .= strip_tags(dev_br2nl($message)) . $eol . $eol; // Body end if ($this->sendHTML()) { $body .= "--alt-{$mime_boundary}--{$eol}{$eol}"; } if ($attachments) { $body .= "--mixed-{$mime_boundary}--{$eol}{$eol}"; } // finish with two eol's for better security. see Injection. // the INI lines are to force the From Address to be used ini_set("sendmail_from", $this->from()); if (count($rcpts) <= 0) { $status .= "The send to address is empty.\n"; } elseif (!self::validateAddress($this->getRecipients())) { $status .= "Email address '" . implode(', ', $this->rcpt) . "' is invalid.\n"; } elseif ($subject == '') { $status .= "Subject line is empty.\n"; } elseif ($message == '') { $status .= "Message is empty.\n"; } elseif (count($cc) > 0 && !self::validateAddress($cc)) { $status .= "Invalid address in the CC line\n"; } elseif (count($bcc) > 0 && !self::validateAddress($bcc)) { $status .= "Invalid address in the BCC line\n"; } elseif (mail(implode(', ', $this->getRecipients()), $this->subject(), $body, $header_info, $this->field('additional'))) { $status = "Mail delivered successfully\n"; } ini_restore("sendmail_from"); $this->status($status); }
function dev_send_email($rcpt = '', $from = '', $subject = '', $message = '', $cc = '', $bcc = '', $html = false, $headers_r = '', $additional = '', $attachments = '') { $status = 'Failed to send mail. '; //Prepare addresses $rcpt = dev_value_to_array($rcpt); $cc = dev_value_to_array($cc); $bcc = dev_value_to_array($bcc); $from = dev_sanitize_email($from); $subject = dev_sanitize_email($subject); $eol = "\r\n"; $mime_boundary = md5(time()); //Build Headers $headers = array(); if ($html) { $headers[] = "MIME-Version: 1.0"; $headers[] = "Content-Type: multipart/related; boundary=\"{$mime_boundary}\""; } /* if ($attachments ) { $headers[] = "MIME-Version: 1.0"; $headers[] = "Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\""; } */ if ($from != '' && dev_validate_email($from)) { $headers[] = "From: {$from}"; $headers[] = "Reply-To: {$from}"; $headers[] = "Return-Path: {$from}"; $headers[] = "Message-ID: <" . time() . "-{$from}>"; } if (count($cc) > 0) { $headers[] = "Cc: " . implode(', ', dev_use_valid_emails($cc)); } if (count($bcc) > 0) { $headers[] = "Bcc: " . implode(', ', dev_use_valid_emails($bcc)); } $headers = array_merge(dev_value_to_array($headers_r), $headers); $headers[] = "X-Mailer: PHP/" . phpversion(); //Compile mail data $header_info = implode($eol, $headers); $message = dev_sanitize_email($message); $message = wordwrap($message, 70); $body = ""; if ($attachments !== false) { $attachments = dev_value_to_array($attachments); foreach ($attachments as $file) { if (is_file($file["file"])) { if (file_exists($file["file"])) { $file_name = substr($file["file"], strrpos($file["file"], "/") + 1); $handle = fopen($file["file"], 'rb'); $f_contents = fread($handle, filesize($file["file"])); $f_contents = chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode(); fclose($handle); // Attach $body .= "--{$mime_boundary}{$eol}"; $body .= "Content-Type: {$file["type"]}; name=\"{$file_name}\"{$eol}"; $body .= "Content-Transfer-Encoding: base64{$eol}"; $body .= "Content-Disposition: attachment; filename=\"{$file_name}\"{$eol}{$eol}"; // !! This line needs TWO end of lines !! IMPORTANT !! $body .= $f_contents . $eol . $eol; } } } } // Begin message text if ($html === true) { // HTML Text $body .= "Content-Type: multipart/alternative{$eol}"; $body .= "--" . $mime_boundary . $eol; $body .= "Content-Type: text/html; charset=iso-8859-1{$eol}"; $body .= "Content-Transfer-Encoding: 8bit{$eol}{$eol}"; $body .= $message . $eol . $eol; } // Plain Text if ($html === true || !is_array($attachments)) { $body .= "--" . $mime_boundary . $eol; $body .= "Content-Type: text/plain; charset=iso-8859-1{$eol}"; $body .= "Content-Transfer-Encoding: 8bit{$eol}{$eol}"; } $body .= strip_tags(dev_br2nl($message)) . $eol . $eol; // Body end if ($html === true || !is_array($attachments)) { $body .= "--{$mime_boundary}--{$eol}{$eol}"; // finish with two eol's for better security. see Injection. } // the INI lines are to force the From Address to be used ini_set("sendmail_from", $from); if (count($rcpt) <= 0) { $status .= "The send to address is empty.\n"; } elseif (!dev_validate_email($rcpt)) { $status .= "Email address '" . implode(', ', $rcpt) . "' is invalid.\n"; } elseif ($subject == '') { $status .= "Subject line is empty.\n"; } elseif ($message == '') { $status .= "Message is empty.\n"; } elseif (count($cc) > 0 && !dev_validate_email($cc)) { $status .= "Invalid address in the CC line\n"; } elseif (count($bcc) > 0 && !dev_validate_email($bcc)) { $status .= "Invalid address in the BCC line\n"; } elseif (mail(implode(', ', dev_use_valid_emails($rcpt)), $subject, $body, $header_info, $additional)) { $status = "Mail delivered successfully\n"; } ini_restore("sendmail_from"); return $status; }