Opencart send email in custom Script

nunong21 picture nunong21 · Apr 16, 2013 · Viewed 14.8k times · Source

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.

Answer

Imtiaz picture Imtiaz · Nov 15, 2016

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.