Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 4

Project Name - mailing

Thisisasimpleapplicationsimilartoourpreviousproject
SimpleApplication_BlogbuthereweaddaextrafeatureAnewuserputs
his/hernameandemailaddressandsubmitsit.Thenaconfirmationmailgetssent
totheusersemailid.ActionMailerallowsyoutosendemailsfromyour
applicationusingamailermodelandviews.
FirstcreateanewrailsappcalledMailing,likewedidbefore,andthenswitchto
therootdirectoryofthatapp.
Then,likeSimpleApplication_Blogprojectwegeneratescaffold.
$railsgscaffoldusersname:stringemail:string
andthenrunthecommand
$rakedb:migrate
Now,Wejustdosomeextrasteps

1.Intheconfig/initializersfolderwecreateafilesetup_mail.rbandaddsome
code.(Seethefilefordetails).

2.AddmailgemtoourGemfile
gemmail

3.Runthecommand
$bundleinstall

4.Noweverythingisconfiguredproperly,wegenerateanewmailer

$railsgmaileruser_mailer
Itcreatedanewuser_mailerclassatapp/mailersdirectory.
5.app/mailers/user_mailer.rbcontainsanemptymailer.Letsaddamethodcalled
registration_confirmation,thatwillsendanemailtotheusersregisteredemail
address(Seethefilefordetails).
6.Wecancreateanewviewforemailundertheapp/views/user_mailerdirectory.
Wecallitregistration_confirmation.text.erb.Wecansayanythingthatwewant
anditappearasthebodyoftheemail.
7.Forsendingthemail,weaddalineinusers_controller.rbfile(Seetheproject
filefordetails.)
UserMailer.registration_confirmation(@user).deliver

8.Wecanspecifyrootofoursitebymodifyingroutes.rbfile.

9.NowIfyourunthecommand
$railsserver
andinyourbrowseraddressbartypetheurlhttp://localhost:3000.youcansee
ourapp.

Someimportantthings
>>delivermethodsendstheemail
>>mail(headers={},&block)public

Themainmethodthatcreatesthemessageandrenderstheemailtemplates.There
aretwowaystocallthismethod,withablock,orwithoutablock.
Bothmethodsacceptaheadershash.Thishashallowsyoutospecifythemostused
headersinanemailmessage,theseare:
:subjectThesubjectofthemessage,ifthisisomitted,ActionMailerwillask
theRailsI18nclassforatranslated:subjectinthescopeof[mailer_scope,
action_name]orifthisismissing,willtranslatethehumanizedversionofthe
action_name
:toWhothemessageisdestinedfor,canbeastringofaddresses,oranarrayof
addresses.
:fromWhothemessageisfrom
:ccWhoyouwouldliketoCarbonCopyonthisemail,canbeastringof
addresses,oranarrayofaddresses.
:bccWhoyouwouldliketoBlindCarbonCopyonthisemail,canbeastring
ofaddresses,oranarrayofaddresses.
:reply_toWhotosettheReplyToheaderoftheemailto.
:dateThedatetosaytheemailwassenton.
Youcansetdefaultvaluesforanyoftheaboveheaders(except:date)byusingthe
defaultclassmethod
>>ThegeneratedmodelinheritsfromActionMailer::Base
>>smtp_settings
Allowsdetailedconfigurationfor:smtpdeliverymethod:
:addressAllowsyoutousearemotemailserver.Justchangeitfromitsdefault
"localhost"setting.
:portOntheoffchancethatyourmailserverdoesn'trunonport25,youcan

changeit.
:domainIfyouneedtospecifyaHELOdomain,youcandoithere.
:user_nameIfyourmailserverrequiresauthentication,settheusernameinthis
setting.
:passwordIfyourmailserverrequiresauthentication,setthepasswordinthis
setting.
:authenticationIfyourmailserverrequiresauthentication,youneedtospecify
theauthenticationtypehere.Thisisasymbolandoneof:plain,:login,
:cram_md5.
:enable_starttls_autoSetthistofalseifthereisaproblemwithyourserver
certificatethatyoucannotresolve.

You might also like