How to fix Send a friend Magento 2 module for email relay compatibility

To expand the awareness of your brand, Magento 2 allows inviting products to friends via email by setting up the Refer Email to a Friend in the configuration. For the easier way, Email a Friend link appears along with an envelope icon instantly on the product page. Just clicking on the link, customers are navigated to the Email to a Friend page, then complete all information of the email. It is possible to send to multiple recipients; however, to avoid the spam, you should limit the number of sharing products in one hour and the number of recipients per sent email.

One of the major bottlenecks here is that Magento recommends use of the SMTP module to successfully send and inbox inviting email, but that never works. Simply, the module-send-friend is designed to place the FROM with the Customer account or Guest account (if enabled) details. Even with all valid SMTP settings (details) this is most likely going to fail when sending emails to any popular email exchange provider like Google, Yahoo, Hotmail, Yandex or similar. Mostly experienced System administrators prevent this kind of FROM cloaking. To make this things work I've did one dirty trick, and yes I know Magento developers are going to say "Never edit Core files", but as this is just an experiment and not permanent solution I don't care.

I've figured that module is controlled from vendor/magento/module-send-friend/Model/SendFriend.php and that lines 179 and 180 controls this:

$sender = [
'name' => $this->_escaper->escapeHtml($this->getSender()->getName()),  
'email' => $this->_escaper->escapeHtml($this->getSender()->getEmail()),  
        ];

In my example I just edited these lines with the following:

$sender = [
'name' => $this->_escaper->escapeHtml($this->getSender()->getEmail()),  
'email' => 'no-reply@magentocommand.ml',  
//$this->_escaper->escapeHtml($this->getSender()->getEmail()),
        ];

To explain. I've swapped the 'name' and 'email' places since it is not necessarry to have Name field repeated twice. You can freely test this functionality of my adjusted code on my Demo/Test page https://magentocommand.ml/hanna-sweater.html by clicking on the Email icon here:

alt

If you are using AmazonSES or any other popular service which enforce the policy that the FROM sender needs to be used as a domain name used this would be extremely helpful to avoid usage of the SMTP module. As I've explained previously mostly SMTP module part fails because SPF/DKIM/DMARC fails.

Sample error:
2019-06-05 17:41:29 1hYZuj-00003b-PW ** x@xxx.xxx R=sendviases T=sessmtp H=xxxxx.com X=TLS1.2:ECDHERSAAES256GCMSHA384:256 CV=no: SMTP error from remote mail server after MAIL FROM:<> SIZE=3446: 501 Invalid MAIL FROM address provided

Once module tweak is added:
2019-06-05 17:49:32 1hYa2W-00003k-Fp => xx@xxx.com R=sendviases T=sessmtp H=xxxx.com X=TLS1.2:ECDHERSAAES256GCMSHA384:256 CV=no A=ses_login C="250 Ok 0100016b28c2a272-2250b0bf-d310-4e2a-b8c6-615259231294-000000"
2019-06-05 17:49:32 1hYa2W-00003k-Fp Completed

How to setup Refer Email to a Friend in Magento 2

Follow this article to learn how to setup Refer Email to a Friend in Magento 2:

On the Admin Panel, Stores > Settings > Configuration.

On the left panel, Catalog > Email to a Friend.
Open the Email Templates section,

alt

For Email to a Friend Email Template, you can read here:
https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/templates/template-email.html

The end results are working finally without SMTP module and as expected:
alt

The potential customer now can reply safely to the same or click on the link from Email template and browse the store.

ADVANCED WAY:

Let's admit that the code adjusted in the first place was the most easiest and probably not that recommended by general Magento standards so let's try now to pull the transemail/identsupport/email data which is defined within Stores --> Configuration --> Store Email Address area, so I have edited two files:

1) vendor/magento/module-send-friend/Helper/Data.php by adding:

public function getConfig($config_path)  
   {
       return $this->scopeConfig->getValue(
           $config_path,
           \Magento\Store\Model\ScopeInterface::SCOPE_STORE
       );
   }

2) vendor/magento/module-send-friend/Model/SendFriend.php

'email' => $this->_sendfriendData->getConfig('trans_email/ident_support/email'),  

(line 184)

THE ULTIMATE FINAL WAY
(ready for contribution submit to Magento):

1) Edit vendor/magento/module-send-friend/Helper/Data.php and just after const CHECK_COOKIE =0; add:

const XML_PATH_SENDER_EMAIL = 'trans_email/ident_support/email';

 /**
    * Get Sender Email
    *
    * @param int $store
    * @return string
    */

    public function getSenderEmail($store = null)
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_SENDER_EMAIL,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $store
        );
    }

2) Edit vendor/magento/module-send-friend/Model/SendFriend.php

'email' => $this->_sendfriendData->getSenderEmail(),  

(line 184)

Hope this workaround helps.