Forum Moderators: coopster

Message Too Old, No Replies

PHP Mailer

Mailer Problem can't figure it out

         

jayrock911

11:23 pm on Jan 23, 2009 (gmt 0)

10+ Year Member



For the life of me, I can't figure out why this isn't working. I took the code from another site of mine that works. But I must be forgetting something.

$subject = 'WANT SILVER NOW'; // Subject of email sent to you.
$emailadd = 'info@example.com'; // Your email address. This is where the form information will be sent.
$url = 'http://www.example.com/thank_you.html'; // Where to redirect after form is processed.
$req = '0'; // Makes all fields required. If set to'1' no field can not be empty. If set to '0' any or all fields can be empty.

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

</body>
</html>

The code on the contact page that is filled out on submit is:

<form name="form1" id="form1" method="post" action="scripts/simple_scripts1.php">

Are there other files that this script is dependant on that I have not addresses? Are there permission issues with different servers that may be blocking? the new site is on Godaddy.com

Please advise?

Thanks,

Jason

[edited by: dreamcatcher at 11:31 am (utc) on Jan. 24, 2009]
[edit reason] use example.com. Thanks. [/edit]

Shores

4:10 pm on Jan 26, 2009 (gmt 0)

10+ Year Member



The first error i see just glancing at the code is that your outputting the meta refresh within the body tag, while it should be in the head tag.

Apart from that, you're not telling us anything about the error you get... :)

jayrock911

5:30 pm on Jan 26, 2009 (gmt 0)

10+ Year Member





<?php

// Simple Form Script
// Copyright (C) 2005 Eric Zhang
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//

//--------------------------Set these paramaters--------------------------

$subject = 'WANT SILVER NOW'; // Subject of email sent to you.
$emailadd = 'info@example.com'; // Your email address. This is where the form information will be sent.
$url = 'http://www.example.com/thank_you.html'; // Where to redirect after form is processed.
$req = '0'; // Makes all fields required. If set to'1' no field can not be empty. If set to '0' any or all fields can be empty.

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;

---------------

Then here is the active code on the form:

<form name="form1" id="form1" method="post" action="scripts/simple_scripts1.php">

It is working on two of my other sites but this one is hosted at godaddy.com. I'm not sure what the problem is. I've tried to change the email address to a site on my server and it still doesn't work.

(not sure what meta refresh is?, I'm not a php writer I just knew what to do to get another site to work) Please be specific if possible and let me know if it could be server issue. godaddy.com said it wasn't. I'm completely stumped?

Jason

[edited by: eelixduppy at 6:05 pm (utc) on Jan. 26, 2009]
[edit reason] exemplified [/edit]

jayrock911

6:01 pm on Jan 30, 2009 (gmt 0)

10+ Year Member



PLEASE SOMEONE HELP ASAP

coopster

8:22 pm on Jan 30, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it still doesn't work.

What isn't working?

jayrock911

3:04 pm on Jan 31, 2009 (gmt 0)

10+ Year Member



Yes, still not working. Could it be a server permissions problem? The code works fine on one of my servers? I'm not a PHP writer so I don't know if there could be a file this code is drawing from? I don't get any errors, it just doesn't send the form info to email?

coopster

3:41 pm on Jan 31, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it just doesn't send the form info to email?

Although posed as a question, we will assume for now that this is the answer to my question, "what is not working?" ...

Monitor the mail() function for a TRUE/FALSE return value:

if (mail($emailadd, $subject, $text, 'From: '.$emailadd.'')) { 
print "MAIL SENT SUCCESSFULLY";
exit;
} else {
print "MAIL FAILED MISERABLY";
exit;
}

Next, try dumping the output to the browser right before sending the email. Dump out all your variables to see what values each contain and determine if anything looks incorrectly formatted.

If all looks well and there are no errors then let it go through again but this time send the form to an email account that you know will accept spam, etc. Your own test account that you can activate just for this purpose often works well here.

If the mail doesn't show up in that mailbox, then check your mail queue. If the mail is not in the mail queue, then your mail() function is failing and is likely returning a FALSE value as mentioned earlier.

Watch your logs throughout this process too. Logs are often the first to let you know where your issue lies.

jayrock911

3:01 pm on Feb 2, 2009 (gmt 0)

10+ Year Member



thanks coop for trying to help. However, this is above my knowledge. I need complete code for the whole php page as I'm not a writer. I did try to input the code and uploaded it but nothing happened. Didn't know what you meant by dumping output to browser or all variables.

If you want to help, you'll have to put it in a fool proof code style.

coopster

12:21 pm on Feb 3, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Oh, I want to help. Most of us write code as part of our daily jobs. We come here to this forum to voluntarily help one another. Our charter [webmasterworld.com] is pretty straightforward in that regard.

I really think you can overcome your issue here. The sample code above would be wrapped around your current/existing "mail" statement. The line where you actually perform the mail function. So, where your mail() function currently exists in your code today, comment that line out and replace it with the code above (making certain the mail statement is still the exact same as your original). What should happen is your browser should display one of the two messages, SUCCESS or FAILURE. It is your starting point for troubleshooting your issue.

jayrock911

4:14 pm on Feb 3, 2009 (gmt 0)

10+ Year Member



Coop, here is what I tried?



<?php

// Simple Form Script
// Copyright (C) 2005 Eric Zhang
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
/
//--------------------------Set these paramaters--------------------------

$subject = 'MORTGAGES-MI APPLICATION'; // Subject of email sent to you.
$emailadd = 'info@example.com'; // Your email address. This is where the form information will be sent.
$url = 'http://www.example.com/thank_you.html'; // Where to redirect after form is processed.
$req = '0'; // Makes all fields required. If set to'1' no field can not be empty. If set to '0' any or all fields can be empty.

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'')) { print "MAIL SENT SUCCESSFULLY"; exit; } else { print "MAIL FAILED MISERABLY"; exit; }
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

?>
</body>
</html>

The browser window was completely blank.

Please advise?

[edited by: eelixduppy at 4:53 pm (utc) on Feb. 3, 2009]
[edit reason] exemplified [/edit]

coopster

1:19 pm on Feb 4, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You didn't wrap the mail statement appropriately in the logic. Here is what it should have looked like ( I trimmed back the code to just the relevant section for now, so add this to the end of your foreach loop):

} // <-- Here is the end of your foreach loop
// Here is the logic for testing your mail() function success:
if (mail($emailadd, $subject, $text, 'From: '.$emailadd.'')) {
print "MAIL SENT SUCCESSFULLY";
exit;
} else {
print "MAIL FAILED MISERABLY";
exit;
}

echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>
</body>
</html>

Now, what will happen is if the mail function is executing successfully, your browser window will be blank except for the SUCCESS or FAILURE message. At least at this point you will know if the mail() function is working or not.

jayrock911

3:55 am on Feb 6, 2009 (gmt 0)

10+ Year Member



O.K here is the new php page:



<?php

// Simple Form Script
// Copyright (C) 2005 Eric Zhang
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//

//--------------------------Set these paramaters--------------------------

$subject = 'MORTGAGES-MI APPLICATION'; // Subject of email sent to you.
$emailadd = 'info@example.com'; // Your email address. This is where the form information will be sent.
$url = 'http://www.example.com/thank_you.html'; // Where to redirect after form is processed.
$req = '0'; // Makes all fields required. If set to'1' no field can not be empty. If set to '0' any or all fields can be empty.

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
if (mail($emailadd, $subject, $text, 'From: '.$emailadd.'')) { print "MAIL SENT SUCCESSFULLY"; exit; } else { print "MAIL FAILED MISERABLY"; exit; }
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

</body>
</html>

-----------

It said mail failed miserabley. So I guess the mail function isn't working. What should I do contact the server in this case godaddy.com?

[edited by: eelixduppy at 5:28 am (utc) on Feb. 6, 2009]
[edit reason] exemplified [/edit]

henry0

1:28 pm on Feb 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are playing with fire
phpMailer is a tool that works very well when you accept it for what it does
which is helping you in mailing tasks.

BUT it is pretty much totally unsecured, used as is, is like calling for hell's gate to open, you need to first learning how to harden it.
Search for how securing phpmailer and you’ll figure what I mean.
I am not trying to belittle your endeavor, please understand that security has to be your number one concern.
Also you need to make sure that you are using the most recent release.

<edit>Typo</edit>

jayrock911

2:15 pm on Feb 7, 2009 (gmt 0)

10+ Year Member



Thanks for the heads up, Just using it as a mailer with no secure info for now.

Coopster, let me know what to do next?

Jayrock its for [mysilverinfo.com...]

over

rocknbil

2:48 pm on Feb 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



with no secure info for now

Jayrock, it's not about the info in the form, it's about the security of the PHP methods. Henry0 is saying this script can easily be abused to use your server to send spam, hack into your server, and who knows what else.

Take five minutes and look at this, it's by no means complete but succinctly describes the problem:

[w3schools.com...]

jayrock911

4:30 am on Feb 9, 2009 (gmt 0)

10+ Year Member



Thanks, I've opened up a can of worms!. Can anyone recommend an easy and safe way to send email with PHP form? I'll need the complete code. Rocknbil, do you trust the one you suggested and what modification may need to be made?

Any advise is appreciated...

J