0

I have a question with my code. I created a form for collecting emails. enter image description here

In my html I used the <form> tag to reference signup.php

HTML: (form part)

<form method="post" action="php/signup.php" name="cform" id="cform">
    <div class="form-row">
    <div class="col-12 col-md-9 mb-2 mb-md-0">
        <input name="email" id="email" type="text" class="form-control form-control-lg" placeholder="Enter your email...">
    </div>
       <div class="col-12 col-md-3">
         <button type="submit" class="btn btn-block btn-lg btn-primary">Sign up!</button>
       </div>
     </div>
</form>

In my signup.php I tried many different things to get it to send to my email but it doesn't send the email from form to my email with subject. I believe I followed PHP's mail class *mail(sendtoemail,subject,body,headers) format. Thanks!!!

PHP (php/signup.php)

<?php

    if(!$_POST) exit;
    // Email address verification, do not edit.
    function isEmail($email) {
        return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
    }
    if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
    $email = $_POST['email']
      if (trim($email) == '') {
        echo 'Please enter a valid email address.';
        exit();
    } else (!isEmail($email)) {
        echo 'You have entered an invalid e-mail address. Please try again.';
        exit();
    }

    $address = "p****@*******tech.mx";
    $to_email = "p****@*******tech.mx";


    $e_body = "You have been contacted by $email"  . PHP_EOL . PHP_EOL;
    $e_reply = "You can contact them via $email";
    $message = wordwrap ($e_body . $e_reply, 70);

    $headers = "From: $address" . PHP_EOL;
    $headers .= "Reply-To: $email" . PHP_EOL;
    $headers .= "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
    $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;


    if(mail($to_email, $e_subject, $message, $headers)) {
        echo "<fieldset>";
        echo "<div id='success_page'>";
        echo "<h3>Email Sent Successfully.</h3>";
        echo "<p>Thank you <strong>$name</strong>, your message has been submitted to Renovatio.</p>";
        echo "</div>";
        echo "</fieldset>";
        } 

        else {
        echo 'ERROR!';
    }
    ?>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150

1 Answers1

0

First, you need to assign a value to e_subject, because according to your code, e_subject is empty. And then remember that sometimes and depending on which host service you have, only authenticated accounts can send emails, preventing spam. Think about use a lib like PHPMailer

Gabriel Souto
  • 600
  • 7
  • 19