Transactional Email – Can’t Manually Send in Magento

emailtransactional-mail

I'm trying to send a transactional email template to myself manually (on a test.php page) but it won't send despite all of my efforts and research.

I've tried all three posts linked in this question and can't get it to send anything.

include_once "app/Mage.php";
umask(0);
Mage::app();

$mailTemplate        = Mage::getModel('core/email_template');
$translate           = Mage::getSingleton('core/translate');
$templateId          = 20;
$template_collection = $mailTemplate->load($templateId);
$template_data       = $template_collection->getData();

if(!empty($template_data))
{
    // var_dump($template_data);
    $templateId  = $template_data['template_id'];
    $mailSubject = $template_data['template_subject'];

    $from_email  = 'trvsw@example.com';
    $from_name   = 'trvsw';
    $sender      = array('name'=> $from_name,
                         'email' => $from_email);

    $vars        = array('customerName' => 'trvsw');
    $storeId     = 2;

    $model = $mailTemplate->setReplyTo($sender['email'])->setTemplateSubject($mailSubject);
    $email = 'trvsw@example.com';
    $name  = 'trvsw';
    echo "template ".$templateId." sender ".$sender['name'].", ".$sender['email']." email ".$email." name ".$name." vars ".$vars['customerName']." store id ".$storeId."<br>";
    $model->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId);
    if(!$mailTemplate->getSentSuccess()) {
        echo "Something went wrong...<br>";
    } else {
        echo "Message sent to ".$email."!!!<br>";
    }
    $translate->setTranslateInline(true);
}

Every time, it just spits out "Something went wrong…" Any ideas?

The transactional email is titled "Test" with the subject "Test" and the body

This is a test.

Thank you, {{var customerName}}

Edit:

Here is the exception that I'm finding in my exception.log

2015-08-06T13:15:01+00:00 ERR (3): 
exception 'Zend_Mail_Protocol_Exception' with message '5.1.1 Recipient unknown
' in /var/www/sites/mbs/lib/Zend/Mail/Protocol/Abstract.php:431
Stack trace:
#0 /var/www/sites/mbs/lib/Zend/Mail/Protocol/Smtp.php(289): Zend_Mail_Protocol_Abstract->_expect(Array, 300)
#1 /var/www/sites/mbs/lib/Zend/Mail/Transport/Smtp.php(217): Zend_Mail_Protocol_Smtp->rcpt('travisw@example...')
#2 /var/www/sites/mbs/lib/Zend/Mail/Transport/Abstract.php(348): Zend_Mail_Transport_Smtp->_sendMail()
#3 /var/www/sites/mbs/lib/Zend/Mail.php(1194): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))
#4 /var/www/sites/mbs/app/code/local/Moogento/Pickpack/Model/Core/Email/Templateattach.php(1983): Zend_Mail->send(Object(Zend_Mail_Transport_Smtp))
#5 /var/www/sites/mbs/app/code/core/Mage/Core/Model/Email/Template.php(508): Moogento_Pickpack_Model_Core_Email_Templateattach->send('travisw@example...', 'trvsw', Array)
#6 /var/www/sites/mbs/test.php(50): Mage_Core_Model_Email_Template->sendTransactional('Test', Array, 'travisw@example...', 'trvsw', Array, '1')
#7 {main}

Best Answer

Looking at your code there are a few items that doesn't seem right. You can update it to the following:

include_once "app/Mage.php";
umask(0);
Mage::app();

$mailTemplate        = Mage::getModel('core/email_template');
$translate           = Mage::getSingleton('core/translate');
$templateId          = 20;
$template_collection = $mailTemplate->load($templateId);
$template_data       = $template_collection->getData();

if(!empty($template_data)) {
    $mailSubject = 'Subject';   
    $from_email  = 'trvsw@example.com';
    $from_name   = 'trvsw';
    $sender      =  array(
                       'name'  => $from_name,
                       'email' => $from_email
                   );

    $vars        = array('customerName' => 'trvsw');
    $storeId     = Mage::app()->getStore()->getStoreId();

    $model = $mailTemplate->setReplyTo($sender['email'])->setTemplateSubject($mailSubject);
    $email = 'trvsw@example.com';
    $name  = 'trvsw';
    try {
        $model->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId);
    if(!$mailTemplate->getSentSuccess()) {
        echo "Something went wrong...<br>";
    } else {
        echo "Message sent to ".$email."!!!<br>";
    }
    $translate->setTranslateInline(true);
    } catch(Exception $e) {
       Mage::logException($e)  ;
    }
}

Originally in your code you're assigning $templateId = $template_data['template_id']; which doesn't exist.

I tested the code and that seems to be the only item that doesn't seem right. I'd also look at changing how you're loading the template to use the name rather than the ID e.g Mage::getModel('core/email_template')->loadByCode('Tests') if Tests is the name of your template you have setup.

EDIT:

Looking at your update looks like its your SMTP server on your local environment that is unable to send the email. see https://stackoverflow.com/questions/7504424/sendmail-returns-error-code-550-5-1-1-user-unknown

Related Topic