daniel-zahariev / php-aws-ses

PHP classes that interfaces Amazon Simple Email Service
307 stars 100 forks source link

Can we add friendly name in email messages? #71

Closed bmellink closed 4 years ago

bmellink commented 4 years ago

It would be great to allow adding a friendly name for the from address, in a way like this:

$m->setFrom('user@example.com', 'John Doe');

I implemented this by adding the following changes in SimpleEmailServiceMessage.php:

   public function setFrom($from, $fromname='') {
        $this->from = $from;
        $this->fromname = $fromname;     // added statement

        $this->is_clean = false;

        return $this;
    }

Then in SimpleEmailService.php I made a change in the function sendEmail() by changing the line:

$ses_request->setParameter('Source', $sesMessage->encodeRecipients($sesMessage->from));

into:

    if ($sesMessage->fromname)
        $ses_request->setParameter('Source', '"'.$sesMessage->fromname.'" <'.$sesMessage->from.'>');
    else
        $ses_request->setParameter('Source', $sesMessage->encodeRecipients($sesMessage->from));

For this to work in raw email mode the same change needs to be made to getRawMessage();

        if ($this->fromname) 
            $raw_message .= 'From: "' . $this->fromname . '" <' . $this->from . '>' . "\n";
        else
            $raw_message .= 'From: ' . $this->encodeRecipients($this->from) . "\n";

Can you please add these changes to the master? (or implement the same functionality differently).

daniel-zahariev commented 4 years ago

Hi @bmellink , you can use this functionality with the following format:

$m->addTo('Recipient Name <recipient@example.com>');
$m->setFrom('Sender <user@example.com>');