I'm using ActionMailer. It's working fine.
But, I've noticed that the owner is clearly stated in header of email sent.
Just like this
Received: (from ftpuser#localhost)
What is it reffered to?
Indeed, some of my rails files's are ftpuser and rest are root.
How should it be?
This is an example and I hope that this will help, in your app/mailers/notifications_mailer.rb you can set default email address.
class NotificationsMailer < ActionMailer::Base
default :from => "noreply#yourdomain.net"
default :to => "info#yourdomain.net"
def new_message(message)
#message = message
mail(:subject => "[Message Confirmation] #{message.subject}")
end
end
Also check you config/environments directory. Mailers can also be configured there.
Related
I can't seem to find the shoppe tag in stackoverflow.
I'm using Shoppe gem for rails.
I want to know if there is a way to edit the views for the emails that are being sent when an order is placed in shoppe.
I would like to add an attachment to the email when you accept the order.
Thanks!
It seems that you can just re-define Shoppe's mailer method:
module Shoppe
class OrderMailer < ActionMailer::Base
def received(order)
#order = order
attachment(content_type: 'image/jpeg', body: File.read('image.jpg'))
mail :from => Shoppe.settings.outbound_email_address, :to => order.email_address, :subject => I18n.t('shoppe.order_mailer.received.subject', :default => "Order Confirmation")
end
end
end
Puts that somewhere into your app/initializers. Remember to set your content-type properly.
This is the best and fastest solution for anyone who just wanting to change the actual text like i did.
You can just go directly to the file of the email and edit it. all i did was visit The exact File Here and recreate that file and its path into my own application and changed the words to my pleasing.
https://github.com/tryshoppe/shoppe/blob/master/app/views/shoppe/order_mailer/accepted.text.erb
I'm trying to upgrade an application from rails 2.3 to 3.0.6
which has the following code in rails 2.3
class MessageSender < ActionMailer::Base
def send_message(subject,to,from,body,respondent = nil, content_type = 'text/plain')
#content_type = content_type
#subject = subject
#recipients = to
#from = from
# #sent_on = Time.now
#body = {:body_text => body}
end
end
In the upgrade process the code is modified as below
class MessageSender < ActionMailer::Base
def send_message(subjet,to,from,body,respondent = nil,content_type='text/plain')
mail(:to => to, :subject => subject, :from => from, :body => body, :content_type => content_type)
end
end
by referring to this famous blog about using ActionMailer in rails 3.0
And finally ran rake rails:upgrade:check(checks for rails 3 incompatible functions) and it shows
Old ActionMailer class API
You're using the old API in a mailer class.
More information: http://lindsaar.net/2010/1/26/new-actionmailer-api
The culprits:
- app/models/message_sender.rb
(i.e) It says I'm still using Old API
Can somebody explain what am I missing here ?
Or is there any other way to eliminate "You're using the old API in a mailer class" bug ?
FYI: the gems are updated and environment is ruby 1.8.7,rails 3.0.6
Try throwing your code away and write it again using ActionMailer guide. The reason can be as Frederick suggested but also your code doesn't look very rails 3 way ;).
The first thing that comes to my mind is how you pass body and content type. Body can be just variable which you will use in view and content type will be set automatically based on what views are defined.
I would write something like:
class MessageSender < ActionMailer::Base
def send_message(subject, to, from, body)
# #sent_on = Time.now
#body = body
mail(:to => to, :subject => subject, :from => from)
end
end
Then to render a view:
# app/views/message_sender/send_message.text.erb
My nice text email.
And my body is <%= #body %>
As you can read in the guide, you can also create html version as app/views/message_sender/send_message.text.erb.
Rails upgrade checks your code by running some regular expressions against it. This isn't foolproof, for example it's trying to guard against the old style way to set the subject:
subject "foo"
But the tests used to check for that will catch any instance of the word subject followed by a space (except when used as a symbol). Since you've got an argument call subject this could easily happen. The same is true of from.
I have tried to add it using the headers as
headers "References" => "<message_id>"
But it is not found in the generated email. Moreover i have tried to add the In-Reply-To header and it dosen't show up either. I am using rails 2.3.8.
Try this.
class UserMailer < ActionMailer::Base
def notification(user)
recipients user.email
from "notifications#example.com"
headers {"References" => "<message_id>"}
# code ...
end
end
Check the documentation here in guides for Rails for the syntax how to specify the headers.
I worked through some basic tutorials on Rails 3. The goal is a community-website on abilities and activities. I am using Devise for authentication. The creation of user profiles with avatars worked well (thanks to paperclip).
As a next step, I want to enable registered users to send an e-mail to a user from his (or her) profile page. I found a great tutorial on creating a contact form using Google Apps:
http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/
The mailer class in this tutorial looks like:
class NotificationsMailer < ActionMailer::Base
default :from => "noreply#youdomain.dev"
default :to => "you#youremail.dev"
def new_message(message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}")
end
end
My question: What is the best way to replace you#youremail.dev with the receivers E-Mail-Address? (from the User-Model)
Thanks in advance!
You can modify the new_message to accept the user (or list of users) to whom you want to send the email. Or an array of email addresses if you want to. Then pass the receiver's email address to the mail method as the :to option.
def new_message(receiver, message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}",
:to => receiver.email_address) # or something similar
end
Then you can invoke your mailer like this
NotificationEmail.new_message(a_user, a_message).deliver
To read the API see here or here (I prefer APIdock).
Also a more comprehensive guide on ActionMailer is available here. If you are new to Rails, you can find more guides here.
I saw this post but mine is slightly different:
Rails ActionMailer with multiple SMTP servers
I am allowing the users to send mail using their own SMTP credentials so it actually does come from them.
But they will be sent from the Rails app, so that means for each user I need to send their emails using their own SMTP server.
How can I do that?
Doing what is described in the other answer is not safe; you are setting class variables here, not instanced variables. If your Rails container is forking, you can do this, but now your application is depending on an implementation detail of the container. If you're not forking a new Ruby process, then you can have a race condition here.
You should have a model that is extending ActionMailer::Base, and when you call a method, it will return a Mail::Message object. That is your instance object and is where you should change your settings. The settings are also just a hash, so you can inline it.
msg = MyMailer.some_message
msg.delivery_method.settings.merge!(#user.mail_settings)
msg.deliver
Where in the above mail_settings returns some hash with appropriate keys IE
{:user_name=>username, :password=>password}
Here is a solution that I came up with based on the previous answers and comments. This uses an ActionMailer interceptor class.
class UserMailer < ActionMailer::Base
default from: proc{ #user.mail_settings[:from_address] }
class DynamicSettingsInterceptor
def self.delivering_email(message)
message.delivery_method.settings.merge!(#user.mail_settings)
end
end
register_interceptor DynamicSettingsInterceptor
end
For Rails 3.2.x
You can include AbstractController::Callbacks in your mailer class and the do a "after_filter :set_delivery_options" inside the mailer.
The set_delivery_options method would have access to instance variables setup by you in your mailer action and you can access the mail object as "message".
class MyNailer < ActionMailer::Base
include AbstractController::Callbacks
after_filter :set_delivery_options
def nail_it(user)
#user = user
mail :subject => "you nailed it"
end
private
def set_delivery_options
message.delivery_method.settings.merge!(#user.company.smtp_creds)
end
end
Since Rails 4+ it works to give the credentials directly via the delivery_method_options parameter:
class UserMailer < ApplicationMailer
def welcome_email
#user = params[:user]
#url = user_url(#user)
delivery_options = { user_name: params[:company].smtp_user,
password: params[:company].smtp_password,
address: params[:company].smtp_host }
mail(to: #user.email,
subject: "Please see the Terms and Conditions attached",
delivery_method_options: delivery_options)
end
end
See Sending Emails with Dynamic Delivery Options (Rails Guides)
Just set the ActionMailer::Base configuration values before each send action.
smtp_config = user.smtp_configuration
ActionMailer::Base.username = smtp_config.username
ActionMailer::Base.password = smtp_config.password
ActionMailer::Base.server = ..
ActionMailer::Base.port = ..
ActionMailer::Base.authentication = ..
in case somebody needs to set delivery method dynamically together with smtp credentials, u can use Mail::Message instance method to set delivery method together with it's variables so my addapted Aditya Sanghi version is:
class MainMailer < ActionMailer::Base
WHATEVER_CONDITION = true # example only f.e. #ser
include AbstractController::Callbacks
after_filter :set_delivery_options
private
def set_delivery_options
settings = {
:address => 'smtp.mandrillapp.com', # intentionally
:port => 587, # intentionally
:domain => 'your_domain', #insetad of localhost.localdomain'
:user_name => 'smtp_username',
:password => 'smtp_password',
:authentication => 'PLAIN' # or smthing else
}
if WHATEVER_CONDITION
message.delivery_method(:smtp, settings)
end
end
end