New Magento SMTP features: Email logging and email sending events

April 23, 2010

I had a longer than usual train ride on Friday, thanks to missing my usual one by about 20 seconds! The good news is, during the extended train ride I have added some new functionality to my SMTP Pro magento extension – it now optionally logs all emails to a database table and you can view the detail of them from within the Magento admin interface. I still can’t upload it to Magento connect however, so please download the release from here and let me know how it goes. Update: Fixed bug pointed out by Luis below – new release is 1.1.1.

I have also added a nice little event firing mechanism – I’m inviting fellow developers to suggest events they want fired, to make manipulating outbound emails easier for other extension developers. Currently the only one is smtppro_email_after_send but there is scope for adding many more – contact me!

I’ll add some notes about the new functionality below, but just to recap using my free smtp extension for Magento you can now:

  • Configure Magento for sending email with any SMTP server
  • Easily configure Magento to send email with GMail or Google Apps Email
  • Diagnose connection problems using the email self test
  • Send using a number of different gmail/google addresses if your email volumes are very high
  • Enable a development mode and an email suppression mode
  • View a log of all email sent from within Magento, the date they were sent and search and filter the list of emails
  • Hook into a growing number of email related events to extend the email capability of Magento

All for free too, I think it compares favorably with the commercial extensions that offer the same functionality. I’d appreciate any opinions on that, as I’m aiming to have the best email extension for Magento. Anyway here’s some info about the new features, in case you care.

The New features: Email Logging

Email logging was added in three parts which I’ll quickly cover below.

Firstly adding the mysql script that creates the table, and then the related objects. This is where the emails are stored.

$installer = $this;
 
$installer->startSetup();
 
Mage::log("Running installer");
 
$installer->run("
CREATE TABLE `{$this->getTable('smtppro_email_log')}` (
  `email_id` int(10) unsigned NOT NULL auto_increment,
  `log_at` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `to` varchar(255) NOT NULL default '',
  `template` varchar(255) NULL,
  `subject` varchar(255) NULL,
  `email_body` text,
  PRIMARY KEY  (`email_id`),
  KEY `log_at` (`log_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    ");
 
$installer->endSetup();

Secondly adding an event which fires each time an email is sent and then listening for that event in a logging observer.

Mage::dispatchEvent('smtppro_email_after_send', 
	 array('to' => $email,
		 'template' => $this->getTemplateId(),
		 'subject' => $this->getProcessedTemplateSubject($variables),
		 'html' => !$this->isPlain(),
		 'email_body' => $text));

and the observer itself…

	public function log($observer) {
 
		$event = $observer->getEvent();
		if (Mage::helper('smtppro')->isLogEnabled()) {
 
				Mage::helper('smtppro')->log(
				$event->getTo(),
				$event->getTemplate(),
				$event->getSubject(),
				$event->getEmailBody(),
				$event->getHtml());
		}
 
	}

Lastly adding an Admin interface to Magento that can display the emails in the table, and allow you to drill-in to them to see how they looked when sent.

    protected function _initAction() {
        // load layout, set active menu and breadcrumbs
        $this->loadLayout()
            ->_setActiveMenu('system/tools')
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('System'), Mage::helper('adminhtml')->__('System'))
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('Tools'), Mage::helper('adminhtml')->__('Tools'))
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('Email Log'), Mage::helper('adminhtml')->__('Email Log'));
        return $this;
    }

Which looks a bit like this:

The new Magento email log menu option - the observant among you may notice another little gem in this screenshot, stay tuned!

The new Magento email log menu option - the observant among you may notice another little gem in this screenshot, stay tuned!

You can view a list of all emails, sort by date, subject and to address - search and then view a selected email

You can view a list of all emails, sort by date, subject and to address - search and then view a selected email

You can see the details of an email as it was sent.

You can see the details of an email as it was sent.

The New features: Email Events

I’d really like to hear from any developers who with suggestions on what events they;d like in order to make email related extension development easier. I know for example in the past my extensions have clashed with some of Fooman’s extensions – hopefully adding events like this will allow us to work through the incompatibilities.

You can catch the events with xml like this in your config.xml global area:

<events>
  <smtppro_email_after_send>
    <observers>
      <smtppro_email_after_send_observer>
        <type>singleton</type>
        <class>smtppro/observer</class>
        <method>log</method>
      </smtppro_email_after_send_observer>
    </observers>
  </smtppro_email_after_send>
</events>

Too easy eh? I would really appreciate any feedback from those of you who give this extension a try.

Ashley

Posts

Hi! I'm Ashley Schroder, a Software Engineer from New Zealand - this is a collection of notes on my experiences with Ecommerce Web Development, particularly Magento Development.

Are Your Customers Getting The Magento Emails You Send?

MageSend Magento Email sending with Amazon SES

I have made a premium Magento Extension called MageSend. MageSend makes it simple and easy to send using Amazon's highly reliable SES email service.

The extension is $99, and comes with a complete money back guarantee, please check it out, I am confident it will resolve any email sending problems with your Magento store.

45 responses to New Magento SMTP features: Email logging and email sending events

  1. In Admin>Newsletter>Add New template

    Parse error: syntax error, unexpected ‘,’ in /home/xxxxxx/public_html/app/code/community/Aschroder/SMTPPro/Model/Newsletter/Template.php on line 99

  2. Thanks Luis – what a moron mistake, I’ll blame the bumpy train ride! 1.1.1 is released with the fix. Updated in the post.

  3. Thank you Ashley.
    Your work is very useful for we and the community in general.

  4. Hi, your module looks great.
    I’d like to try it out but I’m stuck at rather basic problem: How to install the release without Magento connect manager?

    I just hope that only way isn’t using System > Magento connect > Load local Package as it throws en error related to permissions.

  5. I don’t think you can use load local package, certainly I’ve never heard of that being used for installation. I’ll write up a quick 1050 minute blog post on how to install, seeing as it seems to be a hurdle for a few people, and this Magento Connect upload issue doesn’t seem to be getting fixed for me anytime soon.

  6. Done – here’s your guide to installing a Magento extension manually with FTP or SSH. Let me know if that helps.

  7. Hi,
    I installed successfully in magento 1.4.1, however when I am testing, I get error as below:

    ASchroder.com SMTP Pro Self-test results
    Testing outbound connectivity to Server:
    Using Google Apps/Gmail configuration options
    Failed to connect to SMTP server. Reason: Connection timed out(110)
    This extension requires an outbound SMTP connection on port: 587
    Sending test email to your contact form address xxx@gmail.com:
    Unable to send test email. Exception message was: Connection timed out…
    Please check and double check your username and password.
    Testing failed, please review the reported problems and if you need further help visit ASchroder.com to contact me.

    Don’t know how to fix, very new to magento stuff. pls help

  8. Hi, Brian

    Welcome to the world of Magento! That’s normally always a hosting problem, I’d suggest you forward that error to the hosts tech support department, they’ll know what it means. If they don’t, rethink your magento host!

    Cheers,
    Ashley

  9. Just installed this on my 3rd site, and it worked like a charm. This is easily a must-have extension for any Magento installation. It really should become a part of core!

  10. Hey Ashley,

    Just installed version 1.1.2 of your extension on magento 1.3.2.4, but am getting a 404 error on the admin url /admin/smtppro/log/index/ (System-Tools-Email Log). Any help?

    Best,
    Brogr

  11. Can you confirm you have logged out and in again, and cleared cache?

  12. Ashley,

    I just installed your latest version of the module in our 1.4.0.1 store and for some reason it completely killed the whole site, back and front end.
    I tried clearing the cache, logging out and back in to admin, but nothing helped.

    What was strange was, it acted like it wasn’t allowing even the .htaccess file to load before the site loads. I just get a blank screen on both front end and back end.

    Any ideas? I really need to get this working, our store is not sending any invoices, orders or anthing.

    Thanks Daniel

  13. Hi, Daniel – flick me an email with more details, I can take a quick look.

  14. Hi Ashley! First off thanks for this extension! I was able to install it properly on my 1.3.2.4

    But as soon as I tried to run a test here was the result

    ASchroder.com SMTP Pro Self-test results
    Testing outbound connectivity to Server:
    Using SMTP configuration options
    Connection to Host SMTP server successful.
    Sending test email to your contact form address support@lockbargains.com:
    Unable to send test email. Exception message was: 334 UGFzc3dvcmQ6 …
    Please check and double check your username and password.
    Testing failed, please review the reported problems and if you need further help visit ASchroder.com to contact me.

  15. Hi, That error normally means you have used the wrong password or something. Try logging in using the google email web interface to clear the error – it should present a captcha to make you prove you are human. I understand it’s a security measure to stop people trying to brute force break the email account password via the SMTP server.

  16. Hi Ashley, your module does not work. Installed it by FTP and it does not appear in admin, I found that you made error – in your sql/smtppro_setup/mysql4-install-1.1.0.php but the module version regarding etc/config.xml is 1.1.1 ?!?

    I changed this config.xml version to 1.1.0 but it still does not appear in admin.

    You better test your works before any releases.

  17. Hi, Miro

    Thanks for the input. Can you please run the following diagnostics for me to help determine what is wrong:

    1) In your magento database run select * from core_resource; and tell me what version is reported fro record with code: smtppro_setup (if any).

    2) Please paste the output from these commands run in the magento root directory:
    find app/code/community/Aschroder
    and
    find app/etc/modules

    Let me know the results and I can help find what is wrong. I just installed this extension myself today for a new store I’m working on, it worked fine on Magento 1.4.0.1.

    Cheers,
    Ashley

  18. Hi,
    I checked with my webhost nd they said to use smtp localhost and port 25. But after I so use them, I am getting error as below:
    *
    o ASchroder.com SMTP Pro Self-test results
    Testing outbound connectivity to Server:
    Using SMTP configuration options
    Connection to Host SMTP server successful.
    Sending test email to your contact form address brianpat@gmail.com:
    Unable to send test email. Exception message was: 550 “Your IP: 127.0.0.1 : Your domain is not allowed in header From” …
    Please check and double check your username and password.
    Testing failed, please review the reported problems and if you need further help visit ASchroder.com to contact me.

    Any idea, ho wto fix it

  19. I have to be honest, my default solution to any email related problem is to ditch your webhost’s email and just go get a Google Apps account for your domains email – Google’s solution provides consistently excellent service, features and interface. Otherwise, you really need to get support on that issue with your webhost. The extension is just using standard SMTP to send emails, should not require any special configuration. Let me know if that helps.

  20. Hey Ashley,

    Yes, I did log out and in again, and cleared the magento cache too. As soon as I try to access the email log or run the smtp-test I receive an 404 Page not found error.
    Any idea?
    Extension 1.1.2, Magento 1.3.2.4.

    Best,
    Brogr

  21. Can you tell me what URL it is trying to take you to? It sounds like something wrong with the controller setup, if you can see all of the admin configuration options ok.

  22. Erik Molenaar June 2, 2010 at 11:17 am

    Dear Ashley,

    Thanks for this awesome piece of software. I really like the logfunction to check what my Magento webstore has been doing. But…can you tell me how to clear the log (I did a lot of testing which I want to clear from the log)?

    Thanks in advance. Best regards, Erik.

  23. That’s a missing feature really – right now you could do it by deleting the entry from the logging table: smtppro_email_log.

    You could delete all logged emails by running this query:
    truncate table smtppro_email_log

    Have fun, be safe with those sql queries!

    (Thanks Steve for the tip about truncate being better)

  24. Hi Ashley,

    Wish I’d found this site months ago! Most impressed.

    Just a quickie on the last comment…

    It’s always far more efficielt to

    truncate table smtppro_email_log

    rather than

    delete from smtppro_email_log

    as it just deletes then recreates the table, rather than removing the contents row by row. Any related indexes are cleaned up too, so the end result is cleaner.

    Cheers, Steve

  25. Noted and updated – Thanks for the tip. Just checked out your blog too, some more good tips there by the looks. Will read it when I’m next on the train!

  26. Hi,
    really nice extension. I got it to work on only one of my sites.

    The site which is not working, is hosted on a linode host. I installed magento 1.4.0.1 just today from scratch, it’s working fine. When I install the extension, I see it in the admin interface, but settings are not saved, although I get a message that settings are saved OK.

    Any idea where I can look ?

  27. Hi, Edwin – I haven’t heard of that happening before – is your database saving other settings correctly?

  28. i am having a problem using email log in my magento 1.4
    when i am going to Email Log i am getting this error
    “Error log record number: 414745282899”
    How can i fix that?

  29. Hi, clockman – that’s strange, can you send me a screenshot of the error, it sounds like it might be an error that ends up in the var/report directory of Magento, but I’d need to see it to be sure. If it is, find the corresponding file in var/report, and send the contents – it’ll help us to find out what is wrong.

  30. i fixed that. i reinstalled the extension and thats ok now.

  31. Howdy – I’m getting 404 errors as well on the self test and on the admin page. This looks great – any thoughts?

    (did log out / log in thing, etc.)

    Does it make any js changes (I have hosted on cloudfront?)

  32. @Matt – Try fiddling with the ‘add store code to urls’ setting in Magento – people have reported that seems to cause 404’s. I haven’t had time to investigate it myself yet. Also, no js changes at all.

  33. Thanks Ashley — can’t do it as I’m multistore with a common checkout backend and that option is required to keep the carts working straight for some reason.

    If you ever figure it out let me know — great work and cool contribution!

  34. Just wanted to say thanks for this, made life a lot easier this morning.

    I had the same problem as Matt (404s on the self test, under Magento 1.4.0.1).Setting System->Config->Web->Add Store Code to Urls to “no” fixed it.

    Thanks again.

  35. Hi Ashley,

    Somehow, email logging is not working anymore. Last it logged something was 2 weeks ago. What could be the reason?

    We are running Magenton 1.4.2.

    Thanks.

  36. Rahul, did you install any new extensions? Maybe something is clashing?

  37. Well, we have lots of modules running on our shop. How do we get to know whether there is anything clashing?

  38. Hi Ashly

    How can i hook event before smtppro send email?

  39. Hello there,
    1. I’m ussing 1.6 and the extension function well, but not green debug message. THe message is red, but the emails was send successfully

    2. I got the a well known problem with the tool/email log, 404 not found. And doing the sysem/config/web/Add Store Code To URL to NO, resolve the problem.

    3. Thanks for a good job done!!!!! 🙂

  40. hi bro,,,,

    plz help me,,,have any fix for my problem,,,no emails sent from my own server, but when i am using xyz@gmail.com then ur extension work but when SMTP it dosn’t work. i am frusted too much.i have spent 1 week to fix but no luck….plz

    hertly thanks

  41. Hi Ashly,,,

    i have two more problem with my store……

    1–>Report–>Refresh Statistics, all are refreshed except Best Seller gives error msg unable to refresh,,,,,

    2–>another problem is Reports–>sales–>order–>show report,,,,

    gives error msg –page not found…show my host default page.

    sir thanks plz help me,,,,,

  42. Hi ashley

    i am getting a 404 error when i try to access the admin login with your latest version.

    any sugestions

  43. First of all. Great extension. So helpful!

    One problem thought. I need to have “Add Store Code to Urls” set to yes, which means that I’m getting a 404 when accessing the log. Any news on a bugfix?

    Cheers
    Seb

  44. I fixed the 404 problem as follows (since I have “Add Store code to Urls” enabled and I need them):

    Add this to the config.xml e.g. before “…”:

    admin

    Aschroder_SMTPPro
    smtppro

    The admin routers are not configured in the module, so I did it by myself. Works like a charm…

  45. Oh sh**, XML is not possible in comments 🙁 Just copy the “frontend” part in the config.xml, change “frontend” to “admin” and “standard” in the “use”-tag to “admin”. Thats all..