I haven't been on the net for a week so I'm a bit late. But this is how I did mine. It is a bit more verbose but the benefit is that it can be used to send HTML email as well as normal plain text email.
First I generate a model called Notifier with
ruby script/generate mailer Notifier multipart_alternative
Next I create an action for mailing in my controller
def my_mailerfrom_name = params[:comment][:name]
from_email = params[:comment][:email]
the_subject = params[:comment][:subject]
message = params[:comment][:message]
begin
#First check if the senders email is valid
if from_email =~ /^[a-zA-Z0-9._%-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/
#put all the contents of my form in a hash
mail_info = {"from_name" => from_name, "from_email" => from_email, "message" => message, "the_subject" => the_subject}
#Call the Notifier class and send the email
Notifier.deliver_multipart_alternative(mail_info)
#Display a message notifying the sender that his email was delivered.
flash[:notice] = 'Your message was successfully delivered.'
#Then redirect to index or any page you want with the message
redirect_to(:action => 'index')
else
#if the senders email address is not valid
#display a warning and redirect to any action you want
flash[:warning] = 'Your email address appears to be invalid.'
redirect_to(:action => 'index')
end
rescue
#if everything fails, display a warning and the exception
#Maybe not always advisable if your app is public
#But good for debugging, especially if action mailer is setup wrong
flash[:warning] = "Your message could not be delivered at this time. #$!. Please try again later"
redirect_to(:action => 'index')
end
end
Next I add the following to the file, notifier.rb in app/models
#this is the method that actually sends the email
def multipart_alternative(mail_info) @from = mail_info["from_email"]
@recipients = "youremail.com"
@subject = mail_info["subject"]
@mail_info = mail_info
@body["mail_info"] = mail_info
@sent_on = Time.now
part :content_type => "text/plain",
:body => render_message("multipart_alternative_plain", "mail_info" => mail_info)
part :content_type => "text/html",
:body => render_message("multipart_alternative", "mail_info" => mail_info)
end
Next you create the views for your email. You will need to create two views, one is multipart and the other is plain. This is done under app/views/notifier
So the first file, multipart_alternative_plain.rhtml could be:
Hello BaltarYou have recieved a message from <%= @mail_info["from_name"] %> about <%= @mail_info["the_subject"] %>
His message reads thus:
<%= @mail_info["message"] %>
Please consider it and reply to <%= @mail_info["from_email"] %>
Best Wishes,
Your Ruby Slave.
Then for the other file multipart_alternative.rhtml you could write:
<h3>Hello Baltar</h3><p>
You have recieved a message from <%= @mail_info["from_name"] %> about <%= @mail_info["the_subject"] %>
</p>
<br/>
His message reads thus:
<br/>
<p>
<%= @mail_info["message"] %>
<p>
Please consider it and reply to <%= @mail_info["from_email"] %><br/>
Best Wishes,<br/>
Your Ruby Slave.<br/>
Finally the view for sending emails in your application. If added some javascript to verify if the contents of the form are filled in so you don't get blank emails. Also the input type being used is a button so that it can verify before submitting the form.
<script>
function checkForm(){
var _1=document.getElementById("comment_email").value;
var _2=document.getElementById("comment_name").value;
var _3=document.getElementById("comment_message").value;
var _4=document.getElementById("comment_subject").value;if(_1.length==0||_2.length==0||_3.length==0||_4.length==0){
alert("You must fill every part of this form to continue");
}else{
document.getElementById("contact_form").submit();
}
}
</script>
<% if flash[:notice] %><div class="green flash"><b>Success:</b> <%= flash[:notice] %></div><br/><% end -%>
<% if flash[:warning] %><div class="red flash"><b>Error:</b> <%= flash[:warning] %></div><br/><% end -%>
<form action="/my_mailer" method="post" id="contact_form">
<p>Your name <br/>
<%= text_field 'comment', 'name' , :size => 36 %></p>
<!-- You might want to make subject a drop down select box -->
<p>Your inquiry type <br/>
<%= text_field 'comment', 'subject' , :size => 36 %></p>
<p>Your Email (is validated)<br/>
<%= text_field 'comment', 'email' , :size => 36 %></p>
<p>Your Message <br/>
<%= text_area 'comment', 'message', :cols => 42, :rows => 9 %></p>
<br/><br/>
<!-- Before you submit, check if the values are there -->
<%= submit_tag 'Report Error', :onclick => 'checkForm();' , :type => 'button' %>
</form>
Hope it wasn't too verbose. Of course you have to configure actionMailer and whatnot in environment.rb.
Last edited by daibatzu (2006-08-21 14:37:31)