PHP for E-mail Sending

-
Unknown

E-Mail is now a pretty popular way to get your users always updated about status that what is happening on your site or blog. In this PHP can help you a lot. It is very easy to use this function once your script is ready. The php function can also send mails automatically to user on adding any update to your website or blog. For this mailing process php has added a function mail() to send mail via script or you an say via web server. The syntax of this function is

mail(to, subject, message, headers, parameters)

So it is as simple as using echo in your document. The brief information about mail() functions you can read below

  •  To - Specifies the receiver / receivers of the email
  • SubjectSpecifies the subject of the email. Note - This parameter cannot contain any newline characters
  • Message Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters
  • Headers Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n)
  • Parameters Specifies an additional parameter to the sendmail program
Please note again -For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.
Now its time for a good example to explain this very easily and clearly, So have a look

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

Now lets see how to use if and else for better mailing and to stop unwanted email sending every time on opening a page. See how we can make it

if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //sending email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail("someone@example.com", $subject,
  $message, "From:" . $email);
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }

Here email will be sent only the form is filled, if form is not filled than the form will appear again to user. So when the submit button is clicked, page reloads and mail will be sent to given address.


Now , what more is left in post? Well, the last thing left is validating spams which is one of major problems. So we can also deal with spams. Just look this awesome code -


function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }

if (isset($_REQUEST['email']))
  {//if "email" is filled out, proceed

  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {//send email
    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail("someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
    }
  }
else
  {//if "email" is not filled out, display the form
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }


Now here we use to new tags in above function 'spamcheck' .

  • The FILTER_SANITIZE_EMAIL filter removes all illegal e-mail characters from a string
  • The FILTER_VALIDATE_EMAIL filter validates value as an e-mail address
So here our this post ends. Leave comments for further information.

One Response so far.

  1. Its helpfull.............

Leave a Reply