2
<?php
$to = "karthik.hansi@infysky.com";
$subject = "First php test script";
$message = "Hi This message is for leave application ";
$headers = "From:karthik.hansi@infysky.com";
if(mail($to, $subject, $message, $headers))
{
echo "failed";

}else{
echo "sent";
}

?> 

This is My code I have tried this sending mail from PHP not able to send mail anything I have to configure in xampp.

Karthik
  • 37
  • 7

2 Answers2

0
   $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPDebug = 1;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465;
    $mail->IsHTML(true);
    $mail->Username = "yourname@gmail.com";
    $mail->Password = "your_pass";
    $mail->SetFrom("your_email");
    $mail->Subject = "your_title";
    $mail->Body = "your_body";
    $mail->AddAddress("receiver_email");
    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message has been sent";
    }

You have to include phpmailer library. You can download it from here phpmailer

After that you go to your gmail account that you want to use as sender and you need to set 2-step verification ---> off and Allow less secure apps ---> ON.

This guide may help you set up gmail for send emails with php

I am using this script and works fine just make sure to set up all the changes. Maybe you have to add some changes in your xampp ( i did not but i don't know your php.ini file so keep that in mind).

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • SMTP -> ERROR: Password not accepted from server: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 v191sm295231ith.34 - gsmtp SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate. bro getting this error – Karthik Sep 15 '17 at 07:22
  • Do you use gmail account? Try keep it logged in your google account while you try this script. And last is your password correct? – pr1nc3 Sep 15 '17 at 07:23
  • Done bro, I am Configuring this because the employee can send leave to the admin through the apply leave form is this code works for that any help from you. – Karthik Sep 15 '17 at 07:35
-2

You can use libmail: http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en

include "libmail.php";
$m = new Mail(); // create the mail
$m->From( "leo@isp.com" );
$m->To( "destination@somewhere.fr" );
$m->Subject( "the subject of the mail" );
$m->Body( "Hello\nThis is a test of the Mail component" );
$m->Cc( "someone@somewhere.fr");
$m->Priority(4);
//  attach a file of type image/gif to be displayed in the message if possible
$m->Attach( "/home/leo/toto.gif", "image/gif", "inline" );
$m->Send(); // send the mail
echo "Mail was sent:"
echo $m->Get(); // show the mail source
Salim Ibrohimi
  • 1,351
  • 3
  • 17
  • 35