JaySharma Posted February 20, 2014 Share Posted February 20, 2014 <?php header('Content-type: application/json'); $status = array( 'type'=>'success', 'message'=>'Email sent!' ); $name = @trim(stripslashes($_POST['name'])); $email = @trim(stripslashes($_POST['email'])); $subject = @trim(stripslashes($_POST['subject'])); $message = @trim(stripslashes($_POST['message'])); $email_from = $email; $email_to = 'free.bks@gmail.com'; $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message; $success = @mail($email_to, $subject, $body,'From: <'.$email_from.'>'); echo json_encode($status); die; This above is the php email script I have . Form code I am using is below <div class="status alert alert-success" style="display: none"></div> <form id="contact-form" method="POST" class="row mt10 style1 contact-form" action="sendemail.php" name="contact-form"> <div class="col-md-6"> <input type="text" name="name" value="Enter your full name" required="required" onfocus="if(this.value=='Enter your full name')this.value=''" onblur="if(this.value=='')this.value='Enter your full name';" placeholder="name"/> </div> <div class="col-md-6"> <input type="text" name="email" placeholder="email" value="Enter your full e-mail" required="required" onfocus="if(this.value=='Enter your full e-mail')this.value=''" onblur="if(this.value=='')this.value='Enter your full e-mail';" /> </div> <div class="col-md-12"> <textarea rows="7" cols="5" name="message" placeholder="message" id="message" required="required" onfocus="if($(this).val()=='Write your message...'){$(this).val('');}" onblur="if(this.value=='')this.value='Write your message...';" >Write your message...</textarea> <div class="submit_btn"> <div class="inner"> <input class="submit btn" type="submit" value="Submit" name="" /> </div> </div> </div> </form> I am getting emails , been trying for the last few hours changing things , but its not working ; may be I am overlooking something important . I am learning php . Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/ Share on other sites More sharing options...
jairathnem Posted February 20, 2014 Share Posted February 20, 2014 what is the purpouse of echo json_encode($status)? It is predefined and will output the same array regardless of whether the mail is sent or not. I'd suggest you remove that. Are you trying this from a local server? if so, local servers need to be configured to use SMTP to send mails. Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469611 Share on other sites More sharing options...
JaySharma Posted February 20, 2014 Author Share Posted February 20, 2014 echo json_encode($status); outputs the message after i click on submit button . Removing it only stops displaying the message , the emails are still blank . I am testing it on server Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469617 Share on other sites More sharing options...
jairathnem Posted February 20, 2014 Share Posted February 20, 2014 try changing $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message; to $body = "Name: $name \n\nEmail:$email \n\nSubject:$subject\n\nMessage:$message"; Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469624 Share on other sites More sharing options...
JaySharma Posted February 20, 2014 Author Share Posted February 20, 2014 No change , still blank Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469629 Share on other sites More sharing options...
jairathnem Posted February 20, 2014 Share Posted February 20, 2014 No change , still blank you mean to say the mail arrives. but the content is blank? Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469632 Share on other sites More sharing options...
JaySharma Posted February 20, 2014 Author Share Posted February 20, 2014 Yes email arrives but still no data Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469633 Share on other sites More sharing options...
jairathnem Posted February 20, 2014 Share Posted February 20, 2014 Remove the '@' before the mail function, it will let us know any errors. also try adding these headers to the mail $headers = "From: mail <$email_from>\r\n"; $headers .= "MIME-Version: 1.0" ."\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469634 Share on other sites More sharing options...
JaySharma Posted February 20, 2014 Author Share Posted February 20, 2014 <?php header('Content-type: application/json'); $status = array( 'type'=>'success', 'message'=>'Email sent!' ); $name = @trim(stripslashes($_POST['name'])); $email = @trim(stripslashes($_POST['email'])); $subject = @trim(stripslashes($_POST['subject'])); $message = @trim(stripslashes($_POST['message'])); $email_from = $email; $email_to = 'free.bks@gmail.com'; $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message; $headers = "From: mail <$email_from>\r\n"; $headers .= "MIME-Version: 1.0" ."\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $success = mail($email_to, $subject, $body,$header); echo json_encode($status); die; Still got a blank mail Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469637 Share on other sites More sharing options...
jairathnem Posted February 20, 2014 Share Posted February 20, 2014 No ide why the message wouldn't display. do this. create a test php file and add the below code <?php $to = 'nobody@example.com'; $subject = 'the subject'; $message = 'hello. This is the message. '; $headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> change the to and from and visit the page. Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469639 Share on other sites More sharing options...
JaySharma Posted February 20, 2014 Author Share Posted February 20, 2014 (edited) OKie I recieved an email now with content for the test page you wanted me to create. BUt why the script I am using is not working . Edited February 20, 2014 by JaySharma Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469641 Share on other sites More sharing options...
JaySharma Posted February 20, 2014 Author Share Posted February 20, 2014 OKie I recieved an email now with content for the test page you wanted me to create. BUt why the script I am using is not working . ANyone who can help with the script i posted Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469644 Share on other sites More sharing options...
jairathnem Posted February 20, 2014 Share Posted February 20, 2014 all the values in the test one are enclosed in single quotes. Probably that's why. Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469645 Share on other sites More sharing options...
jazzman1 Posted February 20, 2014 Share Posted February 20, 2014 OKie I recieved an email now with content for the test page you wanted me to create. BUt why the script I am using is not working . Check, whether all variables are being defined. In your script I cannot see where "subject" field in your html form is. Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469646 Share on other sites More sharing options...
JaySharma Posted February 20, 2014 Author Share Posted February 20, 2014 Check, whether all variables are being defined. In your script I cannot see where "subject" field in your html form I didnt wanted to use subject , so I kept it as is and didnt used in html . Do you think that might be creating the issue ? Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469648 Share on other sites More sharing options...
mac_gyver Posted February 20, 2014 Share Posted February 20, 2014 because of your use of the json output, i suspect you are using ajax to submit your form and either the data value aren't being submitted at all or you are submitting it as get data. what is the entire code for your form page? Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469650 Share on other sites More sharing options...
JaySharma Posted February 20, 2014 Author Share Posted February 20, 2014 because of your use of the json output, i suspect you are using ajax to submit your form and either the data value aren't being submitted at all or you are submitting it as get data. what is the entire code for your form page? Yes I am using ajax to submit . the form code is <div class="title"><span>Get in touch</span></div> <p class="tac">Contact via email </p> <div class="status alert alert-success" style="display: none"></div> <form id="contact-form" method="POST" class="row mt10 style1 contact-form" action="sendemail.php" name="contact-form"> <div class="col-md-6"> <input type="text" name="name" value="Enter your full name" required="required" onfocus="if(this.value=='Enter your full name')this.value=''" onblur="if(this.value=='')this.value='Enter your full name';" placeholder="name"/> </div> <div class="col-md-6"> <input type="text" name="email" placeholder="email" value="Enter your full e-mail" required="required" onfocus="if(this.value=='Enter your full e-mail')this.value=''" onblur="if(this.value=='')this.value='Enter your full e-mail';" /> </div> <div class="col-md-6"> <input type="text" name="subject" value="Enter your full name" required="required" onfocus="if(this.value=='Enter your full name')this.value=''" onblur="if(this.value=='')this.value='Enter your full name';" placeholder="subject"/> </div> <div class="col-md-12"> <textarea rows="7" cols="5" name="message" placeholder="message" id="message" required="required" onfocus="if($(this).val()=='Write your message...'){$(this).val('');}" onblur="if(this.value=='')this.value='Write your message...';" >Write your message...</textarea> <div class="submit_btn"> <div class="inner"> <input class="submit btn" type="submit" value="Submit" name="" /> </div> </div> </div> </form> Thats the code I am using for form . The javascript code is the following //Ajax contact var form = $('.contact-form'); form.submit(function () { $this = $(this); $.post($(this).attr('action'), function(data) { $this.prev().text(data.message).fadeIn().delay(3000).fadeOut(); },'json'); return false; }); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469652 Share on other sites More sharing options...
mac_gyver Posted February 20, 2014 Share Posted February 20, 2014 your code isn't supplying any data in the .post() method. you need to add $(this).serialize() to get all the form fields/values. untested but should work - $.post($(this).attr('action'),$(this).serialize(), function(data) { Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469660 Share on other sites More sharing options...
JaySharma Posted February 20, 2014 Author Share Posted February 20, 2014 Thanks a Ton Mac ! Its working now ! Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1469679 Share on other sites More sharing options...
angsd Posted February 19, 2015 Share Posted February 19, 2015 your code isn't supplying any data in the .post() method. you need to add $(this).serialize() to get all the form fields/values. untested but should work - $.post($(this).attr('action'),$(this).serialize(), function(data) { please post the full code. still not working Quote Link to comment https://forums.phpfreaks.com/topic/286337-email-script-sending-blank-email/#findComment-1506115 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.