Extending Statamic’s Raven ‘From Name’

16th September 2014

I’ve recently integrated Raven Forms into my Statamic website and adding some new features as well as extending some existing ones: namely the feedback form. (More to come shortly)

I had been using Eric Barnes excellent email_form which uses the standard PHP Mailer however Raven gives me a bit for flexibility in a lot of other areas and supports third party transactional email services. There was one thing that annoyed me with Raven though: when sending emails through it and Mandrill (my transactional email service of choice) the "From Name" was absent and all I got was an email address.

Given that A) the standard PHP Mailer supported this via Erics add-on in my previous implementation, B) a bit of research showed that Mandrill supports this as standard, and C) the little details like that bug the crap out of me; it was time to do something about it.

Hack The Core Code

Would I do such a thing? Apparently so. Ignoring some of my own advice previously whereby modifying core files on a platform that you don’t own/control is a recipe for long-term disaster and that I’m unlikely to perform the sorts of regression testing the Statamic team would do, push that to one side, stick your fingers in your ears and let’s hack…

Note: All line number references are for v1.8.4 of Statamic which is current at time of publishing, although the last modified date of most of these files goes back several revisions.

FILE: _app/core/api/email.php

Around Line 26:

public static $allowed = array('to', 'from', 'subject', 'cc', 'bcc', 'headers', 'text', 'html', 'email_handler', 'email_handler_key');

Becomes: (by adding ‘fromname’,)

 public static $allowed = array('to', 'from', 'fromname', 'subject', 'cc', 'bcc', 'headers', 'text', 'html', 'email_handler', 'email_handler_key');

Around Line 103: Insert:

$email->setFromName($attributes['fromname']);

Around Line 187:

$email->FromName = $attributes['from'];

Becomes: (by adding ‘name’)

$email->FromName = $attributes['fromname'];

Around Line 271 add:

/**
 * Message fromname address
 * @var string
 */
public $fromname = "";

Around Line 354 add the Setter/Getters:

/**
 * Sets the fromname of the message
 *
 * @param string $from
 * @return void
 */
public function setFromName($fromname)
{
    $this->fromname = $fromname;
}

/**
 * Gets the fromname of the message
 *
 * @return string
 */
public function getFromName()
{
    return $this->fromname;
}

FILE: _app/vendor/Stampie/MessageInterface.php

Around Line 18 add the Getter:

/**
 * @return string
 */
function getFromName();

FILE: _app/vendor/Stampie/Message.php

Around Line 101 add the Getter:

/**
 * @return string
 */
public function getFromName()
{
    return $this->getFromName();
}

FILE: _app/vendor/Stampie/Mailer/Mandrill.php

Around Line 59 add the Getter:

'from_name'   => $message->getFromName(),

YAML

Once all that has been updated (it’s really not that much in the grand scheme of things) all you need to do is add something like this to your Raven YAML formset:

email:
  fromname: "John Chidgey"

Some notes to bear in mind:

  • I’ve only looked into Mandrill: the other supported mailers are your problem to figure out if you’re going to use them instead. The process should be very similar.
  • Hacking the Vendor add-on and the Core Statamic Email Library is kinda a bad idea. I know, I know already…
  • I’ve raised a feature request at The Lodge but I’m impatient okay?
  • No warranty is provided, implied, or any other disclaimer you might choose to think of: Insert disclaimer here. Don’t cry to me if something breaks after doing this you good for nothing hacker.
  • Hackin’s…hackin’s bad…mmm-kay?