Jump to content

Problem sending email with php form


hatefulcrawdad

Recommended Posts

WARNING!!!: I am a complete noob!

 

I had a friend and some online tutorials help me make this form and it isn't sending. The only time it sends is if I put in the same email address as the form is set to send to in Email: input.  I'm hoping its something small you guys can spot real quick. I am also hosting through Yahoo, so I know that might cause a problem maybe. All the php setting are on in my yahoo panel though.

 

Here is the code:

 

<?php

$emailName = $_POST['Name'];

$emailFrom = $_POST['Email'];

$emailSubject = $_POST['Subject'];

$emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message'];

 

 

 

if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {

echo '';

} elseif (empty($emailSubject)) {

    echo 'You must enter a valid subject.';

} elseif (empty($emailMessage)) {

    echo 'You must enter a message to send to our team.';

} else {

   

    $emailTo = 'your@email.com';

$from = '';

   

    if (!empty($emailFrom)) {

        $emailHeaders = 'From: "' . $emailName . '" <' . $emailFrom . '>';

    } else {

        $emailHeaders = 'From: "Your Name" <your@email.com>';

    }

   

    /* Send Email */

    if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) {

        echo 'You message has been sent!';

    } else {

        echo 'There was an internal error whilst sending your email.<br>';

        echo 'Please try again later.';   

    }

}

?>

 

 

<form name="contact" method="post" action="index_test.php">

Your Name: <input type="text" name="Name"><br>

Your Email: <input type="text" name="Email"><br>

Subject: <input type="text" name="Subject"><br>

Message:<br>

<textarea name="Message" cols="30" rows="8"></textarea><br>

<input type="submit" name="submit" value="Contact Us">

</form>

Link to comment
Share on other sites

try this one out

<html>
<body>

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail( "dr.virus.india@gmail.com", "Subject: $subject",$message, "From: $email" );
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html> 

 

and one thing use the code tags

Link to comment
Share on other sites

no one loves me.

:'(

 

Has your code ever worked prior? The mail function relies on an SMTP server, try opening telnet and entering:

telnet mail.yourdomain.com 25 or telnet smtp.yourdomain.com 25 and it should return if you can connect or not. Being Yahoo, I believe they should have documenation on how to get this to work, as usually it's not set up by default.

 

Your code looks fine to me. Also.. since you're using Yahoo, your mailserver is shared, so your messages sent may be sent into the spam/bulk e-mail box. Check it there if you haven't.

Link to comment
Share on other sites

It does send now, no matter which email I send, so thank you so much. I just had to check on some setting for yahoo. Now when the form loads it starts with the "error" message above the form because nothing is in the form yet. How can I stop this?

 

<?php
$emailFromName = $_POST['Name'];
$emailFrom = $_POST['Email'];
$emailSubject = $_POST['Subject'];
$emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message'];





if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'Error';
} elseif (empty($emailSubject)) {
    echo 'You must enter a valid subject.';
} elseif (empty($emailMessage)) {
    echo 'You must enter a message to send to our team.';
} else {
    
    $emailTo = 'cm@chrismdesign.com';
$from = '';
    
    if (!empty($emailFrom)) {
        $emailHeaders = 'FROM: <cm@chrismdesign.com>';

    } else {
        $emailHeaders = 'FROM: <cm@chrismdesign.com>';
    }
    
    /* Send Email */
    if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) {
        echo 'You message has been sent!';
    } else {
        echo 'There was an internal error whilst sending your email.<br>';
        echo 'Please try again later.';    
    }
}
?>


<form name="contact" method="post" action="index_test.php">
Your Name: <input type="text" name="Name"><br>
Your Email: <input type="text" name="Email"><br> 
Subject: <input type="text" name="Subject"><br>
Message:<br> 
<textarea name="Message" cols="30" rows="8"></textarea><br>
<input type="submit" name="submit" value="Contact Us"> 
</form>

Link to comment
Share on other sites

I don't think I was using the GET function was I? I didn't find it if I was. Anyway. The same problem exists now that I have moved the mail() function only now it says the error message and the message sent message by default. How can I have those not show until the form has been submitted?

 

Another problem that just happened when I tried to get the form to pass the persons email and name through the message. Now none of the information gets through, just the titles I have written in.

 

<?php
$emailTo = 'cm@chrismdesign.com';
$emailName = $_POST['Name'];
$emailFrom = $_POST['Email'];
$emailSubject = $_POST['Subject'];
$emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message'];
$emailMessage .= "\n-----NAME-----\n" . $_POST['Name'];
$emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email'];


/* Send Email */
if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) {
echo 'You message has been sent!';
} else {
echo 'There was an internal error whilst sending your email.<br>';
echo 'Please try again later.';    
}


if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'Error';
} elseif (empty($emailSubject)) {
echo 'You must enter a valid subject.';
} elseif (empty($emailMessage)) {
echo 'You must enter a message to send to our team.';
} 
   

if (!empty($emailFrom)) {
$emailHeaders = 'FROM: <cm@chrismdesign.com>';

}
?>


<form name="contact" method="post" action="index_test.php">
Your Name: <input type="text" name="Name"><br>
Your Email: <input type="text" name="Email"><br> 
Subject: <input type="text" name="Subject"><br>
Message:<br> 
<textarea name="Message" cols="30" rows="8"></textarea><br>
<input type="submit" name="submit" value="Contact Us"> 
</form>

Link to comment
Share on other sites

Ignore my previous post. I got all the information to pass into the email now. And I moved the mail() function to the bottom again because for some reason it was sending emails each time I anyone visits the page. Like the initial load sends a blank email.

 

I'm still getting the error message printed above the form on load though, and that isn't what I want to happen. I want nothing to show until the user enters the information and presses submit. Here is a url to my test site. www.chrismdesign.com/test/index_test.php

 

Here is the code I'm currently using on that test page.

<?php
$emailTo = 'cm@chrismdesign.com';
$emailName = $_POST['Name'];
$emailFrom = $_POST['Email'];
$emailSubject = $_POST['Subject'];
$emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message'];
$emailMessage .= "\n-----NAME-----\n" . $_POST['Name'];
$emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email'];



if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'Error';
} elseif (empty($emailSubject)) {
echo 'You must enter a valid subject.';
} elseif (empty($emailMessage)) {
echo 'You must enter a message to send to our team.';
} else {
    

if (!empty($emailFrom)) {
$emailHeaders = 'FROM: <cm@chrismdesign.com>';
}
    
/* Send Email */
if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) {
echo 'You message has been sent!';
} else {
echo 'There was an internal error whilst sending your email.<br>';
echo 'Please try again later.';    
}
}
?>


<form name="contact" method="post" action="index_test.php">
Your Name: <input type="text" name="Name"><br>
Your Email: <input type="text" name="Email"><br> 
Subject: <input type="text" name="Subject"><br>
Message:<br> 
<textarea name="Message" cols="30" rows="8"></textarea><br>
<input type="submit" name="submit" value="Contact Us"> 
</form>

Link to comment
Share on other sites

the mail() has to be after the headers.

 

<?php
$emailTo = 'cm@chrismdesign.com';
$emailName = $_POST['Name'];
$emailFrom = $_POST['Email'];
$emailSubject = $_POST['Subject'];
$emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message'];
$emailMessage .= "\n-----NAME-----\n" . $_POST['Name'];
$emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email'];



if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'Error';
} elseif (empty($emailSubject)) {
echo 'You must enter a valid subject.';
} elseif (empty($emailMessage)) {
echo 'You must enter a message to send to our team.';
} else {
    

/* Send Email */
if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) {
echo 'You message has been sent!';
} else {
echo 'There was an internal error whilst sending your email.<br>';
echo 'Please try again later.';    
}
}
?>


if (!empty($emailFrom)) {
$emailHeaders = 'FROM: <cm@chrismdesign.com>';
}

   

 

Link to comment
Share on other sites

What on earth are you guys doing? Try this code:

<?php
if (isset($_POST['Name'])){
  $emailTo = 'cm@chrismdesign.com';
  $emailName = $_POST['Name'];
  $emailFrom = $_POST['Email'];
  $emailSubject = $_POST['Subject'];
  $emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message'];
  $emailMessage .= "\n-----NAME-----\n" . $_POST['Name'];
  $emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email'];


if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
  echo 'Error';
} elseif (empty($emailSubject)) {
  echo 'You must enter a valid subject.';
} elseif (empty($emailMessage)) {
  echo 'You must enter a message to send to our team.';
} else {
  if (!empty($emailFrom)) {
    $emailHeaders = 'FROM: <cm@chrismdesign.com>';
  }
    
/* Send Email */
if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) {
  echo 'You message has been sent!';
} else {
  echo 'There was an internal error whilst sending your email.<br>';
  echo 'Please try again later.';    
}
}
}
?>


<form name="contact" method="post" action="index_test.php">
Your Name: <input type="text" name="Name"><br>
Your Email: <input type="text" name="Email"><br>
Subject: <input type="text" name="Subject"><br>
Message:<br>
<textarea name="Message" cols="30" rows="8"></textarea><br>
<input type="submit" name="submit" value="Contact Us">
</form>

 

That'll prevent the code from running, if the page is first loaded (I.E. No POST data. ). Why on earth are you complaining about headers to be before mail, when it is, and giving invalid code?

Link to comment
Share on other sites

like i stated. u need to define the email headers variable before the if(mail())

 

okay, doesn't this

if (!empty($emailFrom)) {
$emailHeaders = 'FROM: <cm@chrismdesign.com>';
}

 

define it? and it did come before the mail() function before.

 

Im sorry again, like I said, total php noob.

 

Just use my code (on the previous page now), I'm not sure what he's on about. You set the headers correctly.

Link to comment
Share on other sites

What on earth are you guys doing? Try this code:

 

That'll prevent the code from running, if the page is first loaded (I.E. No POST data. ). Why on earth are you complaining about headers to be before mail, when it is, and giving invalid code?

 

Thanks man, I really appreciate it. I didn't understand what the other guy was trying to tell me. The only thing not working now is, if the user doesn't enter a message it is supposed to tell them "Please enter a message" but it just sends now. Not a huge deal because everthing else works. But if you can see why, that would be awesome.

 

<?php
if (isset($_POST['Name'])){
$emailTo = 'cm@chrismdesign.com';
$emailName = $_POST['Name'];
$emailFrom = $_POST['Email'];
$emailSubject = $_POST['Subject'];
$emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message'];
$emailMessage .= "\n-----NAME-----\n" . $_POST['Name'];
$emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email'];


if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'Please enter a valid email address';
} elseif (empty($emailName)) {
echo 'Please enter your name';
} elseif (empty($emailSubject)) {
echo 'Please enter a subject';
} elseif (empty($emailMessage)) {
echo 'Please enter a message';
} else {
if (!empty($emailFrom)) {
$emailHeaders = 'FROM: <cm@chrismdesign.com>';
}
    
/* Send Email */
if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) {
echo 'You message has been sent!';
} else {
echo 'There was an internal error whilst sending your email.<br>';
echo 'Please try again later.';    
}
}
}
?>

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.