Thursday, 30 August 2012

send php mail using gmail smtp server

Everyone looking for cheap/free and reliable smtp server to send mails using php. Gmail is the one of the best and reliable way to send the mails from php.

To send php mail from localhost requires lot much settings and to purchase costly domains/host. While gmail is free and reliable way to send mail from localhost using php.

Below I have given all the gmail smtp servers and its port to send mail. Also, given some headers to send images in mails. means to send images inside the php mail content.

Remember : First of all you need to uncomment the extension=php_openssl.dll into the php.ini file. If you don't find this line into php.ini file then copy it from here and paste it into php.ini and restart your apache server. This is proper solution for following Warning:

Warning : Failed to connect to smtp.gmail.com:465 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
100% Working and tested. Feel free to use below code

<?php

//IT is PEAR mail function file. Don' t remove below line 
require_once "Mail.php";

$from = "your_gmail_ID";
$to = email_of_recipient;

$subject = "Subject Of Your Mail";
$body = '<img src="youBanner.jpg/png/gif" />  <br/>';
$body.='Your Body content';

// SMTP and PORT setting for Gmail
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "yourGmailEmail@gmail.com";
$password = "yourGmailPassword";

//Setting up Headers for text and Image content type
$headers = array ('From' => $from,
                            'To' => $to,
                            'Subject' => $subject);

//Mail Header for Support HTML tags Images and UTF charset
$headers["Content-Type"] = 'text/html; charset=UTF-8';

$smtp = Mail::factory('smtp',
                                   array ('host' => $host,
                                             'port' => $port,
                                             'auth' => true,
                                             'username' => $username,
                                             'password' => $password));

//Send Email using pear sned option
$mail = $smtp->send($to, $headers, $body);

//If any errors occurs
if (PEAR::isError($mail)) {
            echo("<p>" . $mail->getMessage() . "</p>");
}
else {
             echo("<p>Message successfully sent!</p>");
}

?>



If you found any message regarding deprecated and strict standard error then dont worry about it just put error_reporting(E_ALL); or error_reporting(0); and you are done.


Generally We found code for gmail mail sending using php but there is no HTML tag support found easily.
For that you need to customize its header like above to support images and other tags in body of gmail.

With this HTML header, we can easily add tags like div, span, table, and all the other HTML supporting.

If you want simple Header to support \n and \r ten just remove the above header.

If you are done with successful sending then I recommend you to remove the error code. Which generally used to check SMTP error in mail factory.