skip to Main Content

I’m trying to figure out how to have an email system on a site I’m building. The goal is three input spaces (their name, email, and message) and the system take that input and send it in an email. Or even if there’s a way to just get the email/message sent from the site to my email.

I was also wondering if there’s a way to edit the text box or anything for this system using CSS, if so, I’ll toy with it and figure it out. If not, any suggestions of how to go about it?

I tried writing a .php script and here’s what I wrote:

<?php
   if($_POST["message"]) {
      mail("[email protected]", "Message via site",
      
      $_POST["insert message here"]. "From: [email protected]");
   }
?>

And the HTML that gets the form box:

<form method "post" action = "emailform.php">
   <textarea name = "message"></textarea>
   <input type = "submit">
</form>

However, this ends up saving the .php script to my computer and no email or anything is sent. I know I’m doing something wrong here, but this was at the very least supposed to be a test to see if the system works with the remaining input boxes and tuning to be done later.

2

Answers


  1. You could start doing something like this, in a HTML file:

    <form action="email-sender.php" method="post">
        <label for="name">Name:</label>
        <input id="name" type="text" name="name" required>
        <label for="email">Email:</label>
        <input id="email" type="email" name="email" required>
        <label for="message">Message:</label>
        <textarea id="message" name="message" required></textarea>
        <button type="submit">Submit</button>
    </form>
    

    And then you write an PHP file like this:

    <?php
    //These variables are optional, you can assign the $_POST values to other variables if you want to manipulate the values
    $email = $_POST['email'];
    $name = $_POST['name'];
    $message = $_POST['message'];
    
    mail('[email protected]', 'Message Via Website', 'Email sent by ' . $name . ' from ' . $email . " saying:n" . $message);
    
    //This function redirects to another page
    header('Location: myHTMLfile.html');
    

    After this you can test it by installing the apache server on your device and then install the PHP module manually, or you could install an easy to use Web Server like XAMPP, download it here: https://www.apachefriends.org/download.html

    Then if you choose to use XAMPP, you just need to configure the SMTP server on the php.ini and sendmail.ini files or in the case you chose the first option, you’ll need to install sendmail PHP module and then do a bit more complicated configuration also involving the php.ini and sendmail.ini files.

    Hope this helps.

    Login or Signup to reply.
  2. I also recommend PHPMailer library (https://github.com/PHPMailer/PHPMailer) since it is easy to use.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search