Send email in Drupal Localhost using MAMP/XAMPP for Mac with Postfix configuration

Public

After configuring your Mac's Postfix main.cnf w/ Gmail/SendGrid SMTP, e.g. using these guides:
1. http://budiirawan.com/install-mail-server-mac-osx
2. https://thoughts.duoconsulting.com/blog/setting-up-postfix-to-work-with-...

and verifying that these mail mechanisms work:
1. Mac's mail CLI: echo "Hello World" | mail -s "Test Postfix" a@b.com
2. PHP mail() function: custom, non-Drupal test script w/ mail($to, $subject, $message, $headers)

you might still have problem sending mail when in Drupal site/context. It turned out that MAMP/XAMPP for Mac stacks set the DYLD_LIBRARY_PATH environment variable in their "bin/envvars" config files which causes Mac's /usr/sbin/sendmail to use the MAMP/XAMPP's bundled, mail-related libraries instead of the native ones (system default).

A quick fix is to override/unset the AMP stack's DYLD_LIBRARY_PATH using Drupal's settings.php.

Get raw version
php
  1. // You could put this in the site's settings.php.
  2. // We want to use the Mac's mail utilities instead of the MAMP/XAMPP bundled ones by
  3. // unsetting the DYLD_LIBRARY_PATH set by the AMP stack to revert it to system default.
  4. // Tip: DYLD means "DYnamic Link eDitor".
  5. putenv('DYLD_LIBRARY_PATH');