Sending e-mail programmatically in Magento is failing

Sending e-mail programmatically in Magento is failing

July 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Sending e-mail programmatically in Magento is failing

Sending Emails Programmatically in Magento: A Comprehensive Guide

As an e-commerce platform, Magento provides robust functionality for sending emails, including newsletter subscriptions, order confirmations, and customer notifications. However, the built-in email configuration in Magento doesn’t explicitly allow you to specify SMTP server credentials such as a username and password directly from the system settings.

To overcome this limitation, you don’t necessarily need to make changes to the getMail() method outlined in the provided Magento forum post link. Instead, we can create a custom email template and send it programmatically from a controller action using the existing Magento email functionality.

Creating an Email Template

First, let’s create a new transactional email template in Magento:

  1. Navigate to Configuration > Transactional Emails.
  2. Click on “Add New Template.”
  3. Enter the template name (e.g., “Bob”).
  4. Add variables to your template using the syntax {{var myvar1}} and {{var myvar2}}.
  5. Save the template.

The system will generate a unique Template ID for your new template, which is 1 in this case.

Sending Email Programmatically

Now let’s send an email from a controller action in Magento:

public function sendEmailAction()
{
    // Load the default email template
    $emailTemplate = Mage::getModel('core/email_template')->loadDefault('no matter what goes here emailTemplate is not set');

    // Alternative: Load the custom template by its code (not recommended)
    // $emailTemplate  = Mage::getModel('core/email_template')->loadByCode('Bob');

    // Set sender information
    $emailTemplate->setSenderEmail('sent@byme.com');
    $emailTemplate->setSenderName('Steve');

    // Prepare email variables
    $emailTemplateVariables['myvar1'] = 'TestValue1';
    $emailTemplateVariables['myvar2'] = 'TestValue2';

    // Process the template with variables (not required)
    // $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);

    // Send the email to a recipient
    $emailTemplate->send('thisisme@mydomain.com', 'John', $emailTemplateVariables);
}

The loadByCode() function mentioned in your question is indeed not well-documented. It loads an email template by its code or identifier. In this case, we can simply use the default email template provided by Magento.

Unfortunately, sending emails programmatically using the built-in Magento email functionality might still result in errors due to the stream_socket_enable_crypto() warning you encountered link. This warning occurs because of an SSL/TLS issue with the SMTP server configuration.

To resolve this issue, I recommend using a custom extension or a third-party module that provides more advanced email sending capabilities, such as MageMailer or Amasty Email Sending. These extensions offer better control over the email delivery process and can help you avoid the stream_socket_enable_crypto() warning.

In conclusion, while Magento’s built-in email functionality has some limitations in terms of SMTP server configuration, it is still possible to create custom email templates and send them programmatically from a controller action using the existing core features. For more advanced requirements or to resolve SSL/TLS-related issues, consider implementing third-party email sending extensions for improved performance and reliability.

For further reading on Magento’s email functionality and best practices, I recommend checking out our in-depth guides on Sending Emails in Magento and Magento Transactional Emails Made Easy.

Happy coding!

Themes Configuration Email Orders

Our network