I have a site which has two textboxes. The first one is for email and the second for a message. When someone enters their email and a message and presses submit it shows the submitted message on the home page. Unfortunately, it doesn't send email to anyone.
Now I want to make it possible to write multiple emails and when you press submit what it does is show each email/message separately and not one message with 3-4 emails(whatever emails you typed). The message is the same of course. Here is my _forms code http://pastie.org/private/wg5naoay5hxbvxmnp02sbg
I found out that first I have to separate string into arrays with .split(',') method.
I am not sure if I should do that in _forms file or model? Also after I do that split method, how to make it write out same message for each email separately and not one message with all those emails? If you need code let me know.
Thanks
Put this method in Helper
def emails
emails_array= email_string.split(',')
Mailer.send_mail(emails_array)
end
Put this in Mailer
Class Mailer
def send_email emails
emails.each do |email|
##your sending email logic for each email address
end
end
end
Related
I need to send an exactly the same email to a bunch of users.
What I do now is:
emails.each { |em| MyMailer.letter(em).deliver_later }
The problem with that is that now for every single email, it will re-render the whole letter again and again (it's using the mjml to do this, so it's not fast). But I know that the email is exactly the same for all users, so can I sorta pre-render the template, so it only renders once?
Thanks!
Send it to multiple recipients using
mail(to: User.admin.pluck(:email), subject: "Hello Admins!")
This will send it to all the admins.
Check the doc for more info on this.
I have an app where users can sign up for workshops and admin has a possibility to write an e-mail to all the participants through the app. The fragment of code to send mail message to the group looks like this
workshop.students_all.each do |user|
WorkshopNotifyGroupMailer.notify_user(user, workshop, subject, body).deliver_later
end
so it's nothing extraordinary (User and Workshops are instances of models).
Now, I wanted to add one additional e-mail address to be sent each time a group is notified (just to have a copy how does the sent mail look like). I thought of doing it something like that (to keep the code short):
admin = OpenStruct.new(email: 'admin#email.com', first_name: 'Nameless') #These are fields taken from User instance by mailer
WorkshopNotifyGroupMailer.notify_user(admin, workshop, subject, body).deliver_later
Unfortunately, I receive "Unsupported argument type: OpenStruct" error. Is there a way to send an e-mail which uses an instance of a model using some kind of artificial structure? (In this case just assume admin is not on the user list and won't be)
I'm using ruby to send transactional emails via Mandrill.
As part of my product, I want to be able to send the same email to two recipients and have them see each other's email address.
(Like an introduction mail between two people).
So I filled he "to" field with both emails, and on my dashboard is seems that both are sent.
But unfortunately only one of the recipients receive the mail and the details of the second recipient is hidden.
In conclusion, I have two problems:
Only one recipient gets the mail
Hidden details of the second recipient.
I approached Mandrill support and this is what they replied:
If you'd like to enable this option globally for all of the messages you send, then you'll want to ensure that you have the
"Expose The List Of Recipients When Sending To Multiple Addresses"
option enabled in your Sending Defaults.
If, instead of making that change globally, you'd like to enable it
for individual messages, you'll want to use the
X-MC-PreserveRecipients (SMTP header), or the preserve_recipients (API
parameter), and set it to 'true'.
If you set this option to true, we'll expose the list of recipients to
each other as you would see happen when sending mail from a typical
email client program.
It worked!
If you want both recipients to be able to see each other, you can pass an array of e-mails in the to option.
If you do not want either of them to see each other, you can, in a loop over the users, send said e-mail.
If using ActionMailer it can be done like so:
mail(
to: ['person1#gmail.com', 'person2#gmail.com']
)
Or in a loop:
[user1, user2].each do |user|
UserMailer.some_email(user).deliver_now
end
mail(
to: user.email
)
Post your code, I have an idea of what your problem may be. Remember that a method in an ActionMailer class should only return mail() and must not be looped over inside of that method.
tldr: do everything unrelated to e-mail outside mailer, pass through necessary data as params to the method, end method with mail() call.
How can I send an asynchronous email with rails 4? By that I mean, when I click the 'send' button of a contact form, the page doesn't refresh, but the email is validated and sent, and a message is flashed to the user.
I've configured action_mailer correctly, and have a ContactForm mailer with one contact action that takes an email address as a parameter.
As a result,
ContactForm.contact("test#gmail.com").delivers #=> delivers email perfectly
But that's working on the command line. I don't really know the correct way to do this with a link. I mean, I could create a button that naviagates to send_email, and then I could have a route like this:
get 'send_email', to: 'contact#sendemail'
Then I would have a sendemail action which contains this method chain as shown above.
But this isn't asynchronous, and, also, I have no idea how I could validate the email's fields before sending the email, or highlighting invalid fields.
Is Ajax and JSON responses the key to highlighting the fields? What about the validation?
The resque_mailer seems to be a good way to send asyncronous emails. But why do I need this external gem when ajax is handled so well by vanilla rails?
The concept would be to have the form submit remotely. i.e submit to a create method in ContactsController. The method would then call a worker (resque/sidekiq) to send the email.
The create action can also respond to json. The json, response can either be a success or a fail (with errors).
On the AJAX success callback, you can trigger an alert, display div, or whatever notifying the user that the email was sent.
If the json results are returned with erros, then you can display the error message via JS.
This Railscasts Episode #171 Demonstrates sending emails using a background process with the help of DelayedJob
I'm using the following code to retrieve emails from my Gmail inbox.
def get_mail
Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
Net::POP3.start('pop.gmail.com', 995, "uname","pass") do |pop|
unless pop.mails.empty?
pop.each_mail do |mail|
email = TMail::Mail.parse(mail.pop)
email_obj=EmailedQueries.new
email_obj.save_email(email.from,email.subject,email.body_html)
end
end
end
end
This works just fine, but it retrieves only new mails from the inbox. Instead, I want a seperate function that will retrieve ALL emails from the inbox. This function will be used rarely. I wont be retrieving all mails all the time. Only when necessary.
Thanks!
You'll need to configure POP settings in GMail.Enable "Pop for all mail" and you're good to go !