Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

MIME header generator function.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Oct 14, 2015 7:23 pm    Post subject: MIME header generator function. Reply with quote

The plugin will generate a(n almost) correct "Name: value" MIME headers for things like EML or MHT files. Basically any MIME-appropriate container.
It has some shortcuts, and if someone fancy, I would gladly accept the patches to fill in the blanks.

Example usage:
Code:
$smarty->assign('header', array(
  'from' => array('name' => 'Me', 'email' => 'me@example.com'),
  'to' => array('name' => 'You', 'email' => 'you@example.com'),
  'subject' => 'Last sunday of october.'
));

{foreach $header as $name => $value}{mime_header name=$name value=$value}
{/foreach}


Parameters:
name(string) - header name
value(string or array*) - header value
wrap(number, optional) - wrap length (default is an iconv comple-time default)

*If 'name' refers to one of the known address headers (To, From, Sender, Cc etc.), 'value' can be an array of 'name'-'email' pairs as well, indicating recipient names and addresses, which it will try to correctly stitch together into appropriate address string.

The plugin (iconv is required, mbstring [should be] optional):
Code:
<?php
/** Smarty function plugin
*/

function smarty_function_mime_header($params, Smarty_Internal_Template $template)
{
  $_name = mb_convert_case($params['name'], MB_CASE_TITLE);
  $_value = $params['value'];
  $_pref = array(
        'input-charset' => is_callable('mb_internal_encoding')
          ? mb_internal_encoding()
          : 'UTF-8',
        'output-charset' => is_callable('mb_internal_encoding')
          ? mb_internal_encoding()
          : 'UTF-8'
        );
  isset($params['wrap']) ? $_pref['line-length'] = $params['wrap'] : false ;

  switch($_name)
  {
    case 'From':
    case 'Sender':
    case 'To':
    case 'Cc':
    case 'Bcc':
    case 'Resent-From':
    case 'Resent-Sender':
    case 'Resent-To':
    case 'Resent-Cc':
    case 'Resent-Bcc':
      if(!is_array($_value))
      {
        throw new Exception('Compound address strings support not implemented yet.');
      }

      if(!isset($_value['email']))
      {
        throw new Exception('Address not defined or multi-recipient header is requested (not yet implemented).');
      }

      $_aname = isset($_value['name']) ? mb_encode_mimeheader($_value['name']) . ' ' : '';

      if(is_array($_value['email']))
      {
        $_address = '';
        foreach($_value['email'] as $v)
          $_address .= (empty($_address) ? '' : ', ') . "<$v>";
        $_address = mb_encode_mimeheader($_address);
      }
      else
      {
        $_address = "<{$_value['email']}>";
      }

      return "$_name: {$_aname}{$_address}";
    default:
      return iconv_mime_encode($_name, $params['value'], $_pref);
  }
}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP