1. Deploying a Ruby App With SendGrid and Engine Yard
  2. Deploying a PHP App With SendGrid and Engine Yard

Deploying a Ruby App With SendGrid and Engine Yard

Create/Modify Your Code for Engine Yard

If you are using Ruby without a framework, please utilize these instructions.

For email sending only, you can utilize Action Mailer, or for more advanced functionality, you can use this Ruby gem.

We have a sample app on Github that demonstrates sending an email via SendGrid using ActionMailer and receiving an email using our Inbound Parse webhook.

Create/Configure Your Engine Yard Instance

  1. Create a git repository or utilize our sample app.
  2. Create a new Engine Yard Cloud app, making sure you choose Ruby.
  3. Configure the application.

    Configure the application

  4. Next you will setup your Git deploy keys.
  5. Now, create your app’s environment. I changed the Runtime to Ruby and the Database stack to MySQL 5.5.x for use with our sample app.
  6. Setup your environment and click boot this configuration.

    Setup your environment

  7. Scroll to the bottom, and click new Add-on.

    New Add-on

  8. Select SendGrid. If this is your first time adding SendGrid to this account, you’ll need to setup SendGrid. Follow the prompts and select your plan.
    • After configuring SendGrid, you’ll need to go through the process of adding a new Add-on once again, in your environment, select “New Add-on”
  9. Select SendGrid. Once on the setup page, chose the environment you wish to add SendGrid.

    Select an Environment to Add SendGrid

  10. Configure ActionMailer as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    config.action_mailer.delivery_method = :smtp
    config.action_mailer.perform_deliveries = true
    config.action_mailer.raise_delivery_errors = true
    config.action_mailer.smtp_settings = {
         :authentication => :plain,
         :address => "smtp.sendgrid.net",
         :port => 587,
         :domain => EY::Config.get('base', 'domain_name'),
         :user_name => EY::Config.get(:sendgrid, 'SENDGRID_USERNAME'),
         :password => EY::Config.get(:sendgrid, 'SENDGRID_PASSWORD')
    }
    
  11. Now you are ready to start sending emails from your app using SendGrid!
  12. If you are implementing our sample app, which receives emails, you will need to setup your Inbound Parse settings.

Deploying a PHP App With SendGrid and Engine Yard

Create/Modify Your Code for Engine Yard

  1. Engine Yard instances come with Composer, a tool for PHP Package Management, preinstalled. To take advantage of this create a file composer.json in the root of your project with the following JSON:
    1
    2
    3
    4
    5
    
    {
      "require": {
        "sendgrid/sendgrid": "~1.0.0"
      }
    }
    
  2. On deploy, Composer will create a directory vendor with the file autoload.php in it. Require this in any script you wish to use SendGrid (or any other Composer package):
    1
    
    require 'vendor/autoload.php';
    
  3. Once you’ve loaded the SendGrid library, you may initialize SendGrid. Engine Yard has a handy way to use environment variables, by accessing the $_SERVER superglobal in PHP. Take advantage of this so as not to add your SendGrid credentials to your code:
    1
    
    $sendgrid = new SendGrid($_SERVER['SENDGRID_USERNAME'], $_SERVER['SENDGRID_PASSWORD']);
    
  4. Now you may use SendGrid as you would in any other environment.
    1
    2
    3
    4
    5
    6
    7
    8
    
    $mail     = new SendGrid\Mail();
    $mail->addTo('foo@bar.com')->
           addTo('dude@bar.com')->
           setFrom('me@bar.com')->
           setSubject('Subject goes here')->
           setText('Hello World!')->
           setHtml('<strong>Hello World!</strong>');
    $sendgrid->smtp->send($mail);
    

Create/Configure Your Engine Yard Instance

  1. Create a new application. Confirm the language is PHP. Engine Yard requires your Git repository to be hosted elsewhere. You can deploy using our sample app (on GitHub) or your own.

    Creating an Engine Yard Application

  2. Next, create a new environment for your application. If you’re using our sample app, ensure your database is set to MySQL.

    MySQL

  3. Choose your configuration and hit “Boot”
  4. In the Add-ons section of the environment, click “New Add-on”

    New Add-on

  5. Select SendGrid. If this is your first time adding SendGrid to this account, you’ll need to setup SendGrid. Follow the prompts and select your plan.
    • After configuring SendGrid, you’ll need to go through the process of adding a new Add-on once again, in your environment, select “New Add-on”
  6. Select SendGrid. Once on the setup page, chose the environment you wish to add SendGrid.

    Select an Environment to Add SendGrid

  7. Once you’ve added SendGrid to your environment, return to the Environment’s dashboard. SendGrid should now be in the Add-ons Section, take note of the Keys/Variables (they may initially be hidden).
  8. Under Application Instances, find your application instance, and click SSH. This will launch your SSH client. (For this to work, you must have provided Engine Yard with your public key, _before_ booting the instance, if you have not already, provide Engine Yard with your Public Key, edit your environment to include it, and hit apply.)
  9. Now you must edit your environment variables, they’re stored in a shared file. To edit them: nano /data/YOUR_APP_NAME/shared/config/env.custom add the lines:
    1
    2
    
    env[SENDGRID_USERNAME] = YOUR_SENDGRID_USERNAME
    env[SENDGRID_PASSWORD] = YOUR_SENDGRID_PASSWORD
  10. Hit Control-X, and confirm you wish to save changes by hitting Y, then confirm the file name by pressing Return. You may now logout of the SSH session.
  11. You can now navigate to the instance via HTTP and you should be good to go.