简体   繁体   中英

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 = "somewhere@example.com";
    $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 .

Do not create new instances of Config but just simply call

$email_to = "somewhere@example.com";
$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("somewhere@example.com");
$mail->setSender("somewhere@example.com");
$mail->setSubject("test send mail");
$mail->setText("test message body text");

$mail->send();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM