How to send email via SMTP with PHP and Gmail/Hotmail?

Lots of spamer use 25 port to send mail. By default port 25 was forbidden for ComfortVPS,  you need submit a support ticket if you want us open 25 port for you . But you can use 3rd party SMTP service with 465 or 587 port to send email without contact us. eg, Gmail, Hotmail

Below is How to send email via SMTP with PHP and Gmail?

For example, We usually use a php class PHPMailer(very popular) with similar settings to this ones:

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host       = "smtp.gmail.com";      // SMTP server example, use smtp.live.com for Hotmail
$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 465;                   // SMTP port for the GMAIL server 465 or 587
$mail->Username   = "username@gmail.com";  // SMTP account username example
$mail->Password   = "password";            // SMTP account password example

Full example of send email via SMTP use PHPMailer is here: https://github.com/Synchro/PHPMailer/blob/master/examples/smtp.phps

You can find more about PHPMailer here: https://code.google.com/a/apache-extras.org/p/phpmailer/

NOTE: You need login your gmail account, Open the 'Forwarding and POP/IMAP' tab on your 'Settings' page, and configure IMAP or POP. After enabling this in Gmail, make sure you click 'Save Changes' so Gmail can communicate with your mail client.  More FAQ from Gmail

How to send email via Hotmail SMTP?

PHP code is similar to above, just change the smtp server to smtp.live.com 

I'm use a CMS software program, eg, Drupal,Wordpress, What should I do?

Go to your software backend, find email setting, choose smtp for send email, and set someting like above. done!

Gmail SMTP Tutorial: Drupal SMTP setting - Wordpress SMTP setting - cPanel SMTP setting

How to test 465/587/25 port opened or not via SSH?

[root @ comfortvps ~]# telnet smtp.gmail.com 25 
Trying 173.194.64.108...  ===> 25 port not open if it stay here more than 10 seconds

Press Ctrl+C to exit

[root @ comfortvps ~]# telnet smtp.gmail.com 587
Trying 173.194.64.108...
Connected to smtp.gmail.com.  ===> 587 port opened
Escape character is '^]'.
220 mx.google.com ESMTP tv3sm1408290obb.8 - gsmtp
^]
telnet> quit
Connection closed.
[root @ comfortvps ~]# telnet smtp.gmail.com 465
Trying 173.194.64.108...
Connected to smtp.gmail.com.  ===> 465 port opened
Escape character is '^]'.
^]
telnet>  quit
Connection closed.

Reasons to Switch to send email via SMTP with Google/Gmail or Hotmail or some other smtp service

  • Reliability – Gmail has proven to be a very reliable email service.
  • Less Spam – Keep unwanted messages out of your inbox with Google’s innovative spam filter technology.
  • Clogged Email Queues – With the potential of hundreds of other accounts on the same server as you, resources often dry up. This can occur due to a spammer or a hacked script, thus resulting in backed-up email queues.
  • Blocked IP Addresses – Spammers or a hacked script/site cause all kinds of problems, and spam sent from one server often can get multiple servers with similar IPs blocked. Thus, your email can be affected even if your server or domain wasn’t even the problem.
  • Storage Space – With a whopping 7500+ MB of storage that Google gives you – for each email account – you may never have to delete another email again.
  • Portability – Setup to receive IMAP and/or POP email, and you can also use their browser based client to get/store your email.
  • Service & Support – If there is a problem, it most likely effects everyone using Gmail – so you can be assured Google is probably already aware of it, and a fix is in the works.
  • Server Limits – Most hosting providers limit the number of emails sent per domain per hour;  in-order-to prevent spammers. For organizations with many email accounts or that send out newsletters, this can be a problem.

Tutorial: