Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am beginner to php.I am trying to implement mail() in PHP.Code looks correct but the mail sending is failed.Here is my code.

XML
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form method="post" action="mail.php">
  Email: <input name="email" type="text" /><br />
  Subject: <input name="subject" type="text" /><br />
  Message:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
  </form>
  </body>
  </html>



PHP
<?php
//if "email" variable is filled out, send email

  //Email information
  $admin_email = "jsathish1989@gmail.com";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $comment = $_REQUEST['comment'];

  //send email
  $mail=mail($admin_email, "$subject", $comment, "From:" . $email);
  if($mail){
  echo "Thank you for using our mail form";
}else{
  echo "Mail sending failed.";
}

?>



If I need to do anything with php.ini file, then what exactly I should do in that file?

CSS
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =
Posted
Updated 30-Oct-14 1:31am
Comments
Adrian Fatol 30-Oct-14 6:39am    
do you have the smtp server setup on your localhost?
Janardhanam Julapalli 30-Oct-14 6:41am    
no
Janardhanam Julapalli 30-Oct-14 6:41am    
how can i do that

1 solution

As you don't have a smtp server on your localhost, I suggest to use the gmail smtp for start. PHPMailer[^] is a great library that you can use and here you can find an example about how to use it with gmail: https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps[^].

If you use the PHPMailer, then you should change your mail.php to something like:

PHP
<?php

  //Email information
  $admin_email = "jsathish1989@gmail.com";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $comment = $_REQUEST['comment'];

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require 'path_to_mailer/PHPMailerAutoload.php';

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "YOUR_GMAIL_USER@gmail.com";
$mail->Password = "YOUR_GMAIL_PASSW";
$mail->SetFrom($email);
$mail->Subject = $subject;
$mail->Body = $comment;
$mail->AddAddress($admin_email);
 if(!$mail->Send())
 {
    echo "Mailer Error: " . $mail->ErrorInfo;
 }
    else
 {
    echo "Message has been sent";
 }
?>
 
Share this answer
 
v2
Comments
Janardhanam Julapalli 30-Oct-14 6:53am    
Do i need to download this library and place it in my required server location
Adrian Fatol 30-Oct-14 6:56am    
Yes, you have to download it and you can place it in the same folder as the rest of the files (eg mail.php). Then you can include it using:
require 'PHPMailerAutoload.php';
Janardhanam Julapalli 30-Oct-14 7:02am    
I have placed the PHPMailer-master folder in the same directory where my code is present.Now I need to include this require 'PHPMailerAutoload.php'; in my .php file right?
Adrian Fatol 30-Oct-14 7:09am    
right, in your mail.php
Janardhanam Julapalli 30-Oct-14 7:13am    
previously I was getting "mail sending failed" as echo.Now I am not getting anything.When I see in console(network) it is showing 500-Internal server error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900