Rails: test mailgun on localhost - ruby-on-rails

I have a Rails app running on Heroku that uses Mailgun to process incoming emails. I haven't been able to figure how I can debug my email processing locally (on localhost) instead of having to push everything up to heroku every time I make a change. (this is just a test app - I'm the only one using it)
Is it possible to work with Mailgun locally? If so, how do I go about it?
Thank you in advance

Mailgun gives you the option to store a message for later retrieval. If you configure it that way, you'll be able to fetch messages from development for processing without having to set up a publicly-accessible webhook for Mailgun to hit.
But I'm assuming you have production configured with an HTTP endpoint, and it's no fun to do things differently between environments. There are a few tools that will let you set up a public endpoint that routes to localhost:
ngrok, which I've used to good effect to test Twilio. You can set up a permanent subdomain so you don't have to constantly change your Mailgun configuration.
UltraHook, which I haven't personally used, but looks the same.
Localtunnel which looks easiest to start up, but like you get a different host at every boot.
If you have a permanent publicly-accessible server, you can also maintain your own tunnel.

mailgun provides a sandbox that you can use for localhost the only downside to this is that you have to add the test email to valid recipient.

using this gem might be another possible solution:
https://github.com/ryanb/letter_opener/ or https://github.com/fgrehm/letter_opener_web for more advanced features
follow installation from repo
mail will open in new tab

Related

Sendgrid receive email locally

I am trying to implement sendgrid parse API using the 'griddler' gem in my rails application. The problem is how can I test it locally? And how to receive email in my local machine.
The are a couple ways to test SendGrid's Parse Webhook locally.
The easiest is to just simulate the Webhook by POSTing data to your endpoint yourself (via cURL or some other mechanism, like Postman [example]). The SendGrid Parse Webhook Docs has an example of the payload that will be posted to your server, so you may mimic that.
The second option, which allows for end-to-end testing rather than just simulation is by creating a tunnel your local machine and provide SendGrid with the URL of the tunnel. There are a number of free services that allow you to do just that very easily:
https://ngrok.com/
http://progrium.com/localtunnel/
If I may self-promote, I created a Ruby gem that does this:
https://github.com/ccallebs/pokey-sendgrid
It runs alongside your server and generates SendGrid webhook requests at regular intervals.

emails sent from production server end up in spam

I use sendmail to send emails from my application. I always send the emails from SOME_NAME#MY_DOMAIN.com but they always endup in spam folder.
I know that I should do some stuff on the DNS side to make my emails be marked as non-spam, but I don't know what they are.
I am a newbie and this is my first time setting up a production server, a domain and everything else myself. I appreciate if someone helps me.
What sort of environment are you deploying to?
This frequently happens to applications deployed to cloud services, like Amazon or RackSpace. Their entire IP blocks are registered as spam at services like Spamhaus, which is a sensible precaution or else we'd be getting even more spam than usual. You should enter your server's IP address in that field to see if you're listed as a spammer.
If you are, you can request to Spamhaus that the block be lifted. Getting in touch with Amazon's support stuff also helps. Finally, you can get around the issue entirely by using a sendmail service of some sort -- Amazon SES is pretty good, and there's even a Gem out there that provides integration to Rails apps.

Any examples of running an smtp server with Rails?

Has anyone seen any examples of how to run an smtp server with Rails hosted on heroku?
I'm interested to send mail to my own smtp server, manipulate the content with rails by adding a footer, adding link tracking etc, then send it out again.
Once I have the data, easy. Not sure where to begin in terms of setting up smtp.
Alternatively, are there any smtp services out there that will simply POST the data to my app?
Take a look at SendGrid here http://sendgrid.com/
They've got some great APIs, and should be able to enable what you want.
If you want inbound email via a post to your application look at cloudmailin.com
It's python, not RoR, but the Lamson project is pretty close to what you are talking about.
http://lamsonproject.org/
(Thanks Zed!)

How to test sending emails from asp.net on development machine?

How do I accomplish this? The SMTP class throws error on dev machine about not finding an SMTP server. Is there a way to test sending emails on development machine?
Shawn,
Straight from my web.config:
<smtp deliveryMethod="SpecifiedPickupDirectory">
<network host="ignored" />
<specifiedPickupDirectory pickupDirectoryLocation="c:\email_c#" />
</smtp>
this works fine insofar as being able to review the 'emails' that get saved into the pickupDirectoryLocation directory.
Give it a try...
You can dump the files on disk, by configuring System.Net.Mail.SmtpClient to use a deliveryMethod of type SpecifiedPickupDirectory, I found an example on SO
I know this is an old thread, but I just stumbled upon this service:
http://mailtrap.io/
Which is friggin brilliant. It serves as a dummy SMTP server for your app, and doesn't actually send the emails, but allows you to easily view them in the browser (or via API).
It's killer.
There's a couple possible reasons for this.
1) Is your code configured to use local SMTP server during development and you've upgraded to windows 7? There's no longer a SMTP server available on your localhost. Find and download smtp4dev to allow your localhost to trap the sent Emails.
2) If you are using a remote SMTP server, check your windows firewall to confirm that you are allowed to send outgoing mail. If you are, then confirm that your machine/username has rights to send mail via that server. A quick telnet:25 to the server should let you know if your connection is refused or not.
Assuming by "test sending emails" you mean sending test emails instead of formal/unit testing, I like to use smtp4dev:
http://smtp4dev.codeplex.com/
As the page explains, it's a dummy SMTP server, basically intercepting your outgoing messages from your app, allowing you to examine those messages and make sure everything works as you expect. It's a Windows app, which hopefully isn't an issue if you're developing for ASP.NET.
I usually do this by creating a wrapper class for the SmtpClient, then mocking out the wrapper in my tests. This removes the actual mail client/server dependencies from my unit tests. The wrapper itself is relatively thin so I don't feel the need to create tests for it. Usually I do my integration level testing for things like this as exploratory tests in my staging environment. The staging environment typically uses a production mail server, but with "fake" data -- e.g., customer email addresses replaced with my own.
Having said that, I would expect the client to work without errors even on your development system unless your mail server is protected by a firewall or something that would prevent your dev system talking to it. Can you give more detail on what the error you are experiencing?
Without seeing the exception there's not much we can do. As long as the details on your dev machine are pointing to a proper smtp server and have the correct credentials then your code won't be the issue and you should look further down the chain. I had an exception of the target machine refusing the request despite everything else being right. After spending ages double and triple checking the credentials, sending from our server etc I tracked the bug down to McAfee blocking email port 25...

send email localhost

I'm developing rails on localhost and wondering what is the simplest way, in terms of workflow, of building email functionality in.
So I figure:
Get mail working on localhost.
Then when I deploy on a production server just change the smtp settings.
Is this a reasonable approach? Can you actually send email from localhost? Know of any tutorials on email in rails?
E-mail/SMTP should work fine from any host connected to the internet with access to a DNS server.
I don't see anything wrong with your approach.
agree. Using SMTP is a quick and easy way to get mail up and running on your localhost dev box.

Resources