1

I have a few questions regarding setting up a PHP mailer.... My first question being, does the PHP file/folders go in the same spot that all of my others (.html, css, and JS)?

If so, then I do indeed need some help setting this up, or am getting a little lost... My PHP file is as follows:

<?php

    $emailTo = "myemail@yahoo.com"; // Enter your email for feedbacks here 

    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=utf-8\r\n";
    $headers .= "From: ".$_POST['rsEmail']."\r\n";

    if (!isset($_POST['rsSubject'])) {
        $subject = "Contact Form Message"; // Enter your subject here
    } else {
        $subject = $_POST['rsSubject'];
    }

    reset($_POST);

    $body = "";
    $body .= "<p><b>Name: </b>".$_POST['rsName']."</p>";
    $body .= "<p><b>Email: </b>".$_POST['rsEmail']."</p>";
    $body .= "<p><b>Subject: </b>".$subject."</p>";
    $body .= "<p><b>Message: </b>".$_POST['rsMessage']."</p>";  

    if( mail($emailTo, $subject, $body, $headers) ){
        $mail_sent = true;
    } else {
        $mail_sent = false;
    }   
    if(!isset($resp)){
        echo json_encode($mail_sent);
    }
?>

And I believe the corresponding javascript I have along with it is:

function isValidEmail(emailAddress) {
        var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
        return pattern.test(emailAddress);
    };

Thank you guys for the assistance! Means a lot :)

Edit: When the form is filled out correctly, it says: "Congratulations your message was sent" -- meaning that it was a valid form submission, I just never actually get the email.

Edit 2: I went to my filezilla to try and edit/view the js file from there, I get the following error: enter image description here

Not entirely sure what that entails (kind of newish to this all). Also here is my form:

                        <form class="rsForm" action="php/mailsender.php" method="post">
                            <div class="input-field">
                                <label>Name</label>
                                <input type="text" name="rsName" value="">
                                <span class="line"></span>
                            </div>

                            <div class="input-field">
                                <label>Email</label>
                                <input type="email" name="rsEmail" value="">
                                <span class="line"></span>
                            </div>

                            <div class="input-field">
                                <label>Subject</label>
                                <input type="text" name="rsSubject" value="">
                                <span class="line"></span>
                            </div>

                            <div class="input-field">
                                <label>Message</label>
                                <textarea rows="4" name="rsMessage"></textarea>
                                <span class="line"></span>
                            </div>

                            <span class="btn-outer btn-primary-outer ripple">
                                <input class="rsFormSubmit btn btn-lg btn-primary" type="submit" value="Send">
                            </span>
                        </form>
John Barr
  • 105
  • 1
  • 8
  • How is your `html` looking? **Never** rely on `javascript` for validating any data, as that is client based and can be influenced by the user. How are you handling the `form` submission is important in this case. – Nytrix Dec 05 '16 at 01:10
  • The `action` part says that you are sending the `input` data to the folder `php` and file `mailsender.php`. So, that code you showed above should be in that file and folder. In case that that is indeed true, look at the answer @AdrianHiggins gave. A quick `debug` trick is also to just spread random messages, and look where it isn't going anymore where you want it to go. – Nytrix Dec 05 '16 at 01:20
  • Yeah I have a php folder with that exact file (above) in the directory. So that seems to be set-up correctly... – John Barr Dec 05 '16 at 01:21
  • If you want I can help you further in this chat: [chat link](http://chat.stackoverflow.com/rooms/129761/conversation-about-phpmailer) – Nytrix Dec 05 '16 at 01:26
  • @Nytrix Still not letting me, maybe recreate room - or make new one? – John Barr Dec 05 '16 at 01:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129762/discussion-between-nytrix-and-john-barr). – Nytrix Dec 05 '16 at 01:40

1 Answers1

-1

so unless your form is on a website that's already hosted by a web hosting service, or you have something like MAMP PRO to test your web page on a local server (PRO because it comes with Postfix), you won't recieve the email.

For the PHP to actually email you, you need a MTA (Mail Transfer Agent) like Postfix. When you get your website hosted, they automatically set one up (usually) for you on their servers. If you are testing locally like putting the file path in your browser, you won't get the email as your local host is not set up with an MTA.

Check out Test emails locally with mamp to learn how to test your email forms without setting your website up on a professional server yet.

If you have any more questions regarding this, tag me in the comments!

PS. This is a very common problem that people don't know about. It got me the first time too, which is why I bought MAMP PRO for testing websites forms.

Community
  • 1
  • 1
  • So lets say for example, I'm using goDaddy and cPanel... They obviously set up some kind of mailing system for you. I don't exactly remember what I did to set it up way back when I made my website. Would I leave the php folder/files in the same directory? Or does it get moved elsewhere? – John Barr Dec 05 '16 at 01:21
  • With `goDaddy` it already is set up for you, than what he is saying should be fine. I mean, you have actually put your own email into the `$emailTo` variable right? – Nytrix Dec 05 '16 at 01:23
  • @Nytrix Yeah I put my real email -- I dont have enough reputation to chat in your chat link lol... – John Barr Dec 05 '16 at 01:34
  • @JohnBarr now you do. – Nytrix Dec 05 '16 at 01:35
  • You don't necessarily need a local MTA - that's why many people use PHPMailer, because it doesn't need one; it has an SMTP client so you can use an external service like Gmail as a relay. Also, this really isn't an answer to the question posted. – Synchro Dec 05 '16 at 08:46