• Anonymous User 12508426

    (@anonymized-12508426)


    What I want to do is to have a custom contact form with SMTP which will send emails to the receipient and the From email address of that email will be the one which is taken as input from the form.

    I have written this below code for contact form in contact.php:

    <?php
    // Please specify your Mail Server - Example: mail.example.com.
    //ini_set("SMTP","localhost");
    // Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
    //ini_set("smtp_port","25");
    // Please specify the return address to use
    //ini_set('sendmail_from', 'example@localhost');
    
    // define variables and set to empty values
    $sendernameErr = $emailErr = $websiteErr = $commentErr = "";
    $sendername = $email = $comment = $website = "";
    $sendernameErrFlag = $emailErrFlag = $websiteErrFlag = $commentErrFlag = 2;
    //Set variable inputs from form.
      function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if ($referrer == "") {
            $referrer = "This page was accessed directly";
        }
      if (empty($_POST["sendername"])) {
        $sendernameErr = "Name is required";
        $sendernameErrFlag = 1;
      } else {
        $sendername = test_input($_POST["sendername"]);
        // check if sendername only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$sendername)) {
          $sendernameErr = "Only letters and white space allowed";
          $sendernameErrFlag = 1;
        } else {
          $sendernameErrFlag = 0;
      }
      }
    
      if (empty($_POST["email"])) {
        $emailErr = "Email is required";
        $emailErrFlag = 1;
      } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailErr = "Invalid email format";
          $emailErrFlag = 1;
        } else {
          $emailErrFlag = 0;
      }
      }
    
      if (empty($_POST["website"])) {
        $website = "";
      } else {
        $website = test_input($_POST["website"]);
        // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
          $websiteErr = "Invalid URL";
          $websiteErrFlag = 1;
        } else {
          $websiteErrFlag = 0;
      }
      }
      if (empty($_POST["comment"])) {
        $commentErr = "Comment is required";
        $commentErrFlag = 1;
      } else {
        $comment = test_input($_POST["comment"]);
        $commentErrFlag = 0;
      }
    }
    ?>
    <p><span class="error">* required field.</span></p>
    <form method="post" action="<?php echo the_permalink();//htmlspecialchars($_SERVER["REQUEST_URI"], ENT_QUOTES, "utf-8");?>">
        Name: <input type="text" name="sendername" value="<?php echo $sendername;?>">
      <span class="error">* <?php echo $sendernameErr;?></span>
      <br><br>
      E-mail: <input type="text" name="email" value="<?php echo $email;?>">
      <span class="error">* <?php echo $emailErr;?></span>
      <br><br>
      Website: <input type="text" name="website" value="<?php echo $website;?>">
      <span class="error"><?php echo $websiteErr;?></span>
      <br><br>
      Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
      <span class="error">* <?php echo $commentErr;?></span>
      <br><br>
      <input type="submit" name="submit" value="Submit">
    </form>
    <?php
    /*
    echo "<h2>Your Input:</h2>";
    echo $sendername;
    echo "<br>";
    echo $email;
    echo "<br>";
    echo $website;
    echo "<br>";
    echo $comment;
    echo "<br>";
    echo $headers;
    */
    //print_r($_POST);
    //echo $sendernameErrFlag . "<br/>" . $emailErrFlag . "<br/>" . $websiteErrFlag . "<br/>" . $commentErrFlag;
    if($sendernameErrFlag == 0 && $emailErrFlag == 0 && $websiteErrFlag == 0 && $commentErrFlag == 0) {
        $headers = "From: <".$_POST["sendername"].">".$_POST["email"];
        mail("test@gmail.com","My subject",$comment,$headers);
        echo '<script>alert("Message sent successfully!!!");</script>';
    } else {
        $sendernameErrFlag = $emailErrFlag = $websiteErrFlag = $commentErrFlag = 2;
    }
    ?>

    and for SMTP in functions.php

    /* Email SMTP */
    add_action( 'phpmailer_init', 'my_phpmailer_init' );
    function my_phpmailer_init( PHPMailer $phpmailer ) {
        $phpmailer->IsSMTP();
        $phpmailer->Host = 'localhost';
        $phpmailer->Port = 25; // could be different
        //$phpmailer->Username = 'test@example.com'; // if required
        //$phpmailer->Password = 'password'; // if required
        $phpmailer->SMTPAuth = false; // if required
        //$phpmailer->SMTPSecure = 'tls'; // enable if required, 'tls' is another possible value
    }

    The email isn’t working with Gmail SMTP credentials even if the SMTP credentials are correct. With localhost it is working but the headers are not setting up properly in the email. The From address of the email is default server address instead of the email address taken from the contact form.

    How do I fix this?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘How to setup dynamic SMTP and contact form in WordPress via coding with built-in’ is closed to new replies.