Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SilverStripe SendGrid Mailer
# SilverStripe SendGrid Mailer

Simple mailer module that uses SendGrid API to send emails.

Expand All @@ -21,6 +21,7 @@ Add the following to your `app.yml`:
```yaml
Toast\SSSendGrid\SendGridMailer:
api_key: 'YOUR_SENDGRID_API_KEY'
default_email: 'from@example.com' # Required for password reset form
```

## Usage
Expand Down
15 changes: 10 additions & 5 deletions src/SendGridMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
class SendGridMailer implements Mailer
{

public function send($email)
public function send($email)
{

if (!$apiKey = Config::inst()->get(self::class, 'api_key')) {
user_error(self::class . ' requires a SendGrid \'api_key\'. Please add it to your YML configuration.');
}

$sendGridEmail = new \SendGrid\Mail\Mail();
$sendGridEmail = new \SendGrid\Mail\Mail();
$sendGridEmail->setSubject($email->getSubject());

$emailsFrom = Email::getSendAllEmailsFrom();
Expand All @@ -26,6 +26,11 @@ public function send($email)
$fromEmail = array_keys($from)[0];
$fromName = $from[$fromEmail];
$sendGridEmail->setFrom($fromEmail, $fromName);
}elseif ($from = Config::inst()->get(self::class, 'default_email')) {
$sendGridEmail->setFrom($from);
}else{
user_error(self::class . ' requires a SendGrid \'default_email\'. Please add it to your YML configuration.');
exit;
}


Expand All @@ -35,7 +40,7 @@ public function send($email)
$replyToName = $replyTo[$replyToEmail];
$sendGridEmail->setReplyTo($replyToEmail, $replyToName);
}

$emailsTo = Email::getSendAllEmailsTo();
$to = is_array($emailsTo) && count($emailsTo) ? $emailsTo : $email->getTo();
if (is_array($to)) {
Expand Down Expand Up @@ -81,9 +86,9 @@ public function send($email)

} catch (\Exception $e) {
return false;
}
}

}


}
}