Php – using gmail to send mails from your php

emailgmailPHPsmtp

i've read some threads here in SO that someone managed to use gmail's smtp server to send out messages from php script.

so if php sends a mail to peter@hotmail.com it looks like this:

Alternative 1:

  1. php tells sendmail (smtp server) to send the message
  2. sendmail sends the message to gmail's smtp server
  3. gmail's smtp server sends the message to hotmail's smtp server

so now i wonder, why should one use gmail's smtp server to send a mail? isn't it better just to send the message from sendmail to hotmail's smtp server?

Alternative 2:

  1. php tells sends to local sendmail
  2. sendmail sends to hotmail's smtp server

Alternative 3:

  1. php sends the message to gmail's smtp server
  2. gmail's smtp server sends to hotmail's smtp server.

Alternative 4:

  1. php sends the message to hotmail's smtp server. (why does this not work? why have a intermediate smtp server (that actually acts like a pure client) to send to another smtp server?

correct me if im wrong, but isnt all that requires for a mail to be sent a communication between TWO smtp servers, the senders and the receivers (in this case, hotmail)?

i cant still see the picture here and what is happening/required to send a mail. i know what a MTA and a MDA is. but noone has explained this clearly so one could get the idea of how it all works.

would be great if someone could shed a light on this confusing area!

Best Answer

I would guess it isn't set up quite like you have said. Rather it would be a PHP SMTP library that is used and sendmail is not used. For this to work you put the log in information into php for the gmail account and it will communicate directly with it. I guess you could have sendmail use gmail as a smart host so it relays the email, but a PHP library would make more sense to me unless maybe you have a lot of different PHP applications.

If it wasn't like that (no authentication required), gmail would be running what is called an open relay which spammers would use to send crap loads of spam to people.

The reason to use gmail instead of sendmail itself to communicate with hotmail is because you don't have to deal with running an email server. They will make sure gmail users are allowed to send mail to places like hotmail without it being marked as spam. If you run send mail yourself, you are going to have to do all this work yourself.

Lastly, it is called a SMTP server (Simple Mail Transfer Protocol) server because that is what it does, it transfers email between servers (both sends and receives, but doesn't store). This is called an MTA (Mail Transfter Agent). What stores and lets email get their email is the MDA (Something like fetchmail). You can think of the MTA as the post office and the MDA as your mail box. Here is an overview of how email works on wikipedia.

Related Topic