3 ways to achieve the dynamic domain in the from email address of a Formlister form.
#1 @Pathologic's version sounds the best...
(this didn't work for me with one host where email form delivery was a little problematic. I used method #2 below for that site and it works fine.)
<?php
/**
* prepareFromAddress
*
* Dynamic noreply email address for Forms
*
* @category snippet
* @version 1.0
* @lastupdate 7/12/2021
* @author Pathologic
* @forum discussion https://forum.evo.im/d/177-dynamic-noreply-at-domain-email-address-for-formlister-sender/
*
* Example Usage: [!FormLister ...
* &prepare=`prepareFromAddress`
* ... !]
* NB. Remove the "&from" parameter
*/
//prepareFormAddress
$FormLister->mailConfig['from'] = 'noreply@' . $_SERVER['SERVER_NAME'];
#2
<?php
/**
* getDomain
*
* Get domain name - remove http(s)://, www., and forward slash(/) from the URL
*
* @category snippet
* @version 1.0
* @lastupdate 7/12/2021
* @source: https://speedysense.com/php-remove-http-https-www-and-slashes-from-url/
* @forum discussion: https://forum.evo.im/d/177-dynamic-noreply-at-domain-email-address-for-formlister-sender/
*
* Example Usage: [[getDomain]]
* Example Usage in a noreply from email address in a FormLister form: &from=`noreply@[[getDomain]]`
*/
// Remove http(s)://, www., and forward slash(/) from the URL
$input = $modx->getConfig('site_url');
// Output domainname.tld
echo preg_replace( "#^[^:/.]*[:/]+#i", "", preg_replace( "{/$}", "", urldecode( $input ) ) );
#3
<?php
/**
* getDomain2
*
* Get domain name - remove http(s)://, www., and forward slash(/) from the URL
*
* @category snippet
* @version 1.0
* @lastupdate 7/12/2021
* @source: https://speedysense.com/php-remove-http-https-www-and-slashes-from-url/
* @forum discussion: https://forum.evo.im/d/177-dynamic-noreply-at-domain-email-address-for-formlister-sender/
*
* Example Usage: [[getDomain2]]
* Example Usage in a noreply from email address in a FormLister form: &from=`noreply@[[getDomain2]]`
*/
// Get Evo's site_url
$input = $modx->getConfig('site_url');
// Trim forward slash
$input = trim($input, '/');
// If url doesn't have http:// or https:// then prepend it
if (!preg_match('#^http(s)?://#', $input)) {
$input = 'http://' . $input;
}
// Parse url
$urlParts = parse_url($input);
// Remove www.
$domain_name = preg_replace('/^www\./', '', $urlParts['host']);
// Output domainname.tld
echo $domain_name;
Thanks for the help.