Jump to content

Email script sending blank email


JaySharma

Recommended Posts

<?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 .

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 11 months later...

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.