how to send email in simplest way using ruby and rails - ruby-on-rails

require 'net/smtp'
Net::SMTP.start('host', 25) do |smtp|
smtp.send_message "Subject: testing from ruby", 'from-abc#sol.com', ['to-abc#sol.com']
render :text=> "email sent"
end
my code look like this but this is not working

You can try Pony gem
https://github.com/benprew/pony

I'd propose a good old RTFG, the rails guides are pretty good.

Here's A simple Ruby method to send email, which I tried and it works fine.

I second this answer.
Of course you have to change the opts[:server] ||= 'localhost' line to something that actually provides an SMTP service (smtp.yourdomain.com) and that accepts mails from your host without smtp-auth or similar.
You may also install an SMTP server on your localhost (Postfix/qmail/exim... whatever) to serve this purpose.
Otoh: This is indeed not the "Rails" way to send email. Please have a look how to properly setup/use ActionMailer (this answer)

Related

Rails Heroku Sendgrid send to one address in staging

We are using Heroku (Staging and Production) with Sendgrid for a Rails 3 app.
Is there a way to have all the emails generated in Staging re-directed to one address. When testing, we don't want to actually do things like email invoices to our customers.
If not, would we need to program the logic into the mailer?
Thanks for the help!
You can hardcode the 'to' address in the mailer file.
mail(to: "youremail#domain.com", subject: "A new comment has been added to #{list}", from: "some#thing.com")
You can also use the recipient_interceptor gem.

MailBoxer: How should I do make it to send a notification through email?

I am studying a project vish, which has an open source e-Learning platform http://vishub.org, in here, if some one comments another one's Excusion, one email will be sent. This ability is finished by using mailboxer.
But when I run a instance of vish locally, for validate mailboxer, I used a task sending some messages between all users, it worked fine, and I can find the new messages in the header's notification dropdown menu, but I did not get a email when I commented some one Excursion like in vishub, and did not get any error message about mailboxer. According to my newbie experience, I guess that I should config a mail server, but I don't know how to config a mail server for mailboxer, it seems that there is no one discuss about it?
What should I do for enabling mailboxer email? Or any manual should I read? Or my question is not relative to mailboxer? Thanks in advance!
Updated:
According to #rick's reminding, I read some code of mailboxer:
class NotificationMailer < ActionMailer::Base
end
It make me clearer about the relationship between ActionMailer and MailBoxer!
Email wont't be sent in development unless you set config.action_mailer.perform_deliveries = true in environments/development.rb. and also you should use ActionMailer to send email locally. For more information, you can refer this link mailboxer

How do I make ActiveMailer always mail me?

I use ActiveMailer with a 3rd party mailing provider. To develop my app, I want to actually see the emails that come in, as a user would, in my email client.
So in development mode, instead of disabling email, I want my app to send the mails, but change the "to" field so that every email is sent to me. Is that possible?
Update: I want to test the full route my email takes: going through my ESP, arriving in my inbox, viewing it in gmail. I'm not looking to just test that an email is created.
I personally recommend letter_opener by Ryan Bates, however, if you actually want to deliver the mail instead of just viewing it in the browser, there are a number of plugins available that others have already listed. No one, however, has mentioned that you can very easily accomplish this using Interceptors.
Create a new initializer in your config/initializers directory in your Rails app:
# config/initializers/development_mail_interceptor.rb
class DevelopmentMailInterceptor
def self.delivering_email(message)
message.subject = "[#{message.to}] #{message.subject}"
message.to = "YOUR_EMAIL#gmail.com"
end
end
ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
This leverages the power of an interceptor on your app. It doesn't configure anything, but rather changes the envelope on the message, altering the to and subject fields. Replace YOUR_EMAIL with the correct value.
The self.delivering_email(message) method is invoked by ActionMailer. You are hooking into that method and override the message envelope.
Finally, you register that interceptor iff we are currently in the development environment.
Be sure to restart your server, and all your mail (in Development) will actually be sent to your email.
Save yourself some trouble and run MailCatcher. MailCatcher is a simple SMTP server that just grabs outbound email and gives it to you in a simple web interface. Install MailCatcher, add this to your environments/development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :host => 'localhost', :port => 1025 }
Start MailCatcher when you start your Rails server (or use Foreman or something similar to deal with it), and then go to http://localhost:1080/ to see the email that your application is sending out.
You may consider checking out something like MockSMTP (OS X); instead of modifying your "to" fields, you instead set the mail server for dev mode to the "fake" SMTP server created by the app, and from then on ALL emails (sent to anyone) go instead to the app.
I've never used it myself, but I remember seeing that the devs at 37signals use it.
On other operating systems, you may consider one of the following projects:
letter_opener by Ryan Bates - popup a new browser window when an email is sent
MailCatcher (mentioned by mu is too short) - runs a fake SMTP server and a web-based interface for viewing mail sent to it
mailtrap - similar to MockSMTP, has both a mock SMTP server and also a separate viewer program
As much as I like this answer, I went with a different option.
Use the mail_safe gem! As well as providing the functionality from sethvargo's answer, it doesn't require any work other than adding the gem, and it automatically figures out who to email from their .gitconfig.
One important note that I rarely saw mentioned when researching this is that you must use deliver, not deliver!. The latter doesnt call interceptors (though apparently it still calls observers, if that's helpful).

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!)

skip confirmation e-mail in development with devise

I am using devise and I want to be able to skip the confirmation e-mail while I am in development mode. Since my application can't send e-mail while running on my local machine, I will just get the alert message saying that you need to be confirmed before accessing the application.
Devise has also a method skip_confirmation! that you can call on the model before saving it to avoid sending the mail. It simply sets the confirmed_at attribute which results in skipping the mail sending.
try Letter Opener gem from Ryan Bates
https://github.com/ryanb/letter_opener
it will open the email in the browser without sending it. You don't want to be skipping stuff out if you're in development because stuff will get missed/forgotten about.
As simple as that
user=User.new
user.email="you#email.id"
user.password="yourPassword"
user.skip_confirmation!
user.save
Or you could try MailCatcher in your development environment.
If you want a really light weight way to do this, look in your terminal after registering - the rails output shows the email that got sent, you can copy-paste the confirmation link, confirming the account and then continue testing.
crude, but effective.
In Rails 3 you can use an "interceptor" to reroute your development emails as described in Railscast 206.
Devise uses ActionMailer to send emails. In test mode, ActionMailer shouldn't actually send any emails.
Check out this tutorial for an explanation on ActionMailer and testing environments.
So, depending on the environment, you can basically turn delivery off, while not affecting your actual tests. You just have to specify that option in the environments/test.rb file.
Take your model for devise. Commonly Its user.rb. And remove or comment the config comfirmable.
This will prevent the process of confirmation
simmilar to letter_opener gem (recommended by John Beynon above) there is a gem called mailcatcher which works on SMTP level.
basically you configure SMTP on your Rails app to point to port under which mailcatcher run on local machine and you have mailcatcher browser on other port to read the emails
more info https://github.com/sj26/mailcatcher

Resources