11 Replies - 723 Views - Last Post: 27 February 2020 - 08:58 AM Rate Topic: -----

#1 g0dtier   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-February 20

Why isn't my sendmail working?

Posted 27 February 2020 - 05:47 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

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to   		= '[email protected]';//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

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

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





Is This A Good Question/Topic? 0
  • +

Replies To: Why isn't my sendmail working?

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6843
  • View blog
  • Posts: 31,538
  • Joined: 10-May 07

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 06:13 AM

In the file "sendmail.php", I would add logging statements throughout the file. This will tell you how far into that process that it is being reached.
Was This Post Helpful? 0
  • +
  • -

#3 g0dtier   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-February 20

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 06:33 AM

Sorry, I'm fairly new to all this. Would you mind helping me with where and how I'd log the errors? Thanks and sorry for being a noob :(
Was This Post Helpful? 0
  • +
  • -

#4 g0dtier   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-February 20

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 07:01 AM

Okay so now after clicking Submit to send e-mail on my website it says: "Thank you for contact us. As early as possible we will contact" as if the e-mail was sent. I added console.log(data); on the ajax query in main.js but there are no errors.

I'm so very confused why it won't send the e-mail. I checked everyone folder in my e-mail but nothing.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4252
  • View blog
  • Posts: 13,483
  • Joined: 08-June 10

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 07:18 AM

View Postg0dtier, on 27 February 2020 - 04:01 PM, said:

I'm so very confused why it won't send the e-mail.

Sending emails via mail() is not known for being reliable, as it effectively only delegates the mailing task to the underlying OS.

The recommended way of sending emails in PHP is by using a mailing library (such as SwiftMailer) that can use SMTP and knows how to properly create and send emails.
Was This Post Helpful? 4
  • +
  • -

#6 Ornstein   User is online

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 107
  • Joined: 13-May 15

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 07:29 AM

Reliably sending mail using PHP (especially PHP's mail function) can be quite tricky. Most email providers have strict requirements for the domains/servers they'll accept mail from - which can mean messing around with DKIM, SPF and a few others, before a server will accept email from yours.

(If you're using a shared IP/domain/server/etc, you might already be blacklisted.)

If you're not too bothered, a quick alternative might be to configure your server for internal/local mail and then essentially have your email sent to a mailbox on the same server - which you might then choose to access remotely using IMAP or whatever.

You could also either use a PHP library or alter your PHP mail config to connect to (and possibly authenticate with) a remote server (e.g. a Google SMTP server) to send the emails.
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3098
  • View blog
  • Posts: 11,844
  • Joined: 03-December 12

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 07:46 AM

I prefer mail services, SendGrid or MailGun.

Other than that, I second what was said. The mail() function is wholly unreliable, if it sends at all, it typically ends up in the spam folder.
Was This Post Helpful? 0
  • +
  • -

#8 g0dtier   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-February 20

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 08:02 AM

Thanks for the responses. I'll try using something besides mail() and post an update if I can get my form to send a friggin e-mail.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4252
  • View blog
  • Posts: 13,483
  • Joined: 08-June 10

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 08:30 AM

View Postg0dtier, on 27 February 2020 - 05:02 PM, said:

[...] if I can get my form to send a friggin e-mail.

Sending emails is a topic that looks simple at first, but is among the most complex topics once you start looking into it.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6843
  • View blog
  • Posts: 31,538
  • Joined: 10-May 07

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 08:44 AM

That's why I suggested the logging statement. The mail() function only returns true or false based on if the mail is sent. It offers no guarantee what becomes of the data once it leaves.
Was This Post Helpful? 0
  • +
  • -

#11 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2516
  • View blog
  • Posts: 7,623
  • Joined: 15-January 14

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 08:53 AM

Quote

The mail() function only returns true or false based on if the mail is sent.

Not even then, it returns true if the local mail transport agent accepted the mail for delivery. You can't even be sure it was actually sent.
Was This Post Helpful? 1
  • +
  • -

#12 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6843
  • View blog
  • Posts: 31,538
  • Joined: 10-May 07

Re: Why isn't my sendmail working?

Posted 27 February 2020 - 08:58 AM

Right you are. It's been far too long since I have coded PHP to work with mail. Thank you for that clarification.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1