Getting default user picture drupal 6
There is already a Gravatar module out there. I think you would be better off using a vetted, bug tested code than having to build it yourself. Once you have it installed, you can use the function gravatar_get_gravatar($mail, $options = array()) to do exactly what you're wanting.