# Code for receiving emails via Discourse API

**URL:** https://meta.discourse.org/t/code-for-receiving-emails-via-discourse-api/96894
**Category:** Support
**Created:** [September 10, 2018, 8:56pm UTC](https://meta.discourse.org/t/code-for-receiving-emails-via-discourse-api/96894 "2018-09-10T20:56:25Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sudoman](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@sudoman](https://meta.discourse.org/u/sudoman)
#### Post date: [September 10, 2018, 8:56pm UTC](https://meta.discourse.org/t/code-for-receiving-emails-via-discourse-api/96894/1 "2018-09-10T20:56:25Z")

</div>

Hello Discourse,

> [@Troubleshoot reply by email](https://meta.discourse.org/t/configuring-reply-via-email-e-mail/42026):
>
> If your Discourse does not have incoming email set up, see these topics: [Set up reply by email with POP3 polling](https://meta.discourse.org/t/set-up-reply-by-email-with-pop3-polling/14003)[Configure direct-delivery incoming email for self-hosted sites with Mail-Receiver](https://meta.discourse.org/t/direct-delivery-incoming-email-for-self-hosted-sites/49487)information_source If you already have basic reply via email set up (which is common on hosting plans), and you want to add the ability to start a new topic via email, [see this topic](https://meta.discourse.org/t/start-a-new-topic-via-email-e-mail/62977). Logging and Errors All incoming emails are logged and are visible in your admin panel at /admin/email/received.…

Is there a preexisting code base out there for pushing emails to Discourse’s incoming email API?

Thanks,  
Andrew

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [September 10, 2018, 11:29pm UTC](https://meta.discourse.org/t/code-for-receiving-emails-via-discourse-api/96894/2 "2018-09-10T23:29:01Z")

</div>

From the output of `rake routes` You can send a POST request to

```
admin/email/handle_mail

```

Here is the controller method from `handle_mail`:

> <https://github.com/discourse/discourse/blob/main/app/controllers/admin/email_controller.rb#L124>

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [September 10, 2018, 11:31pm UTC](https://meta.discourse.org/t/code-for-receiving-emails-via-discourse-api/96894/3 "2018-09-10T23:31:01Z")

</div>

> [@sudoman](#):
>
> Is there a preexisting code base out there for pushing emails to Discourse’s incoming email API?

> [@Configure direct-delivery incoming email for self-hosted sites with Mail-Receiver](https://meta.discourse.org/t/straightforward-direct-delivery-incoming-mail/49487):
>
> Discourse is all about enabling civilized discussion. While plenty of people like a web interface, e-mail is still the “hub” of many people’s online lives. That’s why sending e-mail is so important, and when you’re sending e-mail, you really want to be able to receive it, too. There are several reasons why: If e-mails “bounce” (they can’t be delivered for some reason), you need to know about that. Repeatedly sending e-mails that bounce will get your e-mails flagged as spam. Receiving e-ma…

---

<div class="post-metadata">

### Author: ![jshockley](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jshockley/32/156257_2.png) [@jshockley](https://meta.discourse.org/u/jshockley)
#### Post date: [January 13, 2020, 10:57pm UTC](https://meta.discourse.org/t/code-for-receiving-emails-via-discourse-api/96894/4 "2020-01-13T22:57:59Z")

</div>

For a non-technical user I’d need to understand the capabilities of inbound email via API:

**What I’ve done already:**

- Inbound Emails via POP3 polling to main domain (not subdomain)
- Working inbound email via the postfix mail module but that only works for subdomain - in my case, [xxx@community.eleoptics.com](mailto:xxx@community.eleoptics.com) - which we can not use, need primary domain.

**Current Setup:**

- Inbound email: G Suite Google “Groups” email address forwarding to free Gmail accounts (to avoid paying for a Gsuite account per email address). POP3 polling
- Outbound email: SMTP Relay Forwarding via GSuite validated by the IP of my Discourse server install on Google Cloud.

This works but the free Gmail pop3 polling accounts is something I’d like to get away from.

We plan to use the Google Cloud Platform Email API to handle email in-out ideally for larger scale deployment and service.

Where is the documentation I could send my tech employees on how to get inbound email going directly to discourse?

If that is already linked in the post above then thank you. Otherwise my guess is there needs to be a greater contextualized explanation.

> [@blake](#):
>
> Here is the controller method from `handle_mail` :
> 
> [github.com](https://github.com/discourse/discourse/blob/master/app/controllers/admin/email_controller.rb#L124)
> 
> #### [discourse/discourse/blob/master/app/controllers/admin/email\_controller.rb#L124](https://github.com/discourse/discourse/blob/master/app/controllers/admin/email_controller.rb#L124)
> 
> ```plaintext
> params.require(:email)
> user = User.find_by_username(params[:username])
> 
> message, skip_reason = UserNotifications.public_send(:digest, user,
> since: params[:last_seen_at]
> )
> 
> if message
> message.to = params[:email]
> begin
> Email::Sender.new(message, :digest).send
> render json: success_json
> rescue => e
> render json: { errors: [e.message] }, status: 422
> end
> else
> render json: { errors: skip_reason }
> end
> end
> 
> def smtp_should_reject
> 
> ```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [November 16, 2023, 1:04am UTC](https://meta.discourse.org/t/code-for-receiving-emails-via-discourse-api/96894/5 "2023-11-16T01:04:59Z")

</div>



---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [November 16, 2023, 2:54am UTC](https://meta.discourse.org/t/code-for-receiving-emails-via-discourse-api/96894/6 "2023-11-16T02:54:12Z")

</div>


