Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

omorhefere

macrumors newbie
Original poster
Sep 4, 2015
1
0
I'm a beginner and i this is my first php project, i keep reading tutorials online about smtp servers and postfix, i've tried using them but they don't seem to work. How do i send e-mails from my local host using xampp. Here's my code

Code:
<?php

$name = $comment = $number = $email = "";

if (isset($_POST['name']))
   $name = fix_string($_POST['name']);
if (isset($_POST['comment']))
   $comment  = fix_string($_POST['comment']);
if (isset($_POST['number']))
   $age      = fix_string($_POST['number']);
if (isset($_POST['email']))
   $email    = fix_string($_POST['email']);

if (isset($_POST['submit'])){
    $to = "efeimoloame1@gmail.com"; // this is your Email address

    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $name . " "  . " wrote the following:" . "\n\n" . $comment;
    $message2 = "Here is a copy of your message " . $name . "\n\n" . $comment;

    $headers = "From:" . $email;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    $maile = mail($email,$subject2,$message2,$headers2);
    // sends a copy of the message to the sender
  if($maile) { echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";}
  else{
    echo "j";
  }
    // You can also use header('Location: thank_you.php'); to redirect to another
}

$fail  = validate_name($name);
$fail .= validate_comment($comment);
$fail .= validate_number($number);
$fail .= validate_email($email);



if ($fail == "")
{
   echo "</head><body>Form data successfully validated:
     $name, $comment,  $number, $email.</body></html>";

   // This is where you would enter the posted fields into a database,
   // preferably using hash encryption for the password.

   exit;
}


function validate_name($field)
{
     return ($field == "") ? "No Name was entered<br>": "";
}

function validate_comment($field)
{
     return($field == "") ? "No comment was entered<br>" : "";
}

   function validate_number($field)
   {
     if ($field == "") return "No phone number  was entered<br>";
     else if ($field.strlen()<11 || $field.strlen()>11)
       return "Phone number should be 11 digits <br>";
     return "";
   }

   function validate_email($field)
   {
     if ($field == "") return "No Email was entered<br>";
       else if (!((strpos($field, ".") > 0) &&
                  (strpos($field, "@") > 0)) ||
                   preg_match("/[^a-zA-Z0-9.@_-]/", $field))
         return "The Email address is invalid<br>";
     return "";
   }

   function fix_string($string)
   {
     if (get_magic_quotes_gpc()) $string = stripslashes($string);
     return htmlentities ($string);
   }
?>
 
Last edited by a moderator:
http://www.w3schools.com/php/php_ref_mail.asp
http://www.w3schools.com/php/func_mail_mail.asp

Respect RFC 2822 and you'll be fine.

This is an example script from the php documentation at http://php.net/manual/en/function.mail.php
Code:
<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

If the basic script above doesn't sendmail, then you probably need to install/configure an MTA on your box (e.g. sendmail).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.