Blog

Connecting Rails to Microsoft Exchange / SMTP Email Server

When a client first asked me to switch from Sendgrid to use their smtp exchange server, I figured it’d be really simple…like connecting to a gmail account. However, I ran into a few issues. Like a lot in programming, it wasn’t anything way different than what I was doing, but a couple to syntax differences. Hopefully, someone will find find this helpful (surprisingly google didn’t help me much with my searches).

So, here’s the standard gmail setup:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address        => 'smtp.gmail.com',
  :port           => '587',
  :authentication => :plan,
  :user_name      => ENV['SMTP_USERNAME'],
  :password       => ENV['SMTP_PASSWORD'],
  :domain         => 'brownwebdesign.com',
  :enable_starttls_auto => true
}

Just a few small changes below to the authentication type.

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address        => 'smtp.office365.com',
  :port           => '587',
  :authentication => :login,
  :user_name      => ENV['SMTP_USERNAME'],
  :password       => ENV['SMTP_PASSWORD'],
  :domain         => 'congrueit.com',
  :enable_starttls_auto => true
}

Your SMTP_USERNAME should be your whole email address.

The other thing that got me was you need to send from the actual account you’re using to log in with. For example, don’t send email from noreply@brownwebdesign.com and use support@brownwebdesign.com as your
SMTP_USERNAME.

There may be some other setting on the exchange side that needs to be tweaked (smtp allowed, authentication, etc.), but I think my client had

Debugging

Maybe the more interesting this is how to debug it.

The app isn't in production yet, but I tried it on the production server first; didn't work. So, easy way never works. Try in test/dev first I use mailcatcher.me in development mode, so I took the smtp settings (above) from config/environments/production.rb and placed them in config/environments/development.rb

Then I made sure that config.action_mailer.raise_delivery_errors = false was set to true and that helped debug the reasons things were going wrong. I got an error about wrong authentication type and then that my account didn't have permission to send (was using different from address).

Posted in General | Tagged , , , | Leave a comment

Setting up a Godaddy domain for heroku

If you have a client using Godaddy, here’s a quick tip for setting up a Godaddy domain for heroku.

 heroku domains:add www.example.com

That’s easy enough to go into Godaddy’s DNS manager and add a cname for www and link it to your heroku domain example.herokuapp.com.

But, heroku doesn’t support naked domains (example.com). Some providers like DNSimple provide an alias for a naked domain. The next best thing is a forwarder. After setting up your CNAME record, exit out and find the link to turn forwarding on. Then, forward example.com to www.example.com (default is 301 permanent…you want that). Now you’re good to go!

Posted in General | Tagged , , | Leave a comment

Setting Up SSL on Heroku

I really love Heroku. The simplicity and beauty of how it all works just makes me happy. However, unlike most of their instructions/documentation, I recently ran into a bit of trouble when setting up a custom domain ssl. Looking back on it, it wasn’t all that big of a deal, but here’s a few details in what I had to do to get it working.

First, know that you can always use heroku’s free ssl with your-app.herokuapp.com, but if you want https://www.yourapp.com, you have to pay the $20/mo fee and setup the SSL Endpoint Add on.

I followed the directions here: https://devcenter.heroku.com/articles/ssl

Everything was heroku smooth until I got to the upload certificate part. Every time I tried to upload the certificate, I got an error. I’ve setup a few servers using ssl and felt pretty confident that I was using the right certificates/keys/etc. After a bit of trying and failing, and googling, I remembered in the last nginx server I setup for ssl I came across documentation for root certificates:

if you have a chain of certificates — by having intermediate certificates between the server certificate and the CA root certificate — they’re not specified separately like you would do for Apache. Instead you’ll need to concatenate all the certificates, starting with the server certificate, and going deeper in the chain running through all the intermediate certificates. This can be done with “cat chain.crt >> mysite.com.crt” on the command line. Once this is done there’s no further use for all the intermediate certificates in what Nginx is concerned. You’ll indicate in the Nginx configuration the file with all the (concatenated) certificates.

I decided to give it a try. I mostly use www.dnsimple.com for ssl certs ($20 bucks!) and they use RapidSSL. So I downloaded the rapidssl_bundle.pem file and concatenated it to the bottom of the server cert. Then heroku certs:add server.crt server.key worked just fine!

The other area I’m still a bit confused about is the “Configure DNS” section. You’ll need to add (or change if you already have setup) your CNAME record to point to the new ssl endpoint add on that heroku creates when you successfully add your keys. What’s not clear is if you still want to serve regular http traffic to certain parts of the app, does it still work?? The app I did this on we use https all the time, so it wasn’t an issue, but I’m curious about the non https and how it works.

**Update** Matthew Manning @ Heroku was kind enough to read my article and answered my question. “Yes. A SSL endpoint can be used with both secure (https) and insecure (http) traffic.” Thanks!

NOTE: It’s been a few weeks since I ran into this trouble and now that I’m wring the blog article I wanted to try and recreate the error. My app still isn’t live, and so as to avoid the $20 charge on another app, I just removed the keys and was going to try and re upload the cert only. It actually worked this time without the pem (no error). I’m not sure if this is because heroku still had something in cache or what?? However, even though it worked, when I previewed my certs heroku certs I got:

Endpoint                 Expires               Trusted
-----------------------  --------------------  -------
nara-2279.herokussl.com  2013-12-11 17:40 UTC  False

Only when I updated and used my concatenated cert did it work, so regardless of whether you get the error or not, you will need to cat the certs.

@mattmanning also gave me this advice: You might also want to mention the SSL Doctor client plugin, which can complete the chain of trust for you automatically. https://github.com/heroku/heroku-ssl-doctor

Posted in General | Leave a comment

Using the sanitize gem – Clean HTML

I recently had a need to sanatize html entered by users in an app. The app (www.csepub.com) allowed professors to enter homework assignments for their ebooks. One type of homework assignment is a written assignment. The app needed to allow the user/student to create and submit an assignment. At first we were using uploaded documents, but with thousands of students, this became a heap of uploaded word docs. Recently we decided to allow the student to submit their assignment via html using an html editor (trimmed way down to just basic text formatting).

My fear was that allowing this many people to enter html into my database, that something might get wonked up. I’m not a security expert, but I figured I should be sanitizing the html.

After a bit of research I found the sanitize gem (https://github.com/rgrove/sanitize). It does 2 basic things: 1) clean the html of unwanted tags and closes any open tags. Hopefully this won’t be a problem for me because I’m preventing the user from editing actual html with a slimmed down html editor, but I don’t want to take a chance.

The gem is pretty straight forward. Just place it in your gem file and then bundle.

Next, in the AR model you want to store your html in, I added a before_save hook.

class AssignmentUpload < ActiveRecord::Base
  before_save :clean_html
 
  private
  def clean_html
    self.html = Sanitize.clean(html, whitelist)
  end
 
end

The biggest decision you have to make is which tags you want to whitelist. Sanitize has several pre built levels for you to use. Sanitize::Config::BASIC looked like the option for me. However, I realized that I needed to allow a span tag with a inline style (professors wanted to make notes to the assignments and highlight text). I didn’t want to recreate the pre setup from scratch, so I figured out Sanitize::Config::BASIC is just a hash so I just slightly modified it.

def whitelist
  whitelist = Sanitize::Config::BASIC
  whitelist[:elements].push("span")
  whitelist[:attributes]["span"] = ["style"]
  whitelist
end
Posted in General, Programming | Tagged , , , | 2 Comments

Ruby on Rails – Increment and decrement

If you’re using rails and have an integer column in database that you want to increment or decrement, rails provides a nice abstraction for you to use. Normally you’d have do something like this:

def increase_credits
  update_attributes(:credits => credits + 1)
end

Then you can call it like:

@user.increase_credits

With the increment and decrement you can do it this way:

@user.increment!(:credits)

or

@user.decrement(:credits, 2)  # decrease credits by 2

It just saves setting up the method and allows you to pass and increment by!

Decrement: http://apidock.com/rails/ActiveRecord/Base/decrement!
Increment: http://apidock.com/rails/ActiveRecord/Base/increment%21

Posted in General | Leave a comment

Rails uniq_by – Unique Results From ActiveRecord Relations

I just discovered a neat method in rails called uniq_by. http://apidock.com/rails/Array/uniq_by

It allows you to get a unique set of results from an active record relation.

Ruby provides a way to get unique results from an array like so:

a = [1, 2, 3, 3, 4]
a.uniq # results [1, 2, 3, 4]

But if you have an active record relation, the normal uniq won’t work. Say you have a list of stores and you want to find their location, but you only want 1 location from each store/company. You can use:

Location.near("monroe, ga").uniq_by(&:store_id)

And that will give you a nice AR relation of unique locations!

Posted in General | Tagged , | Leave a comment

Debugging Magento Duplicate Entry Database Error

I recently got a call from a client that has a Magento cart. He was getting an error when saving a product:

#1062 - Duplicate entry '59-0-1-229' for key 1

I wanted to do a quick writeup because it was a rather difficult one to debug and solve. After duplicating the error, I first tried to reindex the database tables. However, the “Product Prices” tabel was giving an error and couldn’t be saved.

I logged into the server and check the execption log. It didn’t give me much more info:

exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '59-0-1-229' for key 1' in /home/print/public_html/shop/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /home/print/public_html/shop/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /home/print/public_html/shop/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#2 /home/print/public_html/shop/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 /home/print/public_html/shop/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array)
#4 /home/print/public_html/shop/lib/Varien/Db/Adapter/Pdo/Mysql.php(333): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `ca...', Array)
#5 /home/print/public_html/shop/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Indexer/Price/Default.php(449): Varien_Db_Adapter_Pdo_Mysql->query('INSERT INTO `ca...')
#6 /home/print/public_html/shop/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Indexer/Price/Default.php(120): Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Indexer_Price_Default->_applyCustomOption()
#7 /home/print/public_html/shop/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Indexer/Price.php(377): Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Indexer_Price_Default->reindexAll()
#8 /home/print/public_html/shop/app/code/core/Mage/Index/Model/Indexer/Abstract.php(125): Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Indexer_Price->reindexAll(.....

Notice how the query error left out the actual query. At this point I figure there’s something wrong with a table somewhere. There are 250 tables in a Magento database, where to start? It has to be something to do with products, but that doesn’t narrow it down much, there are many tables dealing with products.

Then I tried a global search on the database from phpMyAdmin for the
duplicate key 59-0-1-229, but it turned up no results.

After much trial and error, a database dump and reimport, and more, I decided to focus on finding the query that was causing the error.

I found this article on how to get more detailed exception logging. Basically we’re modifiying the logException function rather than the “string” version of the error. The code I used from the article didn’t change anything for me, so here’s basically what I did:

 * Write exception to log
 *
 * @param Exception $e
 */
public static function logException(Exception $e)
{
    if (!self::getConfig()) {
        return;
    }

    $file = self::getStoreConfig('dev/log/exception_file');
    //comment out the below line
    //self::log("\n" . $e->__toString(), Zend_Log::ERR, $file);
    //add the below line
    self::log($e, Zend_Log::ERR, $file);
}

This will give you a huge dump of info into the
app/var/log/exception.log file. From there I was actually able to see
the sql querry causing the problem.

INSERT INTO `catalog_product_index_price_opt_agr_idx` SELECT `i`.`entity_id`, `i`.`customer_group_id`, `i`.`website_id`, `o`.`option_id`, IF((@price:=IF(IF(ops.option_price_id>0, ops.price_type, opd.price_type)='fixed', IF(ops.option_price_id>0, ops.price, opd.price), ROUND(i.price * (IF(ops.option_price_id>0, ops.price, opd.price) / 100), 4))) AND o.is_require, @price,0) AS `min_price`, @price AS `max_price`, IF(i.base_tier IS NOT NULL, IF((@tier_price:=IF(IF(ops.option_price_id>0, ops.price_type, opd.price_type)='fixed', IF(ops.option_price_id>0, ops.price, opd.price), ROUND(i.base_tier * (IF(ops.option_price_id>0, ops.price, opd.price) / 100), 4))) AND o.is_require, @tier_price, 0), NULL) AS `tier_price` FROM `catalog_product_index_price_final_idx` AS `i`
INNER JOIN `core_website` AS `cw` ON cw.website_id = i.website_id
INNER JOIN `core_store_group` AS `csg` ON csg.group_id = cw.default_group_id
INNER JOIN `core_store` AS `cs` ON cs.store_id = csg.default_store_id
INNER JOIN `catalog_product_option` AS `o` ON o.product_id = i.entity_id
INNER JOIN `catalog_product_option_price` AS `opd` ON opd.option_id = o.option_id AND opd.store_id = 0
LEFT JOIN `catalog_product_option_price` AS `ops` ON ops.option_id = opd.option_id AND ops.store_id = cs.store_id

The first thing I did was copy and paste the query into mysql to make
sure I was getting the same result, and sure enough I got the same
Dtable I checked was the duplicate entry error:

#1062 - Duplicate entry '59-0-1-229' for key 1

I tried deleteing the catalog_product_index_price_opt_agr_idx table
first. Magento rebuilt it back, but with the same error. This is when
I took a closer look at the keys on the table. This is when I see there
is a unique key on fields:

entity_id, customer_group_id, website_id, option_id

That’s when it hits me that the 59-1-1-229, are those field values:

entity_id           59
customer_group_id   0
website_id          1
option_id           229

Then I go and check product #59 for an option 229, but theere isn’t one.
So I go into the catalog_product_option table and delete option 229.
Then I save a product and get no error. Also all tables will now index
properly.

I can only figure that that option must have been deleted at some point,
but never got completely deleted from the options table. A new option
took it’s place, but now was causing a duplicate.

Moral: logging and being able to read logs can help tremendously!

Posted in General | Leave a comment

Read The Code

Several monts ago, John Nunemaker bloged “Stop Googling“.  It was about reading the code in an open source project oppsed to Googling it.  He was a bit funstrated at someone who Googled, polled friends, and basically searched everywhere besides the source to look for an answer for their problem.

Today I found out the hard way how right he was.  I was trying to access the object info passed along to a delayed_job record.  I googled for 30 or 45 minutes trying to find an answer and couldn’t get any bites.  I knew the handler field had the info I needed, but it was a string.  I also knew that somehow that info had to be extracted when my job was processed.  Finally on a whim, I looked to the source code.  In the second file I opened, I found a method called payload_object.

    def payload_object
      @payload_object ||= deserialize(self['handler'])
    end

I saw object, deserialize, and handler.  That’s all I needed to know.  It was exactly what I wanted.  It took less than a minute. I tried it out and that was it.

That’s when I remembered John’s blog.  Note to self:  if a quick google search doesn’t display the obvious answer…LOOK AT THE CODE!

Posted in General | Leave a comment

Ditch FTP For GIT

Brown Webdesign Does GitEver since I started learning Ruby On Rails several years ago, I’ve been using git. Git is a cool tool you can use for version control, or in some cases, simple incremental backups. It basically keeps track of your code and file changes, you commit/save the changes and can revert back, review history and do all sorts of cool stuff. It additionally will allow you to setup a remote repository to push changes to. That’s where this article comes in.

Using git in rails is really a must, but in the static world, FTP mostly dominates.

I’m really tired of using FTP. I probably hit Command + Shift + Upload 200 times a day. Sometimes I edit files very quickly and hop from file to file, add images to a images directory and javascript files to the js directory. How can I remember which files to FTP? It can be as simple as “git push” and only my changes are updated.

First off, requirements:

  • web server with shell access
  • web server with git installed
  • local machine with a terminal
  • local machine with git installed
  • local machine to test (if you can’t run your code on your local machine or local server, it doesn’t make much sense to setup git…it would be painful to add, commit, push every tiny change)

Get Started

Server Side

Take care of the server side first. ssh into your web server and find a place outside your public folder. For example, a cpanel server is /home/account_name/public_html. I’d store it in /home/account_name.

mkdir account_name.git && cd account_name.git
git init --bare

Now we’ll tell git where to store the actual files:

git config core.worktree /home/account_name/public_html
git config core.bare false
git config receive.denycurrentbranch ignore

Now we have to tell git what to do after we push changes.  Create and open a new file:

vim hooks/post-receive

In this file place this code:

 #!/bin/sh
 #tell git to copy over the files
 git checkout -f
 #change ownership of files so your web server can serve them (I’m logging in as root so it may not be necessary if you logging in as account_name
 chown -R account_name:account_name /home/account_name/public_html/*

Note: This file (post-receive) needs to be executable. To be sure:

chmod 755 hooks/post-receive

Local Side

Navigate to the directory where your website code lives. If you haven’t already setup git:

git init
git add .
git commit -m ‘initial commit’

Now link up the remote repository:

git remote add origin ssh://user@yourwebserver.com/home/account_name/account_name.git

Now it’s time for the magic:

git push origin master

Your website should now be live. You can now make changes locally, test and then push. Example:

#...make some changes to your code
# test them (this could be a visual QA test or automated test)
git add .
git commit -m ‘my first changes’
git push #no need for the origin master now
Posted in General, Programming, Web Design | 3 Comments

Highlights From LessMoney

Photo: Adam Wiggall

The past weekend I attended a conference for web development/creative agencies.  The topic was how to make your business better and was taught by some of the best in the industry:  LessEverything.

I’m sure everyone got a little something different out of LessMoney, but I’m going to share the highlights of what I’ve learned. Please feel free to comment or ask questions below.

Marketing

Blogging

First you’ll notice you’re reading this out of my blog which until now has been a boring bit of news about us.  I’ve always been hesitant about writing technical articles on my blog.  My clients don’t care about that stuff right?  Well, it’s almost impossible to market web design to actual clients.  Our clients come from all walks, small local businesses to large corporate businesses and from all different industries.  How can you possibly market to that wide a range…especially as a small firm?  What LessEverything does is market to their peers and well…their competition basically.  Firstly, this establishes themselves as an authority in the industry (they’re well known, respected and liked).  And secondly, our industry is blessed enough to have clients abound that we have to refer out speciality work.  So a design firm may refer work to a development firm and vice verse.  Marketing to your competition still feels backwards to me, but I’m willing to give it a try.  So expect at the least, more insightful technical, award winning, can’t miss articles from this blog :-)

Website

Next is my website.  I’m going to make it better.  I’ve been so busy working on other people’s sites that I’ve neglected my own.  But, all that says is “hey I can afford this guy. (Steven Bristol)”   There were some amazing designers here, like http://turnandface.com/ , http://gauged2.com/, http://www.16toads.com/, just to name a few (I could actually put everyone’s site here).  They explain how much traction they’ve gotten from their sites.  I also want to design my site in a way that will qualify leads.  By making it great, it will naturally discourage away people looking for a cheap site.  But if done in a clever way, a different way, a less boring way, then then ideal clients will get that and others will fly under the radar.

Open Source

Another way to market myself is to create open source projects or design for open source projects.  This is perfect motivation for me to release a project into open source that I use to track my billable time in my company.  I wrote the rails app after needing a new solution for myself and it’s a nice simple tool many others could use, add to, and help me improve my code.  And maybe along the way, new relationships will be created that will lead to new business.

Swagger

I’m a quiet, soft spoken, easy going, humble type of guy.  I have opinions and preferences, but I keep them to myself.  This will need to change.  Clients want to pay for opinions.  They need guidance, ideas, experience and consulting.  Consulting means giving advice, and this is no different in the web industry.  From now on, we’ll display a little swagger!

Success

Our number one goal as consultants, designers, and developers is to help a client define success for a website or web app and provide them with the best path to success with the least risk and least headache. I think we generally do a pretty good job of this, but we could probably do a better job of reiterating it to the client.

Business & Contracts

Use LessAccounting.  Period.  Just kidding.  The joke of the weekend was that LessMoney was really a rouse to get us all to use LessAccouting (LessEverything’s flagship product).

Contracts

Before, I only used contracts for larger projects or on gut feelings.  I’ll now be using a contract for every new project, large or small.  I’ve never had an unhappy client (that I’m aware of), but that day may come and and I want to make sure I’m protected by a contract for what was or wasn’t supposed to be delivered.  They gave us the contract they use and I’ll be using it against my own to modify it for my own needs.

Insurance

I’d never thought of this, but from now on we’ll carry errors and omission insurance to protect the company against the potential of frivolous lawsuits.  I don’t believe we’ll ever have a legitimate lawsuit against us because we’re Godly, honest folks that bend over backwards to please people, but in this country, anyone can sue anyone for anything and it’s expensive to defend yourself, even when there’s absolutely no case against you.

Fees

This is bad news for my clients, but I’m charging way too little for my services compared to others.  We will likely be raising our fees sometime in the near future.  Our quality of work and way too busy schedule is proof that the time has come.  We also haven’t raised our rates since we started over 3 years ago.

Fixed Rate or Hourly?

There was no cut and try answer to this, but one thing clear for me was that I need to move to hourly, or adjust my fixed estimates.  At times I go double over budgets in hours for a project.  Fortunately I can afford to do this because it’s my time instead of having to pay other devs to do it, but it’s not fun, and also adds unnecessary stress takes time away from my family.  If you’re having a hard time estimating, don’t feel alone, nearly everyone there struggles with this.

No content no problem

Myself included, many others cited a problem of getting clients to submit copy/content for the site, which leads to delayed launch, which leads to delayed payment (you get final payment when you launch the site right?).  This has to change.  Some people said they get content up front before starting a project.  Some said they get down payment first, then wait on content to start the project.  Others suggested putting language in the contract that all copy would need to be submitted x number of days (10-30) after the site was ready to launch or there would be a charge for it and they the client was still liable for final payment regardless.  Whether the client is too busy, or is using as a stall tactic, it isn’t fair and should be thought out by the agency ahead of time.

Agency Success

When we first kicked the workshop off, we all (only ~20 total allowed to attend) introduced ourselves, said why we were there and defined success for our business.  It really warmed my heart and inspired me to say that probably 90% + of us said something along the lines of this:  making more money wasn’t really an an objective, but spending more time with family, better quality of life, working on more interesting projects, having less stress, and just learning how to deal with problems was why they were there, and was their goal.  This was really so cool to hear.  As an industry, we all love designing, coding, building, and creating things.  We want to get paid for our hard work to support our families, many are not just in it for the money only.

These highlights were just the tip of the iceberg.  Some of the most value came from the group discussions and relationships established.  I met some super cool people and already have loose plans to meet up with some of them and also already have some leads on some new exciting projects. I highly recommend contacting LessEverything and asking when/if LessEverything Season #2 is going to happen..  It’s well worth the time and money to go!

Posted in General, Web Design | 1 Comment
Google+