# Send custom email from plugin

**URL:** https://meta.discourse.org/t/send-custom-email-from-plugin/81327
**Category:** Development
**Created:** [February 23, 2018, 5:29pm UTC](https://meta.discourse.org/t/send-custom-email-from-plugin/81327 "2018-02-23T17:29:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jordan1909](https://avatars.discourse-cdn.com/v4/letter/j/9f8e36/32.png) [@jordan1909](https://meta.discourse.org/u/jordan1909)
#### Post date: [February 23, 2018, 5:29pm UTC](https://meta.discourse.org/t/send-custom-email-from-plugin/81327/1 "2018-02-23T17:29:58Z")

</div>

In my plugin I am parsing all cc’d/bcc’d users and adding them to the topic they were cc’d on. This works well for both initial emails and forwarded replies. Based on a site setting I either create a full user or a staged email user. Where I have hit a snag is if I create a full user, I want to send an email saying: “hey a user was created … etc”

I can manually post to my sendgrid api and do this but that isn’t very good for a reusable plugin, is there any way to customize an email message and send directly from the discourse core?

---

<div class="post-metadata">

### Author: ![jordan1909](https://avatars.discourse-cdn.com/v4/letter/j/9f8e36/32.png) [@jordan1909](https://meta.discourse.org/u/jordan1909)
#### Post date: [February 23, 2018, 8:50pm UTC](https://meta.discourse.org/t/send-custom-email-from-plugin/81327/2 "2018-02-23T20:50:47Z")

</div>

Ok so I kind of got something working but I can’t figure out what is incorrect here, I’d appreciate any input.

If I do this:

```ruby
def self.send_user_email(address)
    message = Mail::Message.new(from: "from@fromEmail.com", to: address, body: "body would go here controlled by var")
    Email::Sender.new(message, :admin_login).send //I used admin_login as according to the spec it should send even if email is disabled. 
end

```

When I do this however I am getting an error.

If I run the code above natively I see:

_NoMethodError - undefined method `deliver_now' for #<Mail::Message:0x007fa1bf157300> Did you mean? deliver: mail (2.6.6) lib/mail/message.rb:1379:in` method\_missing’_

So if I go and update line [184 of the sender.rb](https://github.com/discourse/discourse/blob/master/lib/email/sender.rb) file to be: @message.deliver instead of deliver\_now I am able to get it to kinda try to send an email on my development environment.

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [February 24, 2018, 8:41pm UTC](https://meta.discourse.org/t/send-custom-email-from-plugin/81327/3 "2018-02-24T20:41:00Z")

</div>

First of all, you should probably send the email in a sidekiq job. I’d start by trying doing something like this within your plugin:

> <https://github.com/discourse/discourse/blob/main/app/jobs/regular/admin_confirmation_email.rb#L16-L17>

> <https://github.com/discourse/discourse/blob/main/app/mailers/admin_confirmation_mailer.rb>

So, essentially, try to reuse the `build_email` method. It should create a message that works. You can add the `template` to your plugin’s locale file.

I hope that helps a little bit. I’m sure you can figure out the rest… 😉
