Social Networks

This post explains you how to “Sending Mail using SMTP and PHP” using SMTP credentials. We had implemented this mail notification system using SMTP and SASL classes. It’s simple just few lines of configuration changes. To connect to an outgoing SMTP server from a PHP script using SMTP authentication and send an email.

smtp-php-code
index.php

Contains PHP code. You have to modify email, name and message

 

$to="user@gmail.com";
$fn="Fisrt Name";
$ln="Last Name";
$name=$fn.' '.$ln;
$from="support@website.com";
$subject = "Welcome to Website";
$message = "Dear $name, 
 
Your Welcome Message.
 
Thanks
www.website.com
";
include('smtpwork.php');

Parameter description

Sending Mail using SMTP and PHP

:
to: the recipient’s email address and name.
fn: first name.
ln: last name.
name: first name and last name
host: your outgoing SMTP server name.
username: the SMTP user name (typically the same as the user name used to retrieve mail).
password: the password for SMTP authentication.

 

 

smtpwork.php

SMTP configuration file you have to modify host, port and credentials.

< ?php
require("smtp.php");
require("sasl.php"); //SASL authentication
$from="support@yourwebsite.com"; 
$smtp=new smtp_class;
$smtp->host_name="www.website.com"; // Or IP address
$smtp->host_port=25;
$smtp->ssl=0;
$smtp->start_tls=0;
$smtp->localhost="localhost";
$smtp->direct_delivery=0;
$smtp->timeout=10;
$smtp->data_timeout=0;
$smtp->debug=1;
$smtp->html_debug=1;
$smtp->pop3_auth_host="";
$smtp->user="support@website.com"; // SMTP Username
$smtp->realm="";
$smtp->password="password"; // SMTP Password
$smtp->workstation="";
$smtp->authentication_mechanism=""; 
 
if($smtp->SendMessage(
$from,
array(
$to
),
array(
"From: $from",
"To: $to",
"Subject: $subject",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"$message"))
{
echo "Message sent to $to OK."; 
}
else
echo "Cound not seend the message to $to.nError: ".$smtp->error;
?>
None found.
  1. Warning: require(/home/yourad6/public_html/faster-key/includes/welcomeMailer/plain_sasl_client.php) [function.require]: failed to open stream: No such file or directory in /home/yourad6/public_html/faster-key/includes/welcomeMailer/sasl.php on line 313

    what to do to over come this error

  2. Hi am getting this error while run above code
    Error: could not resolve host “smtp.gmail.com:587”

    how to solve this? help me

  3. not connect error here :
    Cound not seend the message to infoitmanoj@gmail.com. Error: could not connect to the host “27.107.107.48”: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. Could Not Perform the Query

  4. I am trying to send an email via GMail’s SMTP server from a PHP page but I get this error:
    authentication failure [SMTP: SMTP server does no support authentication (code: 250, response: mx.google.com at your service, [98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)]

    • Pankaj Jha says:

      Below is the working code which you can use –

      < ?php require_once "Mail.php"; $from = "“;
      $to = ““;
      $subject = “Hi!”;
      $body = “Hi,\n\nHow are you?”;

      $host = “ssl://smtp.gmail.com”;
      $port = “465”;
      $username = ““;
      $password = “password”;

      $headers = array (‘From’ => $from,
      ‘To’ => $to,
      ‘Subject’ => $subject);
      $smtp = Mail::factory(‘smtp’,
      array (‘host’ => $host,
      ‘port’ => $port,
      ‘auth’ => true,
      ‘username’ => $username,
      ‘password’ => $password));

      $mail = $smtp->send($to, $headers, $body);

      if (PEAR::isError($mail)) {
      echo(“

      ” . $mail->getMessage() . “

      “);
      } else {
      echo(“

      Message successfully sent!

      “);
      }

      ?>

  5. For sending HTML mail using above class modify the headers. Like:

    “MIME-Version: 1.0”,
    “Content-type: text/html; charset=iso-8859-1”,
    “From: $from”,
    “To: $to”,
    “Subject: $subject”,
    “Date: “.strftime(“%a, %d %b %Y %H:%M:%S %Z”)

  6. I can’t send HTML message this. If i send a simple text message the emial arrives. Can you please check this issue.

  7. Error coming Jha .. please help

    Warning: require(/home/yourad6/public_html/faster-key/includes/welcomeMailer/plain_sasl_client.php) [function.require]: failed to open stream: No such file or directory in /home/yourad6/public_html/faster-key/includes/welcomeMailer/sasl.php on line 313

  8. Thanks for the tutorial…I have concern on how can i send mail from my localhost using Gmail as SMTP server using this tutorial of yours. Is it possible.

  9. Edison Ollivierre says:

    What’s Happening i am new to this, I stumbled upon this I have found It positively helpful and it has helped me out loads. I hope to contribute & aid other users like its helped me. Good job.

Leave a Reply

Your email address will not be published. Required fields are marked *

Allowed Tags:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>