Validation before sending mail in PHP In my initial problem posted on SO, whenever anyone accessed the Mail.php on the server, it used to send an empty email to the $to. To avoid this I came up with a solution.
require 'PHPMailer/PHPMailerAutoload.php';
    
    $yourName = $_POST['yourName'];
    $sender  = $_POST['emailID'];
    $subject  = $_POST['subject'];
    $message  = $_POST['message'];
    $to = 'email@email.com';

        if(empty($yourName) || empty($sender) || empty($subject) || empty($message) || empty($message))
        {
            echo &quot;Fields are empty&quot;;
        }
        else
        {
                $mail = new PHPMailer;
                //$mail-&gt;SMTPDebug = 2;                               
                $mail-&gt;isSMTP();                                      
                $mail-&gt;Host = 'smtp.gmail.com';  
                $mail-&gt;SMTPAuth = true;                               
                $mail-&gt;Username = 'gmailUser';      
                $mail-&gt;Password = 'gmailPassword';                     
                $mail-&gt;SMTPSecure = 'tls';                            
                $mail-&gt;Port = 587;                                    

                $mail-&gt;setFrom($sender,$yourName);
                $mail-&gt;addAddress($to);     
                $mail-&gt;addReplyTo($sender);

                $mail-&gt;isHTML(true);                                  

                $mail-&gt;Subject = $subject;
                $mail-&gt;Body    = &quot;&lt;b&gt;From: &lt;/b&gt;&quot;. $sender. &quot;&lt;br&gt;&quot; .&quot; &lt;b&gt;Name: &lt;/b&gt;&quot;. $yourName. &quot;&lt;br&gt;&quot;. &quot;&lt;b&gt; Message Body &lt;/b&gt;&quot; .$message;
                $mail-&gt;AltBody = &quot;&lt;b&gt;From: &lt;/b&gt;&quot;. $sender. &quot;&lt;br&gt;&quot; .&quot; &lt;b&gt;Name: &lt;/b&gt;&quot;. $yourName. &quot;&lt;br&gt;&quot;. &quot;&lt;b&gt; Message Body &lt;/b&gt;&quot; .$message;

                if(!$mail-&gt;send()) 
                {
                    echo 'Message could not be sent.';
                    echo 'Mailer Error: ' . $mail-&gt;ErrorInfo;
                } 
                else 
                {
                    echo &quot;Message has been sent....You're being redirected.....&quot;;
                }
        }

This fix, basically allows the user to interact with Mail.php, but Mail.php doesn't sends a null email to $to
Now hours later, I find out that this is a very bad approach.

I would like to know how a professional would solve this problem?
Any good approach which I could use to efficiently optimize the code?
Validate your fields individually so you can provide the client side with better error messages (assuming this is an AJAX script)...

$reqd_params = array('yourName', 'emailID', 'subject', 'message');
foreach($reqd_params as $param)
    !empty($_REQUEST[$param]) || die("$param parameter is required.");


The AltBody paramter is supposed to be the plaintext version of the regular body, yours has html in it. You need to do wrap it in striptags and nl2br like so... striptags(nl2br($my_html_message))

Avoid returning plain text from an AJAX script too. Set your properties in an array and return it in JSON so Javascript can read it easier.

for example:

header("content-type: application/json");
echo json_encode(array(
    "success" =&gt; true,
    "message" =&gt; "email_sent"
));


Edit.. I said nl2br but what u need is the opposite of that..