Tuesday, April 19, 2016

ngOptions that works - Populating select boxes using AngularJS

A quick post explaining how to populate select box using AngularJS with the values that you really want, I’ve found that the way to achieve that is not too obvious in the official documentation for ngOptions.


Let’s assume that you’ve got your data from the server or whatever, it’s a JSON array that looks like this

items = [
          {“itemId”:1, “itemName”:”Item 1”},
          {“itemId”:2, “itemName”:”Item 2”}
        ];


You can populate the values by using this syntax

<select ng-options="item.itemId as item.itemName for item in items">
  <option value="">Choose an item</option>
</select>


This way the value will be the “itemId” and “itemName” is the displayed name.

The empty option is used as a placeholder.

That’s it.

Tuesday, April 12, 2016

Sending emails through gmail smtp - Laravel

In this post I'm going through a couple of problems that I've faced while trying to send emails from my web application hosted on Google Cloud (Compute engine), the Laravel version that I'm using is 4.2 .

Swift_TransportException: 

Expected response code 250 but got code "", with message "" 

This Exception was thrown because the port that was used was 465 instead of 587.
Apparently, Google Cloud supports only port 587 by default for outgoing mail, maybe because port 465 is legacy now.
Here's the Stack Overflow link for you: 
http://stackoverflow.com/questions/15796530/what-is-the-difference-between-ports-465-and-587

Solution: Use 587 as the port.



Swift_TransportException:

Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at...

This error is sent by Google refusing to log in to your account from an untrusted app (yours :D ).

Solution: Enable two-step verification for your google account (the one that's used to send the emails in your web app).
 if you haven't already, go to https://www.google.com/landing/2step/ and hit "Get started" button and follow the instructions to set it up.
  • After everything is set up, open "App-specific passwords" tab.
  • click "Manage application-specific passwords".
  • From there, open the "select app" select box and choose "Other".
  • Type a descriptive name for you app and hit "Generate".
  • You'll be given a password, copy that to your Laravel mail configuration file (config/mail.php) in the password entry and you're good to go.
Here's how the configuration file looks like

return array(
    'driver' => 'smtp',
    'host' => 'smtp.gmail.com',
    'port' => 587,
    'encryption' => 'tls',
    'username' => 'your_gmail_username',
    'password' => 'your_generated_password',
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,
);

That's it, your app should now be able to send emails without problems.