I created a file in the cli folder name it sendemail.php with the following code inside:
<?php
/**
* @package Joomla.Cli
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
/**
* A command line cron job to attempt to remove files that should have been deleted at update.
*/
// We are a valid entry point.
const _JEXEC = 1;
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
// Get the framework.
require_once JPATH_LIBRARIES . '/import.legacy.php';
// Bootstrap the CMS libraries.
require_once JPATH_LIBRARIES . '/cms.php';
// Configure error reporting to maximum for CLI output.
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Load Library language
$lang = JFactory::getLanguage();
// Try the files_joomla file in the current language (without allowing the loading of the file in the default language)
$lang->load('files_joomla.sys', JPATH_SITE, null, false, false)
// Fallback to the files_joomla file in the default language
|| $lang->load('files_joomla.sys', JPATH_SITE, null, true);
// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';
/**
* A command line cron job to attempt to remove files that should have been deleted at update.
*
* @since 3.0
*/
class Sendemail extends JApplicationCli
{
/**
* Entry point for the script
*
* @return void
*
* @since 2.5
*/
public function doExecute()
{
$config = JFactory::getConfig();
$mailer = JFactory::getMailer();
$sender = array(
$config->get('mailfrom'),
$config->get('fromname')
);
$mailer->SMTPDebug = true;
$recipient = 'myemail@domain.com';
$body = 'Message Text for test';
$mailer->addRecipient($recipient);
$mailer->setSubject("TestEmail");
$mailer->isHtml(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
$send = $mailer->Send();
if ( $send !== true ) {
echo 'Error sending email: ';
} else {
echo 'Email sent';
}
die;
}
}
JApplicationCli::getInstance('Sendemail')->execute();
but after I run php php sendemail.php I get as return :
Error sending email
When I click send test email on administrator > configuration > server; The test email sent and I get
The email was sent to sendemail@domain.com using SMTP. You should check that you've received the test email.
So the problem is not related to username or password or host of the smtp, but when I try to send email from cli file I get Error sending email and no email is sent.