-2

I am using a dialog box to send an e-mail. A dialog box opens when a user a click on 'assign' button. Here is my HTML output:http://jsfiddle.net/DBWc2/1/.

How can i merge the below PHP code to send email, when a user clicks on 'send email' button of a dialog box.

here is a PHP code i have got from W3schools.com

<?php
 $to = "someone@example.com";
 $subject = "Test mail";
 $message = "Hello! This is a simple email message.";
 $from = "someonelse@example.com";
 $headers = "From:" . $from;
 mail($to,$subject,$message,$headers);
 echo "Mail Sent.";
?>
  • You need to use AJAX for this. Also, be careful with directly including the `$from` there -- somebody could inject malicious headers or even insert malicious attachments. See this page on [CRLF Injection](https://www.owasp.org/index.php/CRLF_Injection). – elixenide Jan 26 '14 at 08:24

1 Answers1

1

Just add an action to your form:

<form action="sendEmail.php" id="dialog" method="post" novalidate="novalidate"> 

then put your php code inside sendEmail.php:

<?php
 $to = "someone@example.com";
 $subject = "Test mail";
 $message = "Hello! This is a simple email message.";
 $from = "someonelse@example.com";
 $headers = "From:" . $from;
 mail($to,$subject,$message,$headers);
 echo "Mail Sent.";
?>

In this case, sendEmail.php is the same directory as your HTML file. It's can be different location based on your current project structure.

If you don't want to refresh the page or redirect to another page to send email. You can take a look at Ajax.

Felix
  • 37,892
  • 8
  • 43
  • 55
  • Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\10CE101\rfp_final\send_email.php. This error turns up. – user3212202 Jan 27 '14 at 05:06
  • @user3212202 Don't use normal `mail` function. Use a library such as [SwiftMailer](http://swiftmailer.org/) to do it. You can go through this question on how to send gmail using `swiftmailer`: http://stackoverflow.com/questions/3478906/using-phps-swiftmailer-with-gmail – Felix Jan 27 '14 at 05:10