Sending newsletters with SendGrid and Rails - What is the best approach? - ruby-on-rails

I have a rails app with sendgrid API setup and I can send transactional emails just fine. Now I want to be able to send newsletters but I'm a bit confused about the flow and how to keep track of which users want to unsubscribe from the newsletter.
The way I was considering doing this is when a use signsup to automatically send a request to the SendGrid API and add them to the contacts list, and if they want to unsubscribe from the newsletter or transactional emails from my app's settings page I can save this info in my app's DB but also send a request to sendgrid's unsubscribe list to change the value there too. Now I can create a newsletter in sendgrid and send it out from there without having to do anything within my app. One caveat to this approach would be if the users clicks the unsubscribe button via the newsletter and the value gets changed in sendgrid but the change won't be reflected in my app - To work around this would it be better to create a cron job that periodically checks sendgrid for any changes and then save them to my DB, or should the unsubscribe page be part of my app, and when they select unsubscribe I save it to my DB and then push the changes to sendgrid? If i do this approach, would I require users to sign in if they want to make these changes?
An alternative approach would be to forego creating any contact lists in sendgrid and just create the email in sendgrid and sent it out using a rake tasks to trigger the sending?
Apologies if this is a very basic question, but this is my first time trying to setup newsletters etc and can't seem to find anything online about the best strategy to take in order to do this - I hope this question makes sense.

Twilio SendGrid developer evangelist here.
This question is a little too opinion based for Stack Overflow, many of the options you describe will work but they rely on whether it's the right decision for your application. But, I will try to give some guidance.
First, however you approach this, I would recommend you set up subuser accounts within SendGrid so that your transactional and newsletter emails come from different subusers. This way unsubscribes from newsletters won't affect transactional emails.
I would use the SendGrid contacts list to send out newsletters. This gives you a lot of power over your contacts without you having to build things yourself. You can segment your lists, create unsubscribe groups (where a user can unsubscribe from a subset of your emails instead of all of them) and send emails from within SendGrid without bothering your Rails app. You can still set up to trigger a newsletter from your app using the API if you want to.
As for maintaining the user's subscription status, I would go with exporting the contact lists and keeping your database up to date that way. The important thing when sending to your contact list is that SendGrid has the source of truth for subscription status and your application can be a bit behind if it needs to be. The SendGrid docs for exporting contacts also say:
Twilio SendGrid recommends exporting your contacts regularly as a backup to avoid issues or lost data.
So this would fulfil that suggestion too.
As for the unsubscribe links, the easiest way is to use the SendGrid unsubscribe form, have SendGrid handle all the unsubscribes, and your exports can keep your database up to date. That also allows you to handle the addition of unsubscribe groups without any more code on your side.
With more work you could create your own unsubscribe form so that your database stays more up to date with the subscription status. Your choice over whether a user should be logged in or not depends on how much friction you want to give the user before they unsubscribe and how much issue you think you might get from users forwarding your emails and having their friends unsubscribe them. That is up to you to decide on though.
Hope this helps a bit!

Related

Create custrom domain specific emails to send and receive emails in Rails 6+

I have a Rails6+ application, where I want the users to create custom emails that they can use to send and receive emails under my domain(mikesgh).For example users can create emails like anything#mikesgh.com, arnie#mikesgh.com. These emails should also be ready to send/receive emails out of the box
I did tried looking for the same features in SendGrid, MailChimp/Mandrill but unable to find anything that might help.
MOreover the other way I think is to create emails dynamically through my Google Cloud(domain is registered from google) and then use it and destroy it on demand, but I have to pay for every newly created users.
How can I achieve this on the fly, is that possible? is there any other alternative to give similar feature to the user where user can use custom emails and then on my platform/website, can see the received messages? I am using SendGrid.
Quick update - I came across a feature of SendGrid called Inbound Parse, which allows to setup a routing to forward incoming emails to custom Rails ActionMailbox, Can this help me to achieve what I want? I havn't tried this earlier but would love to hear from others what do they say?

Handle emails in a Rails 7 Multitenant Web App

I'm developing a multi tenant (with apartment gem) dashboard application with Rails 7. I've bought my own domain for the application but I have no email service yet.
How does my app work?
A customer sign up for it
He can decide to use a custom subdomain like customer1.example.com to reach his dashboard
He also can chose to use his own domain like customer1.com. I've handled this part with cloudflare for SaaS feature
He will use his own dashboard to let other people join, create accounts, interact with that dashboard
What am I looking for?
I'm looking for the best solution to implement these features:
Sending and receiving email for my application: something like info#mydomain.com, support#mydomain.com etc... (and this is the "easy" part. I think I simply need an email host
Here's come the (for my little experience) hard part:
How can I create a system of mail for actions like signup confirmation etc? I think those could just be some sort of "from" email and don't really need to exists.
How can I let the customer send/receive mail sent for example to customer1#mydomain.com or info-customer1#mydomain.com?
In case I just said something really stupid, what should be the correct way to handle emailing in an application like this?
Thanks to everyone, tell me if you need more information!
customer1#mydomain.com and info-customer1#mydomain.com are mailboxes.
You should create the setting for each company and use it to send/receive the email. For dynamically, you can create options to select belike "send as a mailbox"
If you want to find a product to manage sent/received emails from many mailboxes, you can try this instead of implementing

Ruby on Rails. Using Google Client API to parse emails

I am new to Ruby and have a question about how to do a specific task on Rails.
I have a list of inventory and each item has a specific Stock ID that is emailed to my personal Gmail account. I want my web application to listen for emails from a specific email account. When my gmail receives an email from that specific account I want my application to parse it for a couple of fields and insert the stock ID into my database.
For example:
Let's say my database has an item with style code: A5U31 and size:10.
The email will say something like item with style code: A5U31 and size:10 has Stock ID:329193020.
I want my Rails application to search the database for an entry with that specific style code and size, and when it finds an entry to simply insert the stock ID into the row.
I am trying to using the Google-API-Client gem to this, but I am struggling, because I am still a beginner. So far I have done this quick-start guide to authenticate my gmail account with my rails app.
https://developers.google.com/gmail/api/quickstart/ruby?authuser=2
If someone could help me figure out how to write this sort of code and where to put it in my app(models, controllers, views) that would be very appreciated. Thanks!
I know it's been several months since you posted this, so you probably already have it worked out, but in case you still need a solution (or if someone else wants to do something similar), I'll share my thoughts:
At a high level, it sounds like your plan is
Identify when a new email has come in (either by polling or by using a push notification).
Grab the new email's content.
Parse the email's content in order to extract relevant data.
Use the data to query and update a database.
Based on the documentation for the Gmail API, it does look like you should be able to set up push notifications, so you won't have to poll the endpoint to get the information you need.
However, my big takeaway from this list is that none of the items on it really require Rails, since you're not exposing an external web API for requests. I suppose that you could leverage ActiveRecord to create an item model and use that to manage the database; however, since it seems like you'd only need to make some basic SQL queries (and the same ones each time), I'm not sure that bringing in ActiveRecord adds much value.
If I were trying to solve this problem myself, I would probably create a simple Ruby program that (a) uses the gem you mentioned to handle push notifications from the Gmail API, and (b) uses another gem to connect to whatever kind of database you're using (e.g. pg for Postgres) and make the necessary queries.
(All of this assumes, of course, that you aren't specifically using Rails for some other reason, e.g. adding this feature to an existing Rails application).

Any idea to archive emails in Conversations in Asana?

I would like to centralize every email from (or to) customers of a project in the Conversations view of a project in Asana. The goal is to keep an archive of all exchanges with a customer in one place for every team member.
I tried to use the project#mail.asana.com as CC in every emails that i send but customers don't have accounts on Asana (and i don't want them to access it) and so they can't save their replies in Conversations. I tried also to create an email group (in Google Apps) and add the Asana email at it but it didn't work.
Any idea to use the Conversations view as an archive ? Or maybe is there an external tool which integrate with Asana that can do this ?
Thanks a lot in advance !
Hmm, that's interesting! I suppose that if you don't have too many emails this makes some sense (otherwise it might make your Asana Inbox pretty noisy - everyone would see in their inbox every conversation)
I think what I would do is to set up a Gmail filter to automatically forward the email to your team. You can do this in Gmail like this: https://support.google.com/mail/answer/6579?hl=en. If you set up a good filter (i.e. sent to a group address) it seems like it would be pretty painless.
One thing to note is that the conversation in Asana will appear to come from whoever is associated with the email that's sending the conversation to Asana, so if you have a single POC with the outside world, it may make sense to only forward from their account. Alternatively, you could set up a special Asana user just for this purpose, and your teammates can follow a convention that "mailbot" (or whatever user it is) is just used to forward mail, and you should look at the content to get who the author was.

General Guidance on Rails Mailers

I am making an event registration tool in Rails and I am having trouble working out the mailing section. I am using Mailgun API and I've got a generic "Thank you for Registering" email working when the user signs up as well as a contact form submission that comes to my email. Part of the requirements for the application is the ability to send promotional emails (separate from Thank you for Registering emails). These promotional emails are more like (One week reminder) type emails.
So these emails need to be able to be created by the admin setting up the event as this is a general purpose tool. So to save the emails the admin creates, I have a mailings object. So the relationship is a bit like this:
Event has many mailers, registrations, etc. (and those belong to the event). They are nested resources because they are specific to an event. Now I need to bridge the gap of how to go from the mailers created by the admin to sending them to Mailgun. The problem is we will have to have the ability to add recipients because they may want to send to people besides the registrants for the event. So I need to go from the mailing#show (which shows a preview of the mailing and will need to be able to add/remove recipients), loop through all of the recipients, and send the message that is in the mailing.message field.
I am so close to finishing this tool except for this mailing which I cannot wrap my head around. I see a lot of examples that create a mailer but I am not sure if that would work for me since the message are unique and it needs to get the message and subject from the mailer object. Any advice or guidance? I am really struggling to get this part done.
I assume you have a User model with an email column
I would setup an extra model i.e.
class PromoMail < ActiveRecord::Base
has_many :users # recpients
validates :body, presence: true
end
Then add a controller, where admins can create these and insert the Mail content in the body field and add recipients.
Then create a new method in your existing mailer to send the mail with the yielded body to one user.
Then add a action to the forementioned controller to send the PromoMail by looping over the associated users and call the ne mailer metod with each.
Couple of steps here, without going into much detail.
1) Make a rake task which looks for any emails which needs to be sent out, and sends them out. You might need to expand your schema to record whether a mail (or mailing or whatever) has been sent already. The rake task itself shouldn't have much code, it should just call a class method in eg User or Mailing or something.
You'll need to think about how the system can decide which emails need to get sent out. I find that flowcharts can be helpful here: it's going to involve iterating over all users, or all mailings, or something, and applying various logical tests to them. You may find that your current schema isn't up to the job, in which case expand it.
2) Schedule this rake task to be run at regular intervals, eg once a day or once a week. Various scheduled task runners are available, eg cron, or if you're on Heroku you'll need to use their Scheduler tool, for example.

Resources