ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

Working code to send emails from php?

user-image
تم إضافة السؤال من قبل Presley Rebello , Sales Executive , Goa Rajee Auto Pvt. Ltd. GOA
تاريخ النشر: 2014/04/05
Hira Ansari
من قبل Hira Ansari , Project Manager / Website Developer (Wordpress / PHP / HTML5) - Design Develop 'n' SEO , Desing Develop SEO UK

 

>> PHP script file:

Create a new file, name it email_script.php and save it in the root folder after this copy the following php code, paste it in the email_script.php file and save it again.

 

<?php

if(isset($_POST['email']))

{

$email_to= ",";

$email_subject= "Message title for example: Rent an Office Enquiry:";

    

$name = $_POST['name'];

    $email_from = $_POST['email']; 

    $telephone = $_POST['telephone'];

$subject = $_POST['subject']; 

$newsletter = $_POST['newsletter']; 

$message = $_POST['message']; 

$email_message = "Form details below.\\n\\n";

     

    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }

     

    $email_message .= "Name: ".clean_string($name)."\\n";

    $email_message .= "Email: ".clean_string($email_from)."\\n";

    $email_message .= "Telephone: ".clean_string($telephone)."\\n";

    $email_message .= "Subject: ".clean_string($subject)."\\n";

    $email_message .= "Message: ".clean_string($message)."\\n";

    $email_message .= "Subscribe for Emails: ".clean_string($newsletter)."\\n";

   $thankyou="Mail Sent!! Thank you " . $name . ", we will contact you shortly.<br /> Go back to <a href="http://www.consultseouk.co.uk/">Homepage</a>";

// Email Header

$headers = 'From: '.$email_from."\\r\\n".

'Reply-To: '.$email_from."\\r\\n" .

'X-Mailer: PHP/' . phpversion();

    // You can also use header('Location: thank_you.php'); to redirect to another page.;

@mail($email_to, $email_subject, $email_message, $headers);  

echo $thankyou;

    }

?>

 

 

>> HTML Contact Form: 

Now Here is the Html code to create the form fields within a table:

 

<div class="widgets contact-form-position" >

 

<div class="homepage-contact-us-form widget-child-box" >

<div class="widget-h3-a"> <h3>Contact Us</h3></div>

 

<table>

<form action="http://www.yourdomain.com/email_script.php" method="POST" name="contactform"><input type="hidden" name="action" value="submit" />

<tr><td>  <label for="Keyword">Name:</label> </td></tr>

<tr><td>  <input type="text" name="name" tabindex="2" value="" />  </td></tr>

<tr><td>  <label for="name">Email:</label>                 </td></tr>

                   <tr><td> <input type="text" name="name" id="name" value="" tabindex="1" />

             </td></tr>

<tr><td><label for="Keyword">Mobile:</label></td></tr>

<tr><td><input type="text" name="phone" tabindex="2" value="" /></td></tr>

<tr><td><label for="textarea">Enquiry:</label></td> </tr>

<tr><td><textarea cols="40" rows="8" name="message" id="textarea"></textarea></td></tr>

<tr><td><input class="checkbox" type="checkbox" name="checkbox" id="checkbox" />

<label for="checkbox"><small>Send me weekly emails for new properties</small></label></td></tr>

<tr><td> <input class="contact-button" type="submit" value="Submit" /></td></tr>

</form> 

</table>

</div>

</div>      

 

 

Don't forget to replace your email ids with the example email ids on line:    

 

$email_to= ",";

 

you can use multiple ids at a time just by using comma ","  after each email id.    

 

 

kareem zock
من قبل kareem zock , Founder & Blogger , Techwebies

Well there is a lot of ways to send mails using mail function in php but the best is to organize ur code and work .. check this example and see how you can organize your code by using functions and classes 

https://github.com/PHPMailer/PHPMailer

Ali Fayyaz
من قبل Ali Fayyaz , PHP Web Developer , Ilead Digital

<html>

<body>

<?php function spamcheck($field)   {   // Sanitize e-mail address   

$field=filter_var($field, FILTER_SANITIZE_EMAIL);   // Validate e-mail address

if(filter_var($field, FILTER_VALIDATE_EMAIL))     {

    return TRUE;     

}   else     {

    return FALSE;   

  }  

 }?>

<h2>Feedback Form</h2>

<?php// display form if user has not clicked submitif (!isset($_POST["submit"]))  {  ?>

 <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

  From: <input type="text" name="from">

<br>

  Subject: <input type="text" name="subject">

<br>

Message: <textarea rows="10" cols="40" name="message"></textarea>

<br>

<input type="submit" name="submit" value="Submit Feedback"> 

</form>

  <?php   }else  // the user has submitted the form  {  // Check if the "from" input field is filled out 

if (isset($_POST["from"]))    {    // Check if "from" email address is valid

$mailcheck = spamcheck($_POST["from"]);

  if ($mailcheck==FALSE)       {

      echo "Invalid input";    

  }     else       { 

     $from = $_POST["from"]; // sender

      $subject = $_POST["subject"];

      $message = $_POST["message"];      // message lines should not exceed70 characters (PHP rule), so wrap it     

$message = wordwrap($message,70);      // send mail      mail("",$subject,$message,"From: $from\\\\n");

echo "Thank you for sending us feedback";    

  }

    }

  }?>

</body>

</html>

Afsheen Atif
من قبل Afsheen Atif

<?php

$to      = ';

$subject = 'Php email test';

$message = "Assalam alaikum.This is just a test email.";

$headers .= 'From:  coder : <>' . "\\r\\n" .

    'Reply-To: ariya : <>' . "\\r\\n" .

    'X-Mailer: PHP/' . phpversion();

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

echo "email sent";

?>

Cheers!

Noor Khan
من قبل Noor Khan , Technical Team Lead , Ammrys Studios

$to      = '';

$subject = 'the subject';

$message = 'hello';

$headers = 'From: ' . "\\r\\n" .    'Reply-To: ' . "\\r\\n" .    'X-Mailer: PHP/' . phpversion();

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

Refrence: http://php.net/manual/en/function.mail.php

المزيد من الأسئلة المماثلة