Mail() function not working

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1327 Views - Last Post: 04 October 2015 - 05:07 AM Rate Topic: -----

#1 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 14-April 14

Mail() function not working

Posted 29 September 2015 - 08:59 AM

Hi there

I am trying to create an email form that will send an email to the email address that is entered in the email: textbox. I have used the php mail() function, and it is not sending the message to the email entered in the textbox. I have created an account with free web hosting area to test it out, and it still does not work, and I made sure to check if it supports the php mail() function, and it does. I have searched the Internet for this problem but cannot find anything. All the solutions are only showing how to send the email to the email address that is coded in the function. Now I don't want to send an email to a specific email address, I want to send the message to the email address the user enters. Can someone please help me fix this problem and explain to me where I am going wrong?

My code is posted below:

 <?php 
 
echo '<form action="emailform.php" name="emailform" method="POST">';
echo '<fieldset>';
echo '<h4>Email Form: <br /></h4>';
echo '<p>
<label>Email: </label>
//This email address the user enters must receive the message sent 
<input type="text" name="email" size="35" maxlength="35" />
</p>';

echo '<p>
<label>Subject: </label>
<input type="text" name="subject" size="35" maxlength="35" />
</p>';

echo '<p>
<label>Message: </label>
<textarea name="message" rows="10" cols="50" >
</textarea>
</p>';

echo '<p>
<input class="submit" type="submit" value="Submit" />
<input type="reset" value="Clear Form" />
</p>';

$to = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

if (isset($_POST['submit'])) {
	mail($to, $subject, $message);
	echo 'Message sent!';
} else {
	echo 'Message not sent!';
}

echo '</fieldset>';
echo'</form>';

 ?>



Also, when the form loads for the first time, the 'Message not sent' message is displayed. :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Mail() function not working

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4217
  • View blog
  • Posts: 13,328
  • Joined: 08-June 10

Re: Mail() function not working

Posted 29 September 2015 - 09:50 AM

Quote

Also, when the form loads for the first time, the 'Message not sent' message is displayed.

thatís what you have coded, if there is no submit element in POST (and that is true for every GET request like a first load), show that message.

besides that, PHP will emit notices for lines #28 - #30 (which likely are suppressed in your case) on every GET request.

Quote

I have used the php mail() function, and it is not sending the message to the email entered in the textbox.

mail() isnít a reliable mail sender. it just hands the mail over to the serverís sendmail binary without any guarantee for sending. additionally, the FROM header is missing, so itís likely your email is classified as spam.

the best would be to use a mailing library such as SwiftMailer or PHPMailer, which can send email over SMTP.
Was This Post Helpful? 0
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Mail() function not working

Posted 29 September 2015 - 09:53 AM

When you print the values, do they look right?
The var_dump function is great for checking such things.


What bugs me about your code is all those echo statements. They are printing static HTML, so why not just break out of PHP and simply print the HTML?
echo "<p>There is no point</p>";
echo "<p>echoing static HTML from PHP</p>";
echo "<p>in this manner...</p>";


<p>When you can simply put the HTML</p>
<p>outside the PHP tags like this</p>
<p>and then open up PHP tags...</p>
<?php
   doStuff(".. after the HTML is done.");



View PostSlinky, on 29 September 2015 - 03:59 PM, said:

Also, when the form loads for the first time, the 'Message not sent' message is displayed. :helpsmilie:/>/>

That's because your code is telling it to do that...
if (isset($_POST['submit'])) {
    // This IF clause will stop it from sending the email
    // unless the form has been submitted...
    mail($to, $subject, $message);
    echo 'Message sent!';
} else {
    // So if it hasn't been submitted (which is the case
    // the first time the page loads) then this will be
    // printed.
    echo 'Message not sent!';
}



Also, you are grabbing the POST values in the wrong place: before you verify that the form has been submitted. On development servers that will generate Notice messages. - You should always verify that input data exists before you try to use it.

You should avoid checking for "submit" buttons to verify that a form has been submitted. That's historically not a guaranteed method. Instead, check for the expected data.
// Don't do:
if (isset($_POST["submit"])) {
    doStuffWithFormData();
}

// Do:
if (isset($_POST["email"], $_POST["subject"], $_POST["text"])) {
    doStuffWithFormData();
}


Generally you'll want to do input validation/sanitation anyway, so this goes well with that kind of thinking.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,799
  • Joined: 08-August 08

Re: Mail() function not working

Posted 29 September 2015 - 09:54 AM

Do you have a mail server set up on your system? mail simply sends it to the outgoing server, and assumes that it will be received. You might try sending to your [email protected] if you're on a Unix based system. You should also verify that $to is a valid email address before attempting to send.

Quote

Also, when the form loads for the first time, the 'Message not sent' message is displayed.

Of course it does! You've told it to do just that if $_POST['submit'] is not set.
Was This Post Helpful? 0
  • +
  • -

#5 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 14-April 14

Re: Mail() function not working

Posted 29 September 2015 - 12:14 PM

Quote

You might try sending to your [email protected] if you're on a Unix based system.


I can't send the email to the [email protected], the email needs to be sent to the email address the user enters in the textbox.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,799
  • Joined: 08-August 08

Re: Mail() function not working

Posted 29 September 2015 - 01:20 PM

You are the developer, right? You can and should change the code for testing purposes.
Was This Post Helpful? 0
  • +
  • -

#7 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 14-April 14

Re: Mail() function not working

Posted 01 October 2015 - 11:01 AM

No... I am actully not a developer, I am a student.This is my first time doing php programming.

This post has been edited by Slinky: 01 October 2015 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2105
  • View blog
  • Posts: 6,443
  • Joined: 15-January 14

Re: Mail() function not working

Posted 01 October 2015 - 11:06 AM

The point is that you, as the developer on this project, can write test code. You need to test the email system to make sure it works at all before setting up the final code to do what you want it to do. So you can send the email wherever you want when you're testing. Or, type the test email into the textbox if you really, really don't want to set up any test code.
Was This Post Helpful? 1
  • +
  • -

#9 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6698
  • View blog
  • Posts: 31,070
  • Joined: 10-May 07

Re: Mail() function not working

Posted 01 October 2015 - 11:14 AM

View PostSlinky, on 29 September 2015 - 11:59 AM, said:

I have created an account with free web hosting area to test it out, and it still does not work, and I made sure to check if it supports the php mail() function, and it does.


I would contact support of free web hosting area. If you can verify that it's being sent, then it's likely getting cleared as spam. Anytime I see 'free hosting', it's a given that if they allow mail, they are a haven for spammers & likely blacklisted. You can also verify that with mxtools.

The mail function is incredibly simple, it just sends the mail. Verification is non-existant. Therefor I would check with the hosting provider support first, & suspect spam second before trying to troubleshoot any code. There is little point in trying to force something to happen if a.) it can't happen, or b.) it's happening but bad things are happening to the payload. If either of those are the case, they are unfixable by code.

View PostDormilich, on 29 September 2015 - 12:50 PM, said:

Quote

Also, when the form loads for the first time, the 'Message not sent' message is displayed.

thatís what you have coded,

That's what the code base he deployed has coded.

This doesn't seem to be a valid way of learning php, it seems rather hammer a square peg through a circle hole to get a product to work. But that's just my opinion. Someone wanting to learn how a language works would first read the code. But don't let me detour you for learning it in your fashion.
Was This Post Helpful? 0
  • +
  • -

#10 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 14-April 14

Re: Mail() function not working

Posted 02 October 2015 - 11:10 AM

Okay, I have contacted support of the web host, just waiting for a response now.

I have recoded the form and kept all the html elements in the html form instead of inside the php tags. I created a second form with the php code for sending the mail, using the code I have obtained from a textbook I bought today, as shown below:
 <?php
 $to = $_POST['to'];
 $from = $_POST['from'];
 $subject = $_POST['subject'];
 $message = $_POST['message'];
 $headers = 'From: ' . $from . "\r\n";
 ?>
<html>
 <head>
 <title>Mail sent</title>
 </head>
 <body>
 <?php 
 $success = mail($to, $subject, $message, $headers);
 
 if ($success) {
	 echo '<p>Your email has been sent!!</p>';
 } else {
	 echo '<p><strong>There was an error sending your message!</strong></p>';
 }
 ?>
 </body>
</html>



But for some reason, this is still not working. I am now convinced it is the web hosting site that is at fault.
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,799
  • Joined: 08-August 08

Re: Mail() function not working

Posted 02 October 2015 - 11:18 AM

A better test:
<html>
 <head>
 <title>Mail sent</title>
 </head>
 <body>
 <?php 
 $success = mail("[email protected]", "TESTING", "If you get this, it worked!", 'From: [email protected]'."\r\n");
 if ($success) {
	 echo '<p>Your email has been sent!!</p>';
 } else {
	 echo '<p><strong>There was an error sending your message!</strong></p>';
 }
 ?>
 </body>
</html>


This way you don't have to rely on the form data being correctly entered and sent.
Was This Post Helpful? 0
  • +
  • -

#12 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 14-April 14

Re: Mail() function not working

Posted 02 October 2015 - 01:18 PM

Okay, tried it, didn't go through. It said the email was sent but I did not receive the email. I checked the spam folder, there's nothing there either.

I tried the email test the support tech sent me, it said the email went through to the email entered but I did not receive anything, also checked spam folder - nothing either. I have used 2 web hosting services so far and there is nothing.

I think the problem is with these free web hosting services. Maybe I should try a paid hosting service.
Was This Post Helpful? 0
  • +
  • -

#13 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Mail() function not working

Posted 02 October 2015 - 02:48 PM

If the tech support for a hosting service gives you a mail test code, they support it.
The problem must lie elsewhere.

Have you tried changing the to and from addresses, or are you always using the same values?
It's possible that an email server might refuse a message for some reason. Try a different email service, to see if anything changes.

Are you making 100% sure that the subject and message values aren't empty?
If the message is empty it might get queued correctly from PHP, but either the sending or receiving servers might reject it without your code ever knowing it.


Make sure the domain of the "From" address matches the domain of the website sending the email. Some servers can be very sensitive about that kind of stuff.
Was This Post Helpful? 0
  • +
  • -

#14 Slinky   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 14-April 14

Re: Mail() function not working

Posted 03 October 2015 - 04:13 AM

Yes I have tried using different addresses. And no, the subject and message fields are not empty. I have used the email address I've created with the domain of the website and still no luck. I have been checking my inbox and spam folders every hour. There is nothing.

The tech support didn't give code. They just gave a link with a textbox to enter an email in with a sumbit button to test if the website email server is working. So I entered my email address and clicked submit, and it said the email was sent. I done this yesterday and on Thursday and right up till now, I still have not received the email.

I have tried the other test code that @CTphpnwb posted, using the email address with the domain of the website in the 'From: ' section. And it said the email was sent but I have not received the email at all.
Was This Post Helpful? 0
  • +
  • -

#15 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Mail() function not working

Posted 03 October 2015 - 04:45 AM

If CTphpnwb's code didn't get your mail delivered, then there is something wrong with the server. The PHP code is doing it's job correctly, so that's not the issue.

You need to follow up with the host's tech support.

View PostSlinky, on 03 October 2015 - 11:13 AM, said:

The tech support didn't give code. They just gave a link with a textbox to enter an email in with a sumbit button to test if the website email server is working. So I entered my email address and clicked submit, and it said the email was sent. I done this yesterday and on Thursday and right up till now, I still have not received the email.

Have you told them that?


Note that PHP can't actually tell if a mail has been sent, when you are using the mail() function. Even - as with CTphpnwb's code - when the mail() function returns true, and it prints a success message, there is no guarantee that the message was really sent. All PHP knows is that the message has been queued for sending by the SMTP server. Whether the SMTP server sends it or not is not known by PHP.

So, in other words: any PHP script that says "Message sent!" is somewhat misleading. A more accurate message would be: "Email successfully passed on to the local SMTP server!".
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2