0

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.

  • 1
    I downloaded your code and tried it, and it worked ok for me on my Joomla 3.9.15 instance on Windows 10. The only thing I changed was the recipient - are you sure you've got a valid email address there? – Robbie Jackson Jun 7 at 21:22
  • For me I'm getting only : Error sending email – pysql Jun 7 at 22:54
  • Please improve your question by supplying diagnostic information. Please check your error logs and follow this advice on how to turn on PHPMailer's debugging mode: stackoverflow.com/a/2896502/2943403 – mickmackusa Jun 7 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.