Booker

Tuesday, October 3, 2017

How to configure mail forwarder, using Mandrill as a gateway

Mandrill API activation

After creating a new mandrill free account.
Validate DKIM and SPF in mailchimp's Mandrill account.

Then send validation domain mail, and steal the content in the API log, from the failed section.
The mail will contain a body having the full url to validate the domain, open that in any browser, where you are logged using Mandrill, then it will light up green.

Mail gateway

sudo apt-get install ssmtp

sudo vim /etc/ssmtp/ssmtp.conf
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=support

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.mandrillapp.com:587
# Where will the mail seem to come from?
#rewriteDomain=
#Auth info found at: SMTP & API Info, Mandrill's Settings dashboard
AuthUser={My Mandrill USER}
AuthPass={MY Mandrill API KEY}

# The full hostname
hostname=freedom.local

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
sudo vim /etc/ssmtp/revaliases
# Replace root@ with whatever name
support:support@freedom.local:smtp.mandrillapp.com:587

The resulting mail will be like this:
support <support@freedom.local>

Testing 

echo "My mail content" | sudo ssmtp -vvv damjandimitrioski@gmail.com

PHP setup

 sudo vim /etc/php5/fpm/php.ini
locate the [mail function] section and uncomment it
sendmail_path = /usr/sbin/ssmtp -t 
After change, restart php service.
sudo service php5-fpm restart

Testing

$ php -a
php >  mail("petko@gmail.com", "My subject", "My ugly short content");


Saturday, January 28, 2017

Filter for retrieving select field items

The following code needs to be put inside your theme functions.php

/**
 * @param $group_NAME
 * @return array
 */
function fetchACFFields($group_NAME)
{
 // Fetch fields for a given ACF fields group (PRO only)
 $groups = acf_get_field_groups();
 $fields_group = [];
 foreach ($groups as $group) {
  $title = $group['title'];
  if ($title == $group_NAME) {
   $f = acf_get_fields($group['ID']);
   foreach ($f as $i) {
    $fields_group[$group['title']]['fields'][$i['name']] = $i;
    $fields_group[$group['title']]['fields'][$i['name']]['value'] = get_field($i['key']);
   }
   break;
  }
 }
 return $fields_group;
}
add_filter( 'fetch_acf_fields', 'fetchACFFields', 10, 2 );

/**
 * @param $fields_group
 * @param $mysubfield
 * @return array
 */
function fetchSubfieldChoices($group_NAME, $mysubfield)
{
 $fields_group = apply_filters('fetch_acf_fields', $group_NAME);
 // Find choices from subfield
 $first_key = array_keys($fields_group)[0];
 $root = $fields_group[$first_key];

 $choices = [];
 foreach ($root['fields'] as $subfield) {
  foreach ($subfield['sub_fields'] as $sub_field) {
   $sub_field_name = $sub_field['name'];

   if ($sub_field_name == $mysubfield) {
    $choices = $sub_field['choices'];
   }
  }
 }
 return $choices;
}

add_filter( 'fetch_subfield_choices', 'fetchSubfieldChoices', 10, 2 );
and then execute:
/**
$choices = apply_filters('fetch_subfield_choices', $group_NAME, $mysubfield);