First the mail notifications wont work in OpenCart 2.0.2.0. I tried using gmail smtp and tried login registering, this is the error message. I got:
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/spicycom/public_html/system/library/mail.php on line 168Warning: fsockopen() [function.fsockopen]: unable to connect to :465 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/spicycom/public_html/system/library/mail.php on line 168Notice: Error: php_network_getaddresses: getaddrinfo failed: Name or service not known (0) in /home/spicycom/public_html/system/library/mail.php on line 171
Please how do I fix this problem.
This is the mail code snipet
<?php
class Mail {
protected $to;
protected $from;
protected $sender;
protected $replyto;
protected $subject;
protected $text;
protected $html;
protected $attachments = array();
public $protocol = 'mail';
public $smtp_hostname;
public $smtp_username;
public $smtp_password;
public $smtp_port = 25;
public $smtp_timeout = 5;
public $newline = "\n";
public $verp = false;
public $parameter = '';
public function __construct($config = array()) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
}
public function setTo($to) {
$this->to = $to;
}
public function setFrom($from) {
$this->from = $from;
}
public function setSender($sender) {
$this->sender = $sender;
}
public function setReplyTo($reply_to) {
$this->replyto = $reply_to;
}
public function setSubject($subject) {
$this->subject = $subject;
}
public function setText($text) {
$this->text = $text;
}
public function setHtml($html) {
$this->html = $html;
}
public function addAttachment($filename) {
$this->attachments[] = $filename;
}
public function send() {
if (!$this->to) {
trigger_error('Error: E-Mail to required!');
exit();
}
if (!$this->from) {
trigger_error('Error: E-Mail from required!');
exit();
}
if (!$this->sender) {
trigger_error('Error: E-Mail sender required!');
exit();
}
if (!$this->subject) {
trigger_error('Error: E-Mail subject required!');
exit();
}
if ((!$this->text) && (!$this->html)) {
trigger_error('Error: E-Mail message required!');
exit();
}
if (!$this->replyto) {
$this->setReplyTo($this->sender);
}
if (is_array($this->to)) {
$to = implode(',', $this->to);
} else {
$to = $this->to;
}
$boundary = '----=_NextPart_' . md5(time());
$header = 'MIME-Version: 1.0' . $this->newline;
if ($this->protocol != 'mail') {
$header .= 'To: ' . $to . $this->newline;
$header .= 'Subject: ' . '=?UTF-8?B?' . base64_encode($this- > subject) . '?=' . $this->newline;
}
$header .= 'Date: ' . date('D, d M Y H:i:s O') . $this->newline;
$header .= 'From: =?UTF-8?B?' . base64_encode($this->sender) . '?=' . ' <' . $this->from . '>' . $this->newline;
$header .= 'Reply-To: =?UTF-8?B?' . base64_encode($this->replyto) . '?=' . ' <' . $this->from . '>' . $this->newline;
$header .= 'Return-Path: ' . $this->from . $this->newline;
$header .= 'X-Mailer: PHP/' . phpversion() . $this->newline;
$header .= 'Content-Type: multipart/related; boundary="' . $boundary . '"' . $this->newline . $this->newline;
if (!$this->html) {
$message = '--' . $boundary . $this->newline;
$message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
$message .= $this->text . $this->newline;
} else {
$message = '--' . $boundary . $this->newline;
$message .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '_alt"' . $this->newline . $this->newline;
$message .= '--' . $boundary . '_alt' . $this->newline;
$message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
if ($this->text) {
$message .= $this->text . $this->newline;
} else {
$message .= 'This is a HTML email and your email client software does not support HTML email!' . $this->newline;
}
$message .= '--' . $boundary . '_alt' . $this->newline;
$message .= 'Content-Type: text/html; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
$message .= $this->html . $this->newline;
$message .= '--' . $boundary . '_alt--' . $this->newline;
}
foreach ($this->attachments as $attachment) {
if (file_exists($attachment)) {
$handle = fopen($attachment, 'r');
$content = fread($handle, filesize($attachment));
fclose($handle);
$message .= '--' . $boundary . $this->newline;
$message .= 'Content-Type: application/octet-stream; name="' . basename($attachment) . '"' . $this->newline;
$message .= 'Content-Transfer-Encoding: base64' . $this->newline;
$message .= 'Content-Disposition: attachment; filename="' . basename($attachment) . '"' . $this->newline;
$message .= 'Content-ID: <' . basename(urlencode($attachment)) . '>' . $this->newline;
$message .= 'X-Attachment-Id: ' . basename(urlencode($attachment)) . $this->newline . $this->newline;
$message .= chunk_split(base64_encode($content));
}
}
$message .= '--' . $boundary . '--' . $this->newline;
if ($this->protocol == 'mail') {
ini_set('sendmail_from', $this->from);
if ($this->parameter) {
mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header, $this->parameter);
} else {
mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header);
}
} elseif ($this->protocol == 'smtp') {
$tls = substr($this->smtp_hostname, 0, 3) == 'tls';
$hostname = $tls ? substr($this->smtp_hostname, 6) : $this->smtp_hostname;
$handle = fsockopen($hostname, $this->smtp_port, $errno, $errstr, $this->smtp_timeout);
if (!$handle) {
trigger_error('Error: ' . $errstr . ' (' . $errno . ')');
exit();
} else {
if (substr(PHP_OS, 0, 3) != 'WIN') {
socket_set_timeout($handle, $this->smtp_timeout, 0);
}
while ($line = fgets($handle, 515)) {
if (substr($line, 3, 1) == ' ') {
break;
}
}
fputs($handle, 'EHLO ' . getenv('SERVER_NAME') . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: EHLO not accepted from server!');
exit();
}
if ($tls) {
fputs($handle, 'STARTTLS' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 220) {
trigger_error('Error: STARTTLS not accepted from server!');
exit();
}
stream_socket_enable_crypto($handle, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
}
if (!empty($this->smtp_username) && !empty($this->smtp_password)) {
fputs($handle, 'EHLO ' . getenv('SERVER_NAME') . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: EHLO not accepted from server!');
exit();
}
fputs($handle, 'AUTH LOGIN' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 334) {
trigger_error('Error: AUTH LOGIN not accepted from server!');
exit();
}
fputs($handle, base64_encode($this->smtp_username) . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 334) {
trigger_error('Error: Username not accepted from server!');
exit();
}
fputs($handle, base64_encode($this->smtp_password) . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 235) {
trigger_error('Error: Password not accepted from server!');
exit();
}
} else {
fputs($handle, 'HELO ' . getenv('SERVER_NAME') . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: HELO not accepted from server!');
exit();
}
}
if ($this->verp) {
fputs($handle, 'MAIL FROM: <' . $this->from . '>XVERP' . "\r\n");
} else {
fputs($handle, 'MAIL FROM: <' . $this->from . '>' . "\r\n");
}
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: MAIL FROM not accepted from server!');
exit();
}
if (!is_array($this->to)) {
fputs($handle, 'RCPT TO: <' . $this->to . '>' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if ((substr($reply, 0, 3) != 250) && (substr($reply, 0, 3) != 251)) {
trigger_error('Error: RCPT TO not accepted from server!');
exit();
}
} else {
foreach ($this->to as $recipient) {
fputs($handle, 'RCPT TO: <' . $recipient . '>' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if ((substr($reply, 0, 3) != 250) && (substr($reply, 0, 3) != 251)) {
trigger_error('Error: RCPT TO not accepted from server!');
exit();
}
}
}
fputs($handle, 'DATA' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 354) {
trigger_error('Error: DATA not accepted from server!');
exit();
}
// According to rfc 821 we should not send more than 1000 including the CRLF
$message = str_replace("\r\n", "\n", $header . $message);
$message = str_replace("\r", "\n", $message);
$lines = explode("\n", $message);
foreach ($lines as $line) {
$results = str_split($line, 998);
foreach ($results as $result) {
if (substr(PHP_OS, 0, 3) != 'WIN') {
fputs($handle, $result . "\r\n");
} else {
fputs($handle, str_replace("\n", "\r\n", $result) . "\r\n");
}
}
}
fputs($handle, '.' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: DATA not accepted from server!');
exit();
}
fputs($handle, 'QUIT' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 221) {
trigger_error('Error: QUIT not accepted from server!');
exit();
}
fclose($handle);
}
}
}
}`
Related
How to remove special character from email using codeignitor
Email is adding some special character don't know why and how this is going using codeignitor how to solve this this is my Email.php system file please guide
protected function _build_message()
{
if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
{
$this->_body = $this->word_wrap($this->_body);
}
$this->_set_boundaries();
$this->_write_headers();
$hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
$body = '';
switch ($this->_get_content_type())
{
case 'plain' :
$hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: '.$this->_get_encoding();
if ($this->_get_protocol() === 'mail')
{
$this->_header_str .= $hdr;
$this->_finalbody = $this->_body;
}
else
{
$this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
}
return;
case 'html' :
if ($this->send_multipart === FALSE)
{
$hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: quoted-printable';
}
else
{
$hdr .= 'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"';
$body .= $this->_get_mime_message().$this->newline.$this->newline
.'--'.$this->_alt_boundary.$this->newline
.'Content-Type: text/plain; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
.$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
.'Content-Type: text/html; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
}
$this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
if ($this->_get_protocol() === 'mail')
{
$this->_header_str .= $hdr;
}
else
{
$this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
}
if ($this->send_multipart !== FALSE)
{
$this->_finalbody .= '--'.$this->_alt_boundary.'--';
}
return;
case 'plain-attach' :
$hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
if ($this->_get_protocol() === 'mail')
{
$this->_header_str .= $hdr;
}
$body .= $this->_get_mime_message().$this->newline
.$this->newline
.'--'.$this->_atc_boundary.$this->newline
.'Content-Type: text/plain; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
.$this->newline
.$this->_body.$this->newline.$this->newline;
break;
case 'html-attach' :
$hdr .= 'Content-Type: multipart/'.$this->multipart.'; boundary="'.$this->_atc_boundary.'"';
if ($this->_get_protocol() === 'mail')
{
$this->_header_str .= $hdr;
}
$body .= $this->_get_mime_message().$this->newline.$this->newline
.'--'.$this->_atc_boundary.$this->newline
.'Content-Type: multipart/alternative; boundary="'.$this->_alt_boundary.'"'.$this->newline.$this->newline
.'--'.$this->_alt_boundary.$this->newline
.'Content-Type: text/plain; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
.$this->_get_alt_message().$this->newline.$this->newline.'--'.$this->_alt_boundary.$this->newline
.'Content-Type: text/html; charset='.$this->charset.$this->newline
.'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
.'--'.$this->_alt_boundary.'--'.$this->newline.$this->newline;
break;
}
$attachment = array();
for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
{
$filename = $this->_attachments[$i]['name'][0];
$basename = ($this->_attachments[$i]['name'][1] === NULL)
? basename($filename) : $this->_attachments[$i]['name'][1];
$attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
.'Content-type: '.$this->_attachments[$i]['type'].'; '
.'name="'.$basename.'"'.$this->newline
.'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
.'Content-Transfer-Encoding: base64'.$this->newline
.(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline);
$attachment[$z++] = $this->_attachments[$i]['content'];
}
$body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
$this->_finalbody = ($this->_get_protocol() === 'mail')
? $body
: $hdr.$this->newline.$this->newline.$body;
return TRUE;
}
What would be right approach to fix it please guide thanks in advance
one more thing i would like to tell i'm using smtp_crypto => tls that causing issue
You could fix this issue by replacing instances of _prep_quoted_printable in the Email class with the PHP native function quoted_printable_encode.
Just replace inside _build_message()
How this help you !
We have a php script that emails form field values when a user submits the form. The form action points to the script below.
We've been asked to configure things to use expressmail explicitly. My question is, would this entail a modification to the script or is this a config setting on the server somewhere?
<?php
if (! $_POST) {
header('HTTP/1.0 405 Method Not Allowed');
exit;
}
$redirectTo = html_entity_decode($_POST['post']);
$body = '<html><body>';
$content = array();
foreach ($_POST as $key => $value) {
if ('-label' !== substr($key, -6)) {
continue;
}
$field = substr($key, 0, strlen($key) - 6);
$content[$field]['value'] = $_POST[$field];
$content[$field]['label'] = $_POST[$key];
}
$body .= '<h1>' . htmlentities($_POST['formName']) . '</h1>';
foreach ($content as $field => $value) {
$data = $value['value'];
$label = $value['label'];
$body .= '<p><b>' . htmlentities($label) . '</b><br />';
if (false === is_array($data) && (null === $data OR "" === trim($data))) {
$body .= 'N/A';
} elseif (is_array($data)) {
$body .= '<ul>';
foreach ($data as $val) {
$val = htmlentities($val);
$body .= '<li>' . $val . '</li>';
}
$body .= '</ul>';
} else {
$body .= htmlentities($data);
}
$body .= '</p>';
}
$body .= '</body></html>';
$to = strip_tags($_POST['emailTo']);
$subject = strip_tags($_POST['emailSubject']);
$headers = "From: " . strip_tags($_POST['emailFrom']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['emailFrom']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $body, $headers);
header('Location: ' . $redirectTo);
?>
I am trying to receive a loop based generated HTML Table in email but seems like I am getting email but no CSS embedded with it as I am passing the BootStrap library CSS files with the email but it's not getting any CSS at all..So I am wondering that what would be the problem...??
Here is screenshot as :
Here is my whole code as :
<?php
$message .= '<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">';
$message .= '<table class="table table-bordered">';
$message .= '<thead>';
$message .= '<tr>';
$message .= '<th>#</th>';
$message .= '<th>Username</th>';
$message .= '<th>Session From</th>';
$message .= '<th>Session Till</th>';
$message .= '<th>Uptime</th>';
$message .= '<th>Download</th>';
$message .= '<th>Upload</th>';
$message .= '<th>Total Usage</th>';
$message .= '</tr>';
$message .= '</thead>';
$message .= '<tbody>';
function human_filesize($bytes, $decimals = 2) {
$factor = floor((strlen($bytes) - 1) / 3);
if ($factor > 0) $sz = 'KMGT';
return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . #$sz[$factor - 1] . 'B';
}
if (isset($_GET)) {
$user = $_GET["user"];
}
$x = 1;
$handle = fopen($user, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$split_data = (explode(" ",$line));
if (in_array('customer=admin', $split_data)) {
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'user=') !== false) {
$username = explode("=", $split_data[$i]);
$username = $username[1];
}
}
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'from-time=') !== false) {
$from_time = explode("=", $split_data[$i]);
$from_time = $from_time[1];
$from_time = $from_time." ".$split_data[$i+1];
}
}
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'till-time=') !== false) {
$till_time = explode("=", $split_data[$i]);
$till_time = $till_time[1];
$till_time = $till_time." ".$split_data[$i+1];
}
}
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'uptime=') !== false) {
$uptime = explode("=", $split_data[$i]);
$uptime = $uptime[1];
$download = explode("=", $split_data[$i+1]);
$download = $download[1];
$upload = explode("=", $split_data[$i+2]);
$upload = $upload[1];
#$total_download += $download;
#$total_upload += $upload;
$total_usage = $total_download+$total_upload;
}
}
$message .= '<tr>';
$message .= '<th scope="row">'.$x.'</th>';
$message .= '<td>'.$username.'</td>';
$message .= '<td>'.$from_time.'</td>';
$message .= '<td>'.$till_time.'</td>';
$message .= '<td>'.$uptime.'</td>';
$message .= '<td>'.human_filesize($download,2).'</td>';
$message .= '<td>'.human_filesize($upload,2).'</td>';
$message .= '<td>'.human_filesize($total_usage,2).'</td>';
$message .= '</tr>';
$x=$x+1;
}
}
fclose($handle);
}
$message .= '</tbody>';
$message .= '</table>';
$to = 'nicefellow1234#gmail.com';
$subject = 'Website Change Reqest';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers);
?>
As of September 2016, Gmail accepts embedded styles – CSS within <style> tags in the head section of HTML documents.
This is in addition to inline styles, which were previously the only way to apply CSS in Gmail.
At the same time, Google says nothing about support for external styles, which is likely why your Bootstrap styles are failing to load.
https://developers.google.com/gmail/design/
My opencart does not input into the order confirmation email subject. The email comes with no subject. Any idea why that happens?
I cannot figure out, what could be the problem. Any ideas are appreciated.
Thank you in advance
<?php
class Mail {
protected $to;
protected $from;
protected $sender;
protected $subject;
protected $text;
protected $html;
protected $attachments = array();
public $protocol = 'mail';
public $hostname;
public $username;
public $password;
public $port = 25;
public $timeout = 5;
public $newline = "\n";
public $crlf = "\r\n";
public $verp = false;
public $parameter = '';
public function setTo($to) {
$this->to = $to;
}
public function setFrom($from) {
$this->from = $from;
}
public function setSender($sender) {
$this->sender = $sender;
}
public function setSubject($subject) {
$this->subject = $subject;
}
public function setText($text) {
$this->text = $text;
}
public function setHtml($html) {
$this->html = $html;
}
public function addAttachment($filename) {
$this->attachments[] = $filename;
}
public function send() {
if (!$this->to) {
trigger_error('Error: E-Mail to required!');
exit();
}
if (!$this->from) {
trigger_error('Error: E-Mail from required!');
exit();
}
if (!$this->sender) {
trigger_error('Error: E-Mail sender required!');
exit();
}
if (!$this->subject) {
trigger_error('Error: E-Mail subject required!');
exit();
}
if ((!$this->text) && (!$this->html)) {
trigger_error('Error: E-Mail message required!');
exit();
}
if (is_array($this->to)) {
$to = implode(',', $this->to);
} else {
$to = $this->to;
}
$boundary = '----=_NextPart_' . md5(time());
$header = '';
$header .= 'MIME-Version: 1.0' . $this->newline;
if ($this->protocol != 'mail') {
$header .= 'To: ' . $to . $this->newline;
$header .= 'Subject: ' . '=?UTF-8?B?' . base64_encode($this->subject) . '?=' . $this->newline;
}
$header .= 'Date: ' . date('D, d M Y H:i:s O') . $this->newline;
$header = 'From: ' . '=?UTF-8?B?' . base64_encode($this->sender) . '?=' . ' <' . $this->from . '>' . $this->newline;
$header .= 'Reply-To: ' . '=?UTF-8?B?' . base64_encode($this->sender) . '?=' . ' <' . $this->from . '>' . $this->newline;
$header .= 'Return-Path: ' . $this->from . $this->newline;
$header .= 'X-Mailer: PHP/' . phpversion() . $this->newline;
$header .= 'Content-Type: multipart/related; boundary="' . $boundary . '"' . $this->newline . $this->newline;
if (!$this->html) {
$message = '--' . $boundary . $this->newline;
$message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
$message .= $this->text . $this->newline;
} else {
$message = '--' . $boundary . $this->newline;
$message .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '_alt"' . $this->newline . $this->newline;
$message .= '--' . $boundary . '_alt' . $this->newline;
$message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
if ($this->text) {
$message .= $this->text . $this->newline;
} else {
$message .= 'This is a HTML email and your email client software does not support HTML email!' . $this->newline;
}
$message .= '--' . $boundary . '_alt' . $this->newline;
$message .= 'Content-Type: text/html; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
$message .= $this->html . $this->newline;
$message .= '--' . $boundary . '_alt--' . $this->newline;
}
foreach ($this->attachments as $attachment) {
if (file_exists($attachment)) {
$handle = fopen($attachment, 'r');
$content = fread($handle, filesize($attachment));
fclose($handle);
$message .= '--' . $boundary . $this->newline;
$message .= 'Content-Type: application/octet-stream; name="' . basename($attachment) . '"' . $this->newline;
$message .= 'Content-Transfer-Encoding: base64' . $this->newline;
$message .= 'Content-Disposition: attachment; filename="' . basename($attachment) . '"' . $this->newline;
$message .= 'Content-ID: <' . basename(urlencode($attachment)) . '>' . $this->newline;
$message .= 'X-Attachment-Id: ' . basename(urlencode($attachment)) . $this->newline . $this->newline;
$message .= chunk_split(base64_encode($content));
}
}
$message .= '--' . $boundary . '--' . $this->newline;
if ($this->protocol == 'mail') {
ini_set('sendmail_from', $this->from);
if ($this->parameter) {
mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header, $this->parameter);
} else {
mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header);
}
} elseif ($this->protocol == 'smtp') {
$handle = fsockopen($this->hostname, $this->port, $errno, $errstr, $this->timeout);
if (!$handle) {
trigger_error('Error: ' . $errstr . ' (' . $errno . ')');
exit();
} else {
if (substr(PHP_OS, 0, 3) != 'WIN') {
socket_set_timeout($handle, $this->timeout, 0);
}
while ($line = fgets($handle, 515)) {
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($this->hostname, 0, 3) == 'tls') {
fputs($handle, 'STARTTLS' . $this->crlf);
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 220) {
trigger_error('Error: STARTTLS not accepted from server!');
exit();
}
}
if (!empty($this->username) && !empty($this->password)) {
fputs($handle, 'EHLO ' . getenv('SERVER_NAME') . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: EHLO not accepted from server!');
exit();
}
fputs($handle, 'AUTH LOGIN' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 334) {
trigger_error('Error: AUTH LOGIN not accepted from server!');
exit();
}
fputs($handle, base64_encode($this->username) . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 334) {
trigger_error('Error: Username not accepted from server!');
exit();
}
fputs($handle, base64_encode($this->password) . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 235) {
trigger_error('Error: Password not accepted from server!');
exit();
}
} else {
fputs($handle, 'HELO ' . getenv('SERVER_NAME') . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: HELO not accepted from server!');
exit();
}
}
if ($this->verp) {
fputs($handle, 'MAIL FROM: <' . $this->from . '>XVERP' . $this->crlf);
} else {
fputs($handle, 'MAIL FROM: <' . $this->from . '>' . $this->crlf);
}
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: MAIL FROM not accepted from server!');
exit();
}
if (!is_array($this->to)) {
fputs($handle, 'RCPT TO: <' . $this->to . '>' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if ((substr($reply, 0, 3) != 250) && (substr($reply, 0, 3) != 251)) {
trigger_error('Error: RCPT TO not accepted from server!');
exit();
}
} else {
foreach ($this->to as $recipient) {
fputs($handle, 'RCPT TO: <' . $recipient . '>' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if ((substr($reply, 0, 3) != 250) && (substr($reply, 0, 3) != 251)) {
trigger_error('Error: RCPT TO not accepted from server!');
exit();
}
}
}
fputs($handle, 'DATA' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 354) {
trigger_error('Error: DATA not accepted from server!');
exit();
}
// According to rfc 821 we should not send more than 1000 including the CRLF
$message = str_replace("\r\n", "\n", $header . $message);
$message = str_replace("\r", "\n", $message);
$lines = explode("\n", $message);
foreach ($lines as $line) {
$results = str_split($line, 998);
foreach ($results as $result) {
if (substr(PHP_OS, 0, 3) != 'WIN') {
fputs($handle, $result . $this->crlf);
} else {
fputs($handle, str_replace("\n", "\r\n", $result) . $this->crlf);
}
}
}
fputs($handle, '.' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: DATA not accepted from server!');
exit();
}
fputs($handle, 'QUIT' . $this->crlf);
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 221) {
trigger_error('Error: QUIT not accepted from server!');
exit();
}
fclose($handle);
}
}
}
}
?>
when you right the code for mail in the model section will you use this??
$mail = new Mail();
$mail->protocol = $this->config->get('config_mail_protocol');
$mail->parameter = $this->config->get('config_mail_parameter');
$mail->hostname = $this->config->get('config_smtp_host');
$mail->username = $this->config->get('config_smtp_username');
$mail->password = $this->config->get('config_smtp_password');
$mail->port = $this->config->get('config_smtp_port');
$mail->timeout = $this->config->get('config_smtp_timeout');
$mail->setTo($order_info['email']);
$mail->setFrom($this->config->get('config_email'));
$mail->setSender($order_info['store_name']);
$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
$mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
$mail->send();
The mail is generated from model/checkout/order in function addOrderHistory(). You might want to see from there. This way you could solve the issue.
I have recently updated OpenCart 1.5.6.4 to OpenCart 2.0.2.0.
The day I updated, Mail stopped working and I'm unable to register any user or retrieve password, even though my mail settings have not changed.
Also, I can't send any email.
Following are the mail settings I am using in opencart 2.0.2.0 :
Mail Protocol : SMTP
SMTP Hostname : mail.mydomain.com
SMTP Username : emailid#mydomain.com
SMTP Password : myPassword
SMTP Port : 25
SMTP Timeout : 10
Every time I try to register user or retrieve password, I get following error:
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/xxxxx/public_html/system/library/mail.php on line 168Warning: fsockopen(): unable to connect to :25 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/xxxxxx/public_html/system/library/mail.php on line 168Notice: Error: php_network_getaddresses: getaddrinfo failed: Name or service not known (0) in /home/xxxxxx/public_html/system/library/mail.php on line 171
The code for /system/library/mail.php is below:
<?php
class Mail {
protected $to;
protected $from;
protected $sender;
protected $replyto;
protected $subject;
protected $text;
protected $html;
protected $attachments = array();
public $protocol = 'mail';
public $smtp_hostname;
public $smtp_username;
public $smtp_password;
public $smtp_port = 25;
public $smtp_timeout = 5;
public $newline = "\n";
public $verp = false;
public $parameter = '';
public function __construct($config = array()) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
}
public function setTo($to) {
$this->to = $to;
}
public function setFrom($from) {
$this->from = $from;
}
public function setSender($sender) {
$this->sender = $sender;
}
public function setReplyTo($reply_to) {
$this->replyto = $reply_to;
}
public function setSubject($subject) {
$this->subject = $subject;
}
public function setText($text) {
$this->text = $text;
}
public function setHtml($html) {
$this->html = $html;
}
public function addAttachment($filename) {
$this->attachments[] = $filename;
}
public function send() {
if (!$this->to) {
trigger_error('Error: E-Mail to required!');
exit();
}
if (!$this->from) {
trigger_error('Error: E-Mail from required!');
exit();
}
if (!$this->sender) {
trigger_error('Error: E-Mail sender required!');
exit();
}
if (!$this->subject) {
trigger_error('Error: E-Mail subject required!');
exit();
}
if ((!$this->text) && (!$this->html)) {
trigger_error('Error: E-Mail message required!');
exit();
}
if (!$this->replyto) {
$this->setReplyTo($this->sender);
}
if (is_array($this->to)) {
$to = implode(',', $this->to);
} else {
$to = $this->to;
}
$boundary = '----=_NextPart_' . md5(time());
$header = 'MIME-Version: 1.0' . $this->newline;
if ($this->protocol != 'mail') {
$header .= 'To: ' . $to . $this->newline;
$header .= 'Subject: ' . $this->subject . $this->newline;
}
$header .= 'Date: ' . date('D, d M Y H:i:s O') . $this->newline;
$header .= 'From: ' . $this->sender . ' <' . $this->from . '>' . $this->newline;
$header .= 'Reply-To: ' . $this->replyto . ' <' . $this->from . '>' . $this->newline;
$header .= 'Return-Path: ' . $this->from . $this->newline;
$header .= 'X-Mailer: PHP/' . phpversion() . $this->newline;
$header .= 'Content-Type: multipart/related; boundary="' . $boundary . '"' . $this->newline . $this->newline;
if (!$this->html) {
$message = '--' . $boundary . $this->newline;
$message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
$message .= $this->text . $this->newline;
} else {
$message = '--' . $boundary . $this->newline;
$message .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '_alt"' . $this->newline . $this->newline;
$message .= '--' . $boundary . '_alt' . $this->newline;
$message .= 'Content-Type: text/plain; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
if ($this->text) {
$message .= $this->text . $this->newline;
} else {
$message .= 'This is a HTML email and your email client software does not support HTML email!' . $this->newline;
}
$message .= '--' . $boundary . '_alt' . $this->newline;
$message .= 'Content-Type: text/html; charset="utf-8"' . $this->newline;
$message .= 'Content-Transfer-Encoding: 8bit' . $this->newline . $this->newline;
$message .= $this->html . $this->newline;
$message .= '--' . $boundary . '_alt--' . $this->newline;
}
foreach ($this->attachments as $attachment) {
if (file_exists($attachment)) {
$handle = fopen($attachment, 'r');
$content = fread($handle, filesize($attachment));
fclose($handle);
$message .= '--' . $boundary . $this->newline;
$message .= 'Content-Type: application/octet-stream; name="' . basename($attachment) . '"' . $this->newline;
$message .= 'Content-Transfer-Encoding: base64' . $this->newline;
$message .= 'Content-Disposition: attachment; filename="' . basename($attachment) . '"' . $this->newline;
$message .= 'Content-ID: <' . basename(urlencode($attachment)) . '>' . $this->newline;
$message .= 'X-Attachment-Id: ' . basename(urlencode($attachment)) . $this->newline . $this->newline;
$message .= chunk_split(base64_encode($content));
}
}
$message .= '--' . $boundary . '--' . $this->newline;
if ($this->protocol == 'mail') {
ini_set('sendmail_from', $this->from);
if ($this->parameter) {
mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header, $this->parameter);
} else {
mail($to, '=?UTF-8?B?' . base64_encode($this->subject) . '?=', $message, $header);
}
} elseif ($this->protocol == 'smtp') {
$tls = substr($this->smtp_hostname, 0, 3) == 'tls';
$hostname = $tls ? substr($this->smtp_hostname, 6) : $this->smtp_hostname;
$handle = fsockopen($hostname, $this->smtp_port, $errno, $errstr, $this->smtp_timeout);
if (!$handle) {
trigger_error('Error: ' . $errstr . ' (' . $errno . ')');
exit();
} else {
if (substr(PHP_OS, 0, 3) != 'WIN') {
socket_set_timeout($handle, $this->smtp_timeout, 0);
}
while ($line = fgets($handle, 515)) {
if (substr($line, 3, 1) == ' ') {
break;
}
}
fputs($handle, 'EHLO ' . getenv('SERVER_NAME') . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: EHLO not accepted from server!');
exit();
}
if ($tls) {
fputs($handle, 'STARTTLS' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 220) {
trigger_error('Error: STARTTLS not accepted from server!');
exit();
}
stream_socket_enable_crypto($handle, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
}
if (!empty($this->smtp_username) && !empty($this->smtp_password)) {
fputs($handle, 'EHLO ' . getenv('SERVER_NAME') . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: EHLO not accepted from server!');
exit();
}
fputs($handle, 'AUTH LOGIN' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 334) {
trigger_error('Error: AUTH LOGIN not accepted from server!');
exit();
}
fputs($handle, base64_encode($this->smtp_username) . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 334) {
trigger_error('Error: Username not accepted from server!');
exit();
}
fputs($handle, base64_encode($this->smtp_password) . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 235) {
trigger_error('Error: Password not accepted from server!');
exit();
}
} else {
fputs($handle, 'HELO ' . getenv('SERVER_NAME') . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: HELO not accepted from server!');
exit();
}
}
if ($this->verp) {
fputs($handle, 'MAIL FROM: <' . $this->from . '>XVERP' . "\r\n");
} else {
fputs($handle, 'MAIL FROM: <' . $this->from . '>' . "\r\n");
}
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: MAIL FROM not accepted from server!');
exit();
}
if (!is_array($this->to)) {
fputs($handle, 'RCPT TO: <' . $this->to . '>' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if ((substr($reply, 0, 3) != 250) && (substr($reply, 0, 3) != 251)) {
trigger_error('Error: RCPT TO not accepted from server!');
exit();
}
} else {
foreach ($this->to as $recipient) {
fputs($handle, 'RCPT TO: <' . $recipient . '>' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if ((substr($reply, 0, 3) != 250) && (substr($reply, 0, 3) != 251)) {
trigger_error('Error: RCPT TO not accepted from server!');
exit();
}
}
}
fputs($handle, 'DATA' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 354) {
trigger_error('Error: DATA not accepted from server!');
exit();
}
// According to rfc 821 we should not send more than 1000 including the CRLF
$message = str_replace("\r\n", "\n", $header . $message);
$message = str_replace("\r", "\n", $message);
$lines = explode("\n", $message);
foreach ($lines as $line) {
$results = str_split($line, 998);
foreach ($results as $result) {
if (substr(PHP_OS, 0, 3) != 'WIN') {
fputs($handle, $result . "\r\n");
} else {
fputs($handle, str_replace("\n", "\r\n", $result) . "\r\n");
}
}
}
fputs($handle, '.' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 250) {
trigger_error('Error: DATA not accepted from server!');
exit();
}
fputs($handle, 'QUIT' . "\r\n");
$reply = '';
while ($line = fgets($handle, 515)) {
$reply .= $line;
if (substr($line, 3, 1) == ' ') {
break;
}
}
if (substr($reply, 0, 3) != 221) {
trigger_error('Error: QUIT not accepted from server!');
exit();
}
fclose($handle);
}
}
}
}
How can I resolve it?
Opencart 2.0.2.0 has bug with smtp
http://www.opencart.com/index.php?route=extension/extension/info&extension_id=22107&filter_search=smtp
this is a fix. If you configure right SMTP, it should work fine.
PROBLEM>
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$mail = new Mail();
$mail->protocol = $this->config->get('config_mail_protocol');
$mail->parameter = $this->config->get('config_mail_parameter');
$mail->smtp_hostname = $this->config->get('config_mail_smtp_host');
FIX=>
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$mail = new Mail();
$mail->protocol = $this->config->get('config_mail_protocol');
$mail->parameter = $this->config->get('config_mail_parameter');
$mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
Look like DNS issue, it can't lookup email address correctly. Anyway, try settings that works for my OpenCart 2.0.2.0 site.
My email is myemail#mydomain.com
Mail Protocal: Mail
Mail Parameters: -Fmyemail#mydomain.com
SMTP Hostname: mydomain.com
SMTP Username: myemail#mydomain.com
SMTP Password: myemailpassword
SMTP Port: 25
SMTP Timeout: 5
Remember there is a "-F" in front of my email in Mail Parameters