Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencart send email in custom Script

I have a script in my opencart, made by myself, and want to make it send an email, but I think that when I try to get email parameters they return null.

Here is my code:

    $email_to = "[email protected]";
    $config = new Config();
    $mail = new Mail();


    $mail->protocol = $config->get('config_mail_protocol');
    $mail->parameter = $config->get('config_mail_parameter');
    $mail->hostname = $config->get('config_smtp_host');
    $mail->username = $config->get('config_smtp_username');
    $mail->password = $config->get('config_smtp_password');
    $mail->port = $config->get('config_smtp_port');
    $mail->timeout = $config->get('config_smtp_timeout');            
    $mail->setTo($email_to);
    $mail->setFrom("nuno@[mydomain].com");
    $mail->setSender("nuno@[mydomain].com");
    $mail->setSubject("test send mail");
    $mail->setText("test message body text");
    $mail->send();

When I try calling: echo $config->get('config_mail_protocol'); it returns null.

like image 429
nunong21 Avatar asked Apr 16 '13 15:04

nunong21


2 Answers

I've faced problems sending mail with the previously mentioned codes. Opencart mail variables have been changed since opencart 2.

This is the code for opencart 2.3

$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');
$mail->smtp_username = $this->config->get('config_mail_smtp_username');
$mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
$mail->smtp_port = $this->config->get('config_mail_smtp_port');
$mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');

$mail->setTo($order_info['email']);
$mail->setFrom($this->config->get('config_email'));
$mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
$mail->setHtml($this->load->view('mail/order', $data));
$mail->setText($text);
$mail->send();

Code block copied straight from catalog/model/checkout/order.php

I hope somebody will find this useful.

like image 152
Imtiaz Avatar answered Oct 21 '22 23:10

Imtiaz


Do not create new instances of Config but just simply call

$email_to = "[email protected]";
$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($email_to);
$mail->setFrom("[email protected]");
$mail->setSender("[email protected]");
$mail->setSubject("test send mail");
$mail->setText("test message body text");

$mail->send();
like image 43
shadyyx Avatar answered Oct 22 '22 00:10

shadyyx