The ramblings of Ivar Abrahamsen at flurdy.com. Contain ideas, ranting at innocents, blinkered sporting opinions, tech bable, and probably not enough to be interesting.
Showing posts with label heroku. Show all posts
Showing posts with label heroku. Show all posts
Wednesday, 30 January 2013
Heroku commands & tips
I wrote a quick howto of Heroku commands and tips I use all the time.
It covers creating instances, configurations, common add-ons such as databases & email, deployment strategies and more.
As mentioned in the document I tend to have multiple applications per project, so I always append --remote staging for example, resulting in this long deploy command:
git push staging master && \
heroku logs -t --remote staging; \
heroku open --remote staging && \
heroku logs -t --remote staging
For the eagle eyed you will have noticed the ";" which means as the log tailing never really finishes, when you notice the app is up and running you have to manually end it with control+c to proceed with opening up a browser.
Obviously these commands ties nicely into my use of Play on Heroku and the howtos I wrote on integrating both.
They are perhaps obvious commands and basic but I hope they are of use to some people.
Wednesday, 23 May 2012
Send email via SendGrid on Heroku using Play! 2.0 with Scala
If you have a Play! framework 2.0 application that you want to send email from, here are a few tips.
These tips assumes you deploy to Heroku, but other platforms should work similarly. The examples here are using Scala, but Java should work along similar lines. Finally the specifics are for the SendGrid add-on for Heroku, but other mail server providers should be fine.
First add the free option of the SendGrid add-on to your Heroku app by typing in:
Then configure your Play! app to use the mail plugin provided by Typesafe:
Add to your dependencies in the project/Build.scala file: (all on one line)
Next configure the mail server settings. You can either add these directly to your conf/application.conf file, but I prefer to share my projects' source code, so my production settings are set via environment variables so that my username/password are not publicly available.
However for the plugin to run smpt.host must be present. Open conf/application.conf and add:
On Heroku I append the settings to the Heroku's propriatory Procfile file. I append these settings to the Procfile to use SendGrid's servers: (all on one line)
The SendGrid add-on should create the environment SENDGRID_USERNAME and SENDGRID_PASSWORD variables for you.
You can verify this with:
Finally we then create our actual application code to send email:
This should be all that is needed.
Play! 1.x did have a handy mock email interface for development and testing. I will try and find a suitable replacement for 2.0 and update this post when I do.
For more information
These tips assumes you deploy to Heroku, but other platforms should work similarly. The examples here are using Scala, but Java should work along similar lines. Finally the specifics are for the SendGrid add-on for Heroku, but other mail server providers should be fine.
First add the free option of the SendGrid add-on to your Heroku app by typing in:
heroku addons:add sendgrid:starter
Then configure your Play! app to use the mail plugin provided by Typesafe:
Add to your dependencies in the project/Build.scala file: (all on one line)
"com.typesafe" %% "play-plugins-mailer" % "2.0.2"
Then create and add these to a conf/play.plugins file: (all on one line)
1500:com.typesafe.plugin.CommonsMailerPlugin
Next configure the mail server settings. You can either add these directly to your conf/application.conf file, but I prefer to share my projects' source code, so my production settings are set via environment variables so that my username/password are not publicly available.
However for the plugin to run smpt.host must be present. Open conf/application.conf and add:
smtp.host=mock
On Heroku I append the settings to the Heroku's propriatory Procfile file. I append these settings to the Procfile to use SendGrid's servers: (all on one line)
-Dsmtp.host=smtp.sendgrid.net -Dsmtp.port=587 -Dsmtp.ssl=yes -Dsmtp.user=$SENDGRID_USERNAME -Dsmtp.password=$SENDGRID_PASSWORD
You may already have other settings in the Procfile, e.g. database URL, so be aware of the 255 char limit, and use a custom properties file instead.web: target/start -Dhttp.port=${PORT}
-Dconfig.resource=heroku-prod.conf
The SendGrid add-on should create the environment SENDGRID_USERNAME and SENDGRID_PASSWORD variables for you.
You can verify this with:
heroku config
Finally we then create our actual application code to send email:
package notifiers(In case of this posts cant parse tags replace < and > with less than and greater than tags;)
import com.typesafe.plugin._
import play.api.Play.current
import play.api.Play
import play.Logger
object EmailNotifier {
def sendMail {
val mail = use[MailerPlugin].email
mail.setSubject("Mail test")
mail.addRecipient("Joe Smith <joe@example.com>","sue@example.com")
mail.addFrom("Jon Doe <joe@example.com>")
mail.send( "Test email" )
}
def sendTestMail {
if(Play.current.mode == Mode.Prod){
Play.current.configuration.getString("smtp.host") match {
case None => Logger.debug("Email mock")
case Some("mock") => Logger.debug("Email mock")
case _ => sendMail(participant)
}
} else {
Logger.debug("Email mock")}
}
}
This should be all that is needed.
Play! 1.x did have a handy mock email interface for development and testing. I will try and find a suitable replacement for 2.0 and update this post when I do.
For more information
- Play! 2.0: http://www.playframework.org/
- Play! 2.0 docs: https://github.com/playframework/Play20/wiki
- Heroku and SendGrid: https://devcenter.heroku.com/articles/sendgrid
- Mail plugin for Play! 2.0: https://github.com/typesafehub/play-plugins/tree/master/mailer
- Heroku environment variables: https://devcenter.heroku.com/articles/config-vars
Subscribe to:
Posts (Atom)