Blankman
Blankman

Reputation: 267040

Creating easy to use parameters for this method

I have a API that I am using that I want to create a helper method.

def send_email( ??????????? )
   client = Mailgun::Client.new
   builder = Mailgun::MessageBuilder.new

   builder.set_from_address("[email protected]", {"first" =>"John", "last" => "Doe"});

   builder.set_subject("hello world")
   builder.set_text_body("this is the body")

   client.send_message(domain, builder)
end

I want to use a params hash, but not sure how I can embed the "from address" first and last name in it:

message_params = { from: 'bob@sending_domain.com',
                    to:   '[email protected]',
                    subject: 'The Ruby SDK is awesome!',
                    text:    'It is really easy to send a message!'
}

Is there a way I can embed the first/last in the params?

Upvotes: 0

Views: 36

Answers (1)

Nic Nilov
Nic Nilov

Reputation: 5156

Sure, here is one way to do it:

message_params = {
  from: {
    email: 'bob@sending_domain.com',
    name: {
      'first' => 'Bob',
      'last' => 'Doe'
    }
  },
  to:   '[email protected]',
  subject: 'The Ruby SDK is awesome!',
  text:    'It is really easy to send a message!'
}

Then, in your send_mail method:

def send_email(params)
  # ...

  builder.set_from_address(params[:from][:email], params[:from][:name]);

  # ...
  client.send_message(domain, builder)
end

Upvotes: 2

Related Questions