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

FR: Extend Email API to support attachments #1422

Closed
Nevcairiel opened this Issue May 15, 2017 · 2 comments

Comments

Projects
None yet
4 participants
@Nevcairiel

Nevcairiel commented May 15, 2017

Feature Request

It would be awesome to extend the Statamic Email API to support file attachments (ie. basically forwarding the laravel support for attachments).

While we can use the Laravel API directly as a work-around, using the Statamic API links better into the entire system, and would make some of our custom site-specific addons much nicer.

@jackwakefield

This comment has been minimized.

jackwakefield commented Feb 21, 2018

I've created a small patch for this

diff --git a/statamic/core/Email/Builder.php b/statamic/core/Email/Builder.php
index 3e60242..3d31962 100644
--- a/statamic/core/Email/Builder.php
+++ b/statamic/core/Email/Builder.php
@@ -114,6 +114,13 @@ class Builder
         return $this;
     }
 
+    public function attach($file, array $options = [])
+    {
+        $this->message->attach($file, $options);
+
+        return $this;
+    }
+
     public function automagic($automagic = true)
     {
         $this->message->automagic($automagic);
diff --git a/statamic/core/Email/Message.php b/statamic/core/Email/Message.php
index 6b6ff18..41ec9aa 100644
--- a/statamic/core/Email/Message.php
+++ b/statamic/core/Email/Message.php
@@ -54,6 +54,11 @@ class Message
      */
     protected $data = [];
 
+    /**
+     * @var array
+     */
+    protected $attachments = [];
+
     /**
      * @var bool
      */
@@ -198,6 +203,18 @@ class Message
         return $this;
     }
 
+    public function attachments()
+    {
+        return $this->attachments;
+    }
+
+    public function attach($file = null, array $options = [])
+    {
+        $this->attachments[] = [$file, $options];
+
+        return $this;
+    }
+
     public function automagic($automagic = null)
     {
         if (is_null($automagic)) {
diff --git a/statamic/core/Email/Sender.php b/statamic/core/Email/Sender.php
index a172581..e736d08 100644
--- a/statamic/core/Email/Sender.php
+++ b/statamic/core/Email/Sender.php
@@ -69,6 +69,10 @@ class Sender
             }
 
             $m->subject($this->message->subject());
+
+            foreach ($this->message->attachments() as list($file, $options)) {
+                $m->attach($file, $options);
+            }
         });
     }
 
@jackmcdade

This comment has been minimized.

Member

jackmcdade commented Feb 21, 2018

In 2.8.7. Thanks for the patch!

@jackmcdade jackmcdade closed this Feb 21, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment