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.


13 comments:

  1. Thanks a lot !! very useful script well done keep it up

    ReplyDelete
  2. Another simple solution would be SMTP4PHP
    https://plus.google.com/109110210502120742267/posts/QhppGGUk3FS

    ReplyDelete
  3. No doubt It is good one too. I have tried SMTP4PHP too.

    Y u should donwload new one while the thing is inbuilt available.

    But for new user it my seems scary as it shows red markers in professional tools like Dreamweaver.

    Whole code works well in SMTP4PHP jst download and place it to your foler and provide required details. you are done.

    ReplyDelete
  4. Thank for trying it out! :)
    I am not sure about what you are talking about regarding Dreamweaver; I'm using "gedit" on Linux and Notepad++ on Windows! :)

    ReplyDelete
  5. How to install pear on windows??
    which change we have to maid in php.ini other then this??

    ReplyDelete
    Replies
    1. Well above changes are quiet enough for sending mail.
      If you are using XAMPP then u can find PEAR installed in php folder inside XAMPP.
      php.ini contains path to PEAR file directly.

      If you dont find PEAR installed on your WINDOWS then you can find steps here.
      https://pear.php.net/manual/en/installation.php
      http://pear.php.net/manual/en/installation.getting.php

      Delete
    2. I am Using Wamp Server..
      Apache Version :
      2.2.21
      PHP Version :
      5.4.3

      Delete
    3. Thanx Very Detail and Useful Information.
      I also Suggest to all don't use wamp its more complicated.
      Xampp is very userfriendy to work.

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Using 3D www.aolmail.com ​can be one of the most successful ways to boost your direct mail success. Read my shortly article to see how it can work in your business, no matter if you're in the B2B or B2C business.

    ReplyDelete
  8. Excellent website! I adore how it is easy on my eyes it is. I am questioning how I might be notified whenever a new post has been made. Looking for more new updates. Have a great day! direct mailing company

    ReplyDelete