1

I want to send an email in php from my localhost which includes mail.php file.

Here is my code for test.php file:

<?php 
include_once("Mail.php"); 

$From = "Sender's name <testing123xyz@gmail.com>"; 
$To = "Recipient's name <test2@gmail.com>"; 
$Subject = "Send Email using SMTP authentication"; 
$Message = "This example demonstrates how you can send email with PHP using SMTP authentication"; 

$Host = "mail.gmail.com"; 
$Username = "testing123xyz"; 
$Password = "testing"; 

// Do not change bellow 

$Headers = array ('From' => $From, 'To' => $To, 'Subject' => $Subject); 
$SMTP = Mail::factory('smtp', array ('host' => $Host, 'auth' => true, 
'username' => $Username, 'password' => $Password)); 

$mail = $SMTP->send($To, $Headers, $Message); 

if (PEAR::isError($mail)){ 
echo($mail->getMessage()); 
} else { 
echo("Email Message sent!"); 
} 
?>

When i hit the url of test.php file, server is throwing following error :

Failed to connect to smtp.gmail.com:25 [SMTP: Failed to connect socket: No connection could be made because the target machine actively refused it. (code: -1, response: )]

Please help me out. Thanks.

deepak.mr888
  • 349
  • 2
  • 11
  • 26

2 Answers2

1

Download and use phpmailer from github:https://github.com/PHPMailer/PHPMailer Also, check this tutorial, it will help you: http://phpmailer.worxware.com/?pg=tutorial

This is the php code to send emails:

 <?php

        function email($recipient_email_id,$senders_name){

        include("phpmailer/class.phpmailer.php");

            $mail = new PHPMailer();
            $mail->IsSMTP(); // send via SMTP
            $mail = new PHPMailer();
            $mail->IsSMTP(); // send via SMTP
        //IsSMTP(); // send via SMTP
            $mail->SMTPAuth = true; // turn on SMTP authentication
            $mail->Username = "sender@gmail.com"; // SMTP username
            $mail->Password = "xxx"; // SMTP password
            $webmaster_email = "reply@gmail.com"; //Reply to this email ID
            $email = "$recipient_email_id"; // Recipients email ID
            $name = "$senders_name"; // sender's's name
            $mail->From = $webmaster_email;
            $mail->FromName = $name;
         $mail->AddAddress($email, $name);
            $mail->AddReplyTo($webmaster_email, "Name");
            $mail->WordWrap = 50; // set word wrap
        $mail->IsHTML(true); // send as HTML
            $mail->Subject = "This is the subject";
            $mail->Body = "Hi,
        This is the HTML BODY "; //HTML Body
            $mail->AltBody = "This is the body when user views in plain text format"; //Text Body
            if (!$mail->Send()) {
                echo "Mailer Error: " . $mail->ErrorInfo;
            } else {
                echo "Message has been sent";
            }
        }
        ?>
Hardik Dave
  • 676
  • 1
  • 6
  • 17
0

My guess is that you are using localhost. Try using Test Mail Server

EDIT as of this link, you are using wrong host. Change it to smtp.gmail.com

Justinas
  • 41,402
  • 5
  • 66
  • 96