0

I am trying to send mail using php.And i am using WampServer. so i tried the following code

ini_set("SMTP","smtp.gmail.com" );
ini_set("smtp_port","465");
ini_set('sendmail_from', 'person1@gmail.com');          
$to = "person2@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "person1@gmail.com";
$headers = "From:" . $from;
$retval = mail($to,$subject,$message,$headers);
   if( $retval == true )  
   {
      echo "Message sent successfully...";
   }
   else
   {
      echo "Message could not be sent...";
   }

but it take more time to connect and says could not connect with localhost. Please help me in solving the problem

krishna
  • 4,069
  • 2
  • 29
  • 56

3 Answers3

1

try this configuration:

http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/

this might help.

Mark
  • 8,046
  • 15
  • 48
  • 78
1

I stumbled on something similar using XAMP and was able to send emails through PHPMailer PHP library.

There is a variety of examples that can be found in PHPMailer/examples/ that can be quite helpful. Below is a sample code that can be used to send an email using PHPMailer.

Note: it requires downloading the library files and adjusting the path to the require or use files which are Exception, PHPMailer, and SMTP. Also, This implementation requires the sender's username and password to send the email not efficient for contact forms.

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';

$mail = new PHPMailer(true);
$mail->CharSet =  "utf-8";
$mail->IsSMTP();
// enable SMTP authentication
$mail->SMTPAuth = true;                  

$mail->Username = "Email to Send From"; // GMAIL username

$mail->Password = "#########"; // GMAIL password
$mail->SMTPSecure = "ssl";  

$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = "465"; // set the SMTP port for the GMAIL server

$mail->From= $emailFrom;
$mail->FromName='SenderName';
$mail->AddAddress($emailTo, $name);
$mail->Subject  =  'EMAIL SUBJECT';
$mail->IsHTML(true);
$mail->Body    = "

    Dear ".$name.", <br><br>

    Some Text/Message Content. <br><br>

    Regards";

// Temporary fix, remove if SSL available
$mail->SMTPOptions = array(
'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
)
);

try {
$mail->Send();

  // Sent Successfully
  
}catch (Exception $e) {
  echo "Mail Error - >".$mail->ErrorInfo;
}

Hope this helps :)

Osama Hussein
  • 69
  • 2
  • 8
-1

You're trying to send mail from your localhost (Your PC) I guess It's not setup to send mail. Move the script to a production server and it will work

Rakesh K
  • 692
  • 4
  • 13
  • 26