[Help] Why isn't my sendmail working?

the mail() function

Moderators: egami, macek, gesf

Post Reply
g0dtier
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Feb 27, 2020 4:36 am

Thu Feb 27, 2020 4:37 am


Hi guys I'm seriously stressing hard about this. I seem to have written everything properly but my contact form just won't send an e-mail and update the status on my webpage. I fill out the details on my contact form and my status text stays on "Email is sending...". I'd REALLY REALLY appreciate any help you could toss my way. Thanks a ton!

sendmail.php

Code: Select all

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to   		= 'myemail@gmail.com';//replace with your email

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($to, $subject, $message, $headers);

die();
?>
main.js

Code: Select all

//Contact Form
	var form = $('#contact-form');
	form.submit(function(event){
		event.preventDefault();
		var form_status = $('.form-status');
		$.ajax({
			url: $(this).attr('action'),
			beforeSend: function(){
				form_status.find('.form-status-content').html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn();
			}
		}).done(function(data){
			form_status.find('.form-status-content').html('<p class="text-success">Thank you for contact us. As early as possible  we will contact you</p>').delay(3000).fadeOut();
		});
	});
contact.html

Code: Select all

<form class="form-horizontal" id="contact-form" role="form">
                                <div class="form-group form-status">
                                    <div class="col-sm-offset-2 col-sm-6">
                                        <div class="form-status-content">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-6">
                                        <input type="text" name="name" class="form-control input-lg" placeholder="Name" required="required">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-6">
                                        <input type="email" name="email" class="form-control input-lg" placeholder="Email" required="required">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-6">
                                        <input type="text" name="subject" class="form-control input-lg" placeholder="Subject" required="required">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-8">
                                        <textarea name="message" rows="6" class="form-control input-lg" placeholder="Message" required="required">	         </textarea>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-8">
                                        <button type="submit" class="btn btn-lg btn-transparent">Submit</button>
                                    </div>
                                </div>
                            </form>

g0dtier
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Feb 27, 2020 4:36 am

Thu Feb 27, 2020 5:59 am

Okay so now that status text "E-mail sent we'll contact you soon" but there is no e-mail sent. I logged (data) in main.js and no errors appear.

Edit2: My website is http://spanishwithjosh.com and navigate to the Contact page.

I'm flabbergasted I have no idea why it won't send.
User avatar
augustowloch
New php-forum User
New php-forum User
Posts: 9
Joined: Sun Feb 23, 2020 7:26 am
Location: Buenos Aires, Argentina
Contact:

Thu Feb 27, 2020 5:59 am

try instead of die() in the php, to return a HTML 200 OK
g0dtier
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Feb 27, 2020 4:36 am

Thu Feb 27, 2020 6:08 am

changed to return; and still nothing

Code: Select all

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to   		= 'j0sh.all3n@gmail.com';//replace with your email

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($to, $subject, $message, $headers);

return;
?>
Any way to put an error logging code in there and how to do that? Sorry im completely new to all this. Thanks for your help though I really, really appreciate it. Seriously do.
User avatar
augustowloch
New php-forum User
New php-forum User
Posts: 9
Joined: Sun Feb 23, 2020 7:26 am
Location: Buenos Aires, Argentina
Contact:

Thu Feb 27, 2020 6:38 am

try:

Code: Select all

http_response_code(200); 
die();
check please in browser inspector what is the response of the php in the ajax call!
g0dtier
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Feb 27, 2020 4:36 am

Thu Feb 27, 2020 7:02 am

Nothing. Lol.

I'm going to get rid of mail() and using something else. I'll post back if I can get a friggin e-mail sent with my contact form...
crazymhegde
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Apr 10, 2020 11:03 pm

Fri Apr 10, 2020 11:08 pm

it is not working
Post Reply