0

I'm trying to retrieve information from text fields on button click and send the data to mail@example.com:

$(document).ready(function() {
    $("#submit").click(function() {
        var email = $("#email").val();
        var name = $("#Name").val();
        var subject = $("#Subject").val();
        var text = $("#textarea").val();
        var telephone = $("#tel").val();
        var varData = "email : " + email + "name : " + name + "subject : " + subject + "text : " + text + "telephone : " + telephone;
        console.log(varData);

        $.ajax({
            type: "POST",
            url: 'sendPHP.php',
            data: varData,
            success: function() {
                alert("message sent");
            }
        });
    });
});

sendPHP.php:

<?php 
  $to = 'mail@example.com';
  $name = $_POST['email'];
  and so on ..
  ..
  ..
  mail(mail@example.com, name, (here goes email, name, subject, textarea, tel) as message in new lines);
    and somewhere i have to write from what emial im sending to example@gmail.com and what is it's password i guess
?>
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
Georgi Antonov
  • 1,581
  • 21
  • 40
  • 2
    And what exactly is your question/problem? – toesslab Nov 19 '15 at 20:20
  • See this question: http://stackoverflow.com/questions/712392/send-email-using-the-gmail-smtp-server-from-a-php-page – toesslab Nov 19 '15 at 20:21
  • my problem is that i dont know the syntax and where i should link the file with the php code.im really new i know i have to read the syntax first just wanted a fast help its for project that require just this php code – Georgi Antonov Nov 19 '15 at 20:27
  • the syntax of the `mail()` function? – toesslab Nov 19 '15 at 20:28
  • If i'm reading it correctly, `varData` is a string that looks like this: `"email : person@example.comname : personsubject: Lorem Ipsumtext: Lorem ipsum dulttelephone : 123-456-7890"` - there's no way to extract the actual fields from that. – samlev Nov 19 '15 at 20:28
  • Try to serialize the `varData` – toesslab Nov 19 '15 at 20:30
  • If all you want to know is how to use the 'mail' function, look at the [php documentation for it](http://php.net/manual/en/function.mail.php). – samlev Nov 19 '15 at 20:32
  • If you are getting the required data in php file then for setting up email refer: http://php.net/manual/en/function.mail.php. Things are pretty clear and simple. – Osama Yawar Khawaja Nov 19 '15 at 20:35
  • just a question how toinclude the php code from php file so that it works on the server i searched and found that i have to rename my .html file with all the HTML of my page to .php and write the php code in the head is this true or im mistaken – Georgi Antonov Nov 19 '15 at 20:43
  • 2
    Dude, ask a clear question in the question area, not in the comments. – Jonathan M Nov 19 '15 at 20:57

1 Answers1

2

While it is still somewhat unclear what your exact concern is, I have a few thoughts on your process.

1 - You are sending data to a php file as a string which is not recommended and is probably giving you problems right there. I have really seen 2 approaches to sending data to the server via post: A) store your data in a form and use jQuery's $().serialize() function, or B) store your data in a JSON object and send it that way.

Option B is my preferred method because JSON simplifies everything. It has the advantage that your data is already stored as key/value pairs, and PHP makes it super easy to work with JSON using the json_decode function. Let's make a few changes to the existing code you have:

$(document).ready(function() {
$("#submit").click(function() {
    var email = $("#email").val();
    var name = $("#Name").val();
    var subject = $("#Subject").val();
    var text = $("#textarea").val();
    var telephone = $("#tel").val();
    var varData = {
                     "email"      : email , 
                     "name"       : name, 
                     "subject"    : subject, 
                     "text"       : text ,
                     "telephone"  : telephone
                   } //This is your data in JSON form

    console.log(varData);

    $.ajax({
        type: "POST",
        url: 'sendPHP.php',
        data: JSON.stringifiy(varData), //Notice that I added JSON.stringify
        success: function() {
            alert("message sent");
        }
    });
});

});

Now I'll show you how to handle this in PHP - it's actually very simple.

Step 1 - turn this JSON object into an associative array so that it's easy to work with:

$input = json_decode(file_get_contents('php://input'), true);

Now we have a variable called $input that is an associative array with all of your mail data - let's set up the variables you need.

$email      = $input['email']; //Notice string in brackets is JSON key
$name       = $input['name'];
$subject    = $input['subject'];
$text       = $input['text'];
$telephone  = $input['telephone'];

Ok, cool - all of your data you gathered from the front end is now ready for use on the back end. So it looks like you're using the built-in mail() function that PHP offers. I would highly recommend using a library for this as they are typically much more reliable and verbose. My favorite is called PHPMailer - here's a link to their github page https://github.com/PHPMailer/PHPMailer.

If you'd like to use that library, the process is simple.

First, include the autoloader file in your script

<?php
    require('PHPMailer-master/PHPMailerAutoload.php');

Next, and they have this documented in numerous examples, you create a new instance of PHPMailer and set some basic variables - this is straight from their documentation and I promise you'll have less headache than if you try the PHP mail() approach https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps.

Best of luck and let me know if I was unclear on anything.

Robert
  • 981
  • 1
  • 15
  • 24
  • you are pretty clear except that i have 1 unussed email which i use to send those emails to the Hotel mail. The point of this task is to allow users to ask somethnig from the Website and and when they click the unused mail sends the data to the hotel mail so the sender mail and the reciever mail are always the same and they do not change so my question is where do i include the username and pass of the sender email – Georgi Antonov Nov 19 '15 at 21:02
  • So, as I stated the first thing you're doing incorrectly is sending a string to the server that you can't parse using the simple post statement. Secondly, if you're using PHP's mail() function you can only send mail from your server to a recipient. Thirdly, your question is extremely unclear and I have no idea what you mean by unused email and hotel email. If my answer was helpful an up vote or marking it as correct would be appreciated. – Robert Nov 19 '15 at 21:07