nunong21 nunong21 - 17 days ago 16
PHP Question

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
.

Answer

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();
Comments