-1

I want to link up my contact form to be able to send to my email. However, I have zero knowledge in the back-end php scrips.

I've tried looking up some basic ones but none of them have worked out. I've even tried looking at some of the tutorials online. I really only need the fields to be linked up to an email. Also, the required fields in php aren't needed because i'm just going to use the html require validation.

  <div class="contactme">
    <div class="col-md-6 contactform">
        <h2>Contact</h2>
        <hr />
        <form class="col-md-12" id="contact-form" method="post" action="contact.php" role="form">
            <div class="messages"></div>
            <div class="controls">
                <div class="row">
                    <div style="padding: 20px;" class="col-md-6">
                        <div class="form-group">
                            <label for="form_name">Firstname *</label>
                            <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required." />
                            <div class="help-block with-errors"></div>
                        </div>
                    </div>
                    <div style="padding: 20px;" class="col-md-6">
                        <div class="form-group">
                            <label for="form_lastname">Lastname *</label>
                            <input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required." />
                            <div class="help-block with-errors"></div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div style="padding: 20px;" class="col-md-6">
                        <div class="form-group">
                            <label for="form_email">Email *</label>
                            <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required." />
                            <div class="help-block with-errors"></div>
                        </div>
                    </div>
                    <div style="padding: 20px;" class="col-md-6">
                        <div class="form-group">
                            <label for="form_need">Please specify your need *</label>
                            <select id="form_need" name="need" class="form-control" required="required" data-error="Please specify your need.">
                                <option value=""></option>
                                <option value="Request Project">Request Project</option>
                                <option value="Request order status">Recruiter Looking To Recruit</option>
                                <option value="Other">Other...</option>
                            </select>
                            <div class="help-block with-errors"></div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div style="padding: 20px;" class="col-md-12">
                        <div class="form-group">
                            <label for="form_message">Message *</label>
                            <textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="Please, leave us a message."></textarea>
                            <div class="help-block with-errors"></div>
                        </div>
                        <div class="col-md-12 text-center">
                            <input id="sendBtn" type="submit" class="btn btn-send" value="Send message" />
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div style="padding: 20px;" class="col-md-12">
                        <p class="text-muted">
                            <strong>*</strong> These fields are required.
                        </p>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

sorry if it isn't properly formatted, I'm on my mobile phone. format on save should do the trick though.

Amanjot Kaur
  • 2,028
  • 4
  • 18
  • 33
  • 1
    this is just html code... show us the php code for sending mail that you have tried – Amanjot Kaur Nov 12 '19 at 07:07
  • *Also, the **required fields in php aren't needed** because i'm just going to use the html require validation.* - no just no, you will risk your application, please [Dont Trust User Input](https://www.owasp.org/index.php/Don%27t_trust_user_input). anyway, after you done with the html, you will need to handle the email sending in your `contact.php`. you can refer to [this QA](https://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php) for example that uses SMTP server. however, do remember to always sanitize the parameters and disallow new line (`\n\r`) for security purpose. – Bagus Tesa Nov 12 '19 at 07:11
  • @BagusTesa Ok, thank you. – Grayson McMurry Nov 12 '19 at 07:15

1 Answers1

-1

As a possible solution you could perhaps try like this. It is tested only in so far as the form submits, processes the reuqest and tries to send the email but fails as I have no mail server and the email address / domain are clearly bogus and require modification.

<?php
    error_reporting( E_ALL );
    ini_set( 'display_errors', 1 );

    if( $_SERVER['REQUEST_METHOD']=='POST' ){

        define( 'DOMAIN', 'x-marks-the-spot.com' ); #change
        define( 'WEBMASTER', sprintf( 'webmaster@%s', DOMAIN ) ); #change if req'd
        define( 'ENQUIRIES', sprintf( 'enquiries@%s', DOMAIN ) ); #change if req'd
        define( 'SUBJECT', sprintf( 'Enquiry for %s', DOMAIN ) ); #change if req'd



        $errors=array();
        $args=array(
            'name'      =>  FILTER_SANITIZE_STRING,
            'surname'   =>  FILTER_SANITIZE_STRING,
            'email'     =>  FILTER_SANITIZE_EMAIL,
            'need'      =>  FILTER_SANITIZE_STRING,
            'message'   =>  FILTER_SANITIZE_STRING
        );

        foreach( array_keys( $args ) as $field ){
            if( !isset( $_POST[ $field ] ) ) $errors[]=sprintf( 'The field "%s" is not set', $field );
        }

        foreach( $_POST as $field => $value ){
            if( !in_array( $field, array_keys( $args ) ) )$errors[]=sprintf( 'Unknown field "%s"', $field );
        }

        if( empty( $errors ) ){
            $_POST=filter_input_array( INPUT_POST, $args );
            extract( $_POST );



            $message = sprintf("
                    Name: %s\n
                    Surname: %s\n\n
                    Requirement: %s\n\n
                    Message: %s\n",
                $name,
                $surname,
                $need,
                $message
            );
            $headers=array(
                sprintf('From: %s',$email ),
                sprintf('Reply-To: %s', WEBMASTER )
            );


            $status=mail( ENQUIRIES, SUBJECT, $message, implode( PHP_EOL, $headers ) );
            header( sprintf( 'Location: ?mailsent=%s', $status ? 'true' : 'false' ) );
        }
    }


?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
    </head>
    <body>



        <div class="contactme">
                <div class="col-md-6 contactform">
                    <h2>Contact</h2>
                    <hr />
                    <!--

                        form action removed purely for purposes of this
                        script.... copy above PHP code to `contact.php`
                        if that is a different page than the one containing
                        this form. If it is the same page, omit the action.

                    -->

                    <!-- action="contact.php" -->

                    <form
                    class="col-md-12"
                    id="contact-form"
                    method="post"
                    role="form">

                    <div class="messages"></div>

                    <div class="controls">
                        <div class="row">
                            <div style="padding: 20px;" class="col-md-6">
                                <div class="form-group">
                                    <label for="form_name">Firstname *</label>
                                    <input
                                        id="form_name"
                                        type="text"
                                        name="name"
                                        class="form-control"
                                        placeholder="Please enter your firstname *"
                                        required="required"
                                        data-error="Firstname is required."
                                    />
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div style="padding: 20px;" class="col-md-6">
                                <div class="form-group">
                                    <label for="form_lastname">Lastname *</label>
                                    <input
                                        id="form_lastname"
                                        type="text"
                                        name="surname"
                                        class="form-control"
                                        placeholder="Please enter your lastname *"
                                        required="required"
                                        data-error="Lastname is required."
                                    />
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div style="padding: 20px;" class="col-md-6">
                                <div class="form-group">
                                    <label for="form_email">Email *</label>
                                    <input
                                        id="form_email"
                                        type="email"
                                        name="email"
                                        class="form-control"
                                        placeholder="Please enter your email *"
                                        required="required"
                                        data-error="Valid email is required."
                                    />
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div style="padding: 20px;" class="col-md-6">
                                <div class="form-group">
                                    <label for="form_need">Please specify your need *</label>
                                    <select
                                        id="form_need"
                                        name="need"
                                        class="form-control"
                                        required="required"
                                        data-error="Please specify your need."
                                    >
                                        <option value=""></option>
                                        <option value="Request Project">Request Project</option>
                                        <option value="Request order status"
                                            >Recruiter Looking To Recruit</option
                                        >
                                        <option value="Other">Other...</option>
                                    </select>
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div style="padding: 20px;" class="col-md-12">
                                <div class="form-group">
                                    <label for="form_message">Message *</label>
                                    <textarea
                                        id="form_message"
                                        name="message"
                                        class="form-control"
                                        placeholder="Message for me *"
                                        rows="4"
                                        required="required"
                                        data-error="Please, leave us a message."
                                    ></textarea>
                                    <div class="help-block with-errors"></div>
                                </div>
                                <div class="col-md-12 text-center">
                                    <input
                                        id="sendBtn"
                                        type="submit"
                                        class="btn btn-send"
                                        value="Send message"
                                    />
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div style="padding: 20px;" class="col-md-12">
                                <p class="text-muted">
                                    <strong>*</strong> These fields are required.
                                </p>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>



    </body>
</html>

Bascially, in the simplest form the PHP mail function can operate like this:

$returnvalue = mail( $email, $subject, $message );

$email is to whom the message will be sent. To include the sender or reply-to these are done using the headers argument ( the fourth argument - not shown )

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • that's awesome. thank you for that. I was able to set up my domain and configure accordingly. I'm just trying to figure out. Where do I put my email? – Grayson McMurry Nov 12 '19 at 08:04
  • which email would that be? – Professor Abronsius Nov 12 '19 at 08:14
  • just any email lets just say `blah@gmail.com`. I was able to make a domain and input my domain in the domain field above, now i guess the next step is inserting the email. – Grayson McMurry Nov 12 '19 at 08:16
  • do you mean the email of the person using the contact form or the address to whom the email will be delived at your domain? – Professor Abronsius Nov 12 '19 at 08:19
  • i'm guessing i would need both. Either way, whichever one would help me in my case. Thank you – Grayson McMurry Nov 12 '19 at 08:20
  • if you follow the above, the email would be sent to ENQUIRIES and would appear to originate from $email ( what the user entered in the form )... there are other ways to set this up ( such as having the message appear to be sent from WEBMASTER or ENQUIRIES etc ) ~ it is a matter of preference and for you to decide. I suggest reading through the documentation - https://www.php.net/manual/en/function.mail.php – Professor Abronsius Nov 12 '19 at 08:29
  • Never extract post variables! – Dharman Nov 12 '19 at 10:08
  • they should be relatively safe at that point unless the filtering has failed – Professor Abronsius Nov 12 '19 at 11:20