Verbose mailing function I have multiple forms that send an email. As of this moment I have 3 forms that send mail and vary only by the number of inputs:


One requires just an email
Another requires a name and email
Another requires name, email, and a comment


Instead of making 3 separate functions to mail the inputs away, I figured I could consolidate them into 1. The issue here is a lot of if statements. The code all works, but spans around 30 lines, which seems like too much.

function sendMail()
{
$comment = null;
$to =  "destination@mail.com";
$copy = "mymail@mail.com";
$subject = "Email..";
if (isset($_POST["contactSubscribe"]) || isset($_POST["questionSubmit"]) || isset($_POST["submit"])) {  
  foreach ($_POST as $field =&gt; $value) {
    if ($field == "contactName") {
      $contactName = $value;
    }
    if ($field == "email") {
      $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    }
    if ($field == "comment") {
      $comment = $value;
    }
  }
}
if (!is_null($contactName) &amp;&amp; is_null($comment) ) {
  $message = "Name: $contactName Email: $email"; 
}
if (is_null($comment) &amp;&amp; is_null($contactName)) {
  $message = "inputname=i_" . $email;
}
if (!is_null($contactName) &amp;&amp; !is_null($comment) ) {
  $message = "Name: $contactName Email: $email Message: $comment"; 
}
mail($to, $subject, $message);
mail($copy, $subject, $message);
}
I can slightly improve your function, like this:

function array_whitelist($array,$whitelist) 
// only allow certain keys in an array, in reaction to comment by tim
{
  $list = explode(',',$whitelist);
  return array_intersect_key($array,array_flip($list));
}

function sendMail()
{
  // check form submission
  if (is_array($_POST))
  {
    // initialize
    $to      = 'destination@mail.com';
    $copy    = 'mymail@mail.com';
    $subject = 'Email..';
    $message = '';
    // make all posted input fields into local variables
    $inputs  = array_whitelist($_POST,'email,contactName,comment');
    extract($inputs);
    // filter email address
    $email = filter_var($email,FILTER_SANITIZE_EMAIL);
    // add some fields to message 
    if (isset($contactName)) $message .= "Name: $contactName".PHP_EOL;
                        else $message .= "Name: i_".$email.PHP_EOL;
    if (isset($comment)) $message .= "Message: $comment".PHP_EOL; 
    // send mails
    mail($to, $subject, $message, "BCC: $copy");
  }
}


It is clear you seem to overdo the conditions, they are not used very effectively. But I think you have removed some essential parts, so this is never going to work. 

Perhaps it is time you start to think about a more general email function, not just one that will fit your three forms. What if a fourth form comes along? So more than looking at what I did, think about generalizing your email routines. You could make a class. Seperate the building of the mail message from the sending of the emails. You can find enough examples here (search for: 'PHP Email Class').

Also don't forget the next thing you will want to do is send HTML email messages, and add attachments. That's when it becomes really complex. Email HTML and attachments are just plain tricky. Look for a library that can do these things for you, there are some very good ones out there (https://github.com/PHPMailer/PHPMailer, and the like).