Skip to content

Instantly share code, notes, and snippets.

@basherr
Created December 10, 2020 07:54
Show Gist options
  • Save basherr/8b8685e207d22b7f90fd399c6657f853 to your computer and use it in GitHub Desktop.
Save basherr/8b8685e207d22b7f90fd399c6657f853 to your computer and use it in GitHub Desktop.
Composing email
public function send(
SendEmailRequest $request,
Email $email,
EmailBuilder $emailBuilder,
OutgoingEmailQueue $emailQueue
)
{
$from = auth()->user()->email;
$to = collect(explode(',', $request->to));
$subject = $request->subject;
$content = $request->content;
// create a unique message-id to avoid duplication
$messageID = md5($request->to . $from . $subject . time());
$email->import([
'to' => $to,
'cc' => collect([]),
'envelope' => ['to' => $to, 'cc' => collect([])],
'from' => $from,
'subject' => $subject,
'raw' => $content,
'html' => $content,
'text' => $content,
'attachments' => collect([]),
'headers' => collect([
'message-id' => $messageID,
])
]);
$outgoing = $emailBuilder->buildOutgoingList($email);
$emailQueue->merge($outgoing)
->flush();
return back()->with('success', 'Email sent successfully.');
}
// we need to add some sort of headers for custom composing
// so that external emails are not skipped and should be merged with reply-token emails
protected function buildRecipientList($recipients, $buildExternal = false)
{
$groupedRecipients = $recipients->mapToGroups(function ($recipient) {
// ensure only gemacademy.nz addresses are in envelope:to
return [$this->recipientType($recipient) => $this->recipientAddress($recipient)];
});
$groupExternalEmails = $groupedRecipients->get('reply-token');
// add external emails too since they haven't received any copy
// because it has been composed
if($buildExternals && !is_null($groupExternalEmails)) {
$groupExternalEmails->merge( $groupedRecipients->get('external') );
} else if($buildExternals) {
$groupExternalEmails = $groupedRecipients->get('external');
}
// if there are reply-token addresses we have to replace them out all at once
return $groupedRecipients->get('proxy', collect())
->push($groupExternalEmails)
->filter();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment