Skip to content

Support reply-to header #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/Messaging/Mail/MailBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class MailBag implements \JsonSerializable
*/
private $from;

/**
* @var From Reply-To
*/
private $replyTo;

/**
* @var string
*/
Expand Down Expand Up @@ -63,6 +68,15 @@ public function setFrom(string $fromEmail, string $fromName): void
$this->from = new From(new Email($fromEmail), $fromName);
}

/**
* @param string $replyToEmail
* @param string $replyToName
*/
public function setReplyTo(string $replyToEmail, string $replyToName): void
{
$this->replyTo = new From(new Email($replyToEmail), $replyToName);
}

/**
* @param mixed $subject
*/
Expand Down Expand Up @@ -165,6 +179,30 @@ public function getFromName(): string
return $this->from->getName();
}

/**
* @return string
*/
public function getReplyToEmail(): ?string
{
if ($this->replyTo) {
return $this->replyTo->getEmail();
}

return null;
}

/**
* @return string
*/
public function getReplyToName(): ?string
{
if ($this->replyTo) {
return $this->replyTo->getName();
}

return null;
}

/**
* @return string
*/
Expand Down Expand Up @@ -253,6 +291,7 @@ private function getContent(ContentType $contentType): ?Content

return null;
}

/**
* @return array
*/
Expand Down Expand Up @@ -302,6 +341,18 @@ function jsonSerialize(): array
'recipients' => $recipients
];

$replyTo = [];
if ($this->getReplyToEmail()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is one mistake, if replyTo email address is not set then code failed with message:

PHP Fatal error:  Uncaught Error: Call to a member function getEmail() on null in /Users/suchy/project/php-api-client-calthaeu/src/Messaging/Mail/MailBag.php:187

because we always want to retrieve property by method on object that was not initialize:

public function getReplyToEmail(): string
    {
        return $this->replyTo->getEmail();
    }

To fix code should look like this:

/**
     * @return string
     */
    public function getReplyToEmail(): ?string
    {
        if ($this->replyTo) {
            return $this->replyTo->getEmail();
        }

        return null;
    }

    /**
     * @return string
     */
    public function getReplyToName(): ?string
    {
        if ($this->replyTo) {
            return $this->replyTo->getName();
        }

        return null;
    }

and then it works perfectly

Copy link
Contributor Author

@scardinius scardinius Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, indeed. Fixed. Thank you!

$replyTo['email'] = $this->getReplyToEmail();
if ($this->getReplyToName()) {
$replyTo['name'] = $this->getReplyToName();
}
}

if ($replyTo) {
$data['replyTo'] = $replyTo;
}

if ($contents) {
$data['contents'] = $contents;
}
Expand Down