1

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}
3

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

  • The code I posted is the entire content of test.php - I'm not passing any data in other than this. As for templateId, it exists because it loads the template data and template id is just one of the parameters within the data. Yes, this transactional email exists as id 20 in my install. I tried changing to $templateId = 'Test'; $template_collection = $mailTemplate->loadByCode($templateId); but it gives the same results. – travisw Aug 6 '15 at 1:45
  • Just updated my answer give that a go. – rob3000 Aug 6 '15 at 2:08
  • Still no success. I echoed and dumped everything to make sure it's all there and it is. template Test email travisw@example.com name trvsw store id 1 array(2) { ["name"]=> string(5) "trvsw" ["email"]=> string(19) "travisw@example.com" } array(1) { ["customerName"]=> string(5) "trvsw" } Something went wrong... I don't know why it wouldn't be working... I updated my question with the exception that is logged. – travisw Aug 6 '15 at 13:15
  • It looks like this is probably an issue with our server. It lets me send to my work email (hosted locally) but not to my gmail. – travisw Aug 6 '15 at 17:56
  • It looks like that it is your server. I just updated my answer but also have a look at stackoverflow.com/questions/7504424/… hopefully this helps – rob3000 Aug 6 '15 at 23:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.