codenoobie Posted August 4, 2008 Share Posted August 4, 2008 *Note: Blade280891 just answered this question on my earlier post. He said that I can't do this without PHPMailer. So, I guess that's my answer. Thank you This is actually part two of an earlier post. I figured I'd do it this way because the question is different from the original. I'm in trouble here. I'm a newbie at PHP I'm going to have to really learn PHPMailer as soon as I can. But, I'm really under the gun timewise (I'm pregnant and due tomorrow - if I don't get this site done by the time this baby comes I'm in real trouble with my boss.) Is there anyway to add headers to this code that will customize the "from" email address to say "TEAM:BARIATRICS" instead of "[email protected]" easily? I can't figure it out without help. I've been trying to all day and I'm so new at this I'm not having luck at all: <?php //--------------------------Set these paramaters-------------------------- // Subject of email sent to you. $subject = 'TEAM:BARIATRICS Informational Packet Request'; // Your email address. This is where the form information will be sent. $emailadd = '[email protected]'; // Where to redirect after form is processed. $url = 'http://www.butneveragain.org/info/thankyou.html'; // 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. $req = '0'; // Email address put into Email Field $email_field = $_POST["Email"]; // --------------------------Do not edit below this line-------------------------- $auto_respond_subject = 'TEAM:BARIATRICS Informational Packet Request'; $auto_respond_body = "Thank you for requesting your free TEAM:BARIATRICS Informational Packet. \n You should recieve your packet within 5-10 business days.\n If you would like additional information, call 574.537.TEAM or visit us online at www.butneveragain.org."; mail($email_field, $auto_respond_subject, $auto_respond_body); $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: Online Request"); echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; ?> Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/ Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 Try this: <?php //--------------------------Set these paramaters-------------------------- // Subject of email sent to you. $subject = 'TEAM:BARIATRICS Informational Packet Request'; // Your email address. This is where the form information will be sent. $emailadd = '[email protected]'; // Where to redirect after form is processed. $url = 'http://www.butneveragain.org/info/thankyou.html'; // 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. $req = '0'; // Email address put into Email Field $email_field = $_POST["Email"]; //Here is where I set the headers, you can add more if ever necessary $headers = "From: Team:Bariatrics <[email protected]>"; // --------------------------Do not edit below this line-------------------------- $auto_respond_subject = 'TEAM:BARIATRICS Informational Packet Request'; $auto_respond_body = "Thank you for requesting your free TEAM:BARIATRICS Informational Packet. \n You should recieve your packet within 5-10 business days.\n If you would like additional information, call 574.537.TEAM or visit us online at www.butneveragain.org."; mail($email_field, $auto_respond_subject, $auto_respond_body, $headers); $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: Online Request"); echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; ?> Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-607988 Share on other sites More sharing options...
Stooney Posted August 4, 2008 Share Posted August 4, 2008 Try this. I don't do much with mail, just going off what I read on php.net. mail($emailadd, $subject, $text, "From: Online Request\r\n"); Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-607991 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 No, that won't do it. Try my code. >_< I set the headers for the mail in the $headers variable. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-607997 Share on other sites More sharing options...
lemmin Posted August 4, 2008 Share Posted August 4, 2008 I wrote this a long time ago, but I remember it working: $fp = fsockopen("mail.server.com", 25, $errno, $errstr, 30); if (!$fp) die("Mail server could not be contacted. Please try again later."); else { $out = "HELO\r\n"; $out .= "MAIL FROM: [email protected]\r\n"; $out .= "RCPT TO: [email protected]\r\n"; $out .= "DATA\r\n"; $out .= "SUBJECT: Subject\r\n"; $out .= "MESSAGE-ID: Extra info\r\n\r\n"; $out .= "body of message\r\n"; $out .= ".\r\n"; $out .= "quit\r\n"; fwrite($fp, $out); fclose($fp); } You should be able to change the MAIL FROM: part to any name you want, but some mail servers have rules for that name. Good luck! Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-607999 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 You don't need sockets for this, the mail() function works just fine. o-O Try my code and post back. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608000 Share on other sites More sharing options...
codenoobie Posted August 4, 2008 Author Share Posted August 4, 2008 DarkWater - Thank you so much. I tried your code and I recieved this error message: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/ghsfiles/public_html/butneveragain/info/mailer.php on line 4 What does it mean? Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608009 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 Eh, sorry about that. Can you post the code that you have? I didn't touch line 4 so maybe it was just a copy+paste issue or something. Please post it and I'll check it out. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608013 Share on other sites More sharing options...
codenoobie Posted August 4, 2008 Author Share Posted August 4, 2008 Sure! Here goes: <?php //--------------------------Set these paramaters-------------------------- // Subject of email sent to you. $subject = 'TEAM:BARIATRICS Informational Packet Request'; // Your email address. This is where the form information will be sent. $emailadd = '[email protected]'; // Where to redirect after form is processed. $url = 'http://www.butneveragain.org/info/thankyou.html'; // 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. $req = '0'; // Email address put into Email Field $email_field = $_POST["Email"]; //Here is where I set the headers, you can add more if ever necessary $headers = "From: Team:Bariatrics <[email protected]>"; // --------------------------Do not edit below this line-------------------------- $auto_respond_subject = 'TEAM:BARIATRICS Informational Packet Request'; $auto_respond_body = "Thank you for requesting your free TEAM:BARIATRICS Informational Packet. \n You should recieve your packet within 5-10 business days.\n If you would like additional information, call 574.537.TEAM or visit us online at www.butneveragain.org."; mail($email_field, $auto_respond_subject, $auto_respond_body, $headers); $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: Online Request"); echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; ?> Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608014 Share on other sites More sharing options...
lemmin Posted August 4, 2008 Share Posted August 4, 2008 I would assume that the mail() function works similarly to what I posted. Doing it this way, though, allows you to customize the raw data that is being sent to the mail server. It seems like headers would work too, so that is probably cleaner if you can get it to work. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608018 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 Hmm, interestingly enough, I just visited the page (the actual page, mailer.php, directly), and it said the error was on line 6. >_> Edit: Just went back and it changed to line 4 this time... O_O Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608020 Share on other sites More sharing options...
codenoobie Posted August 4, 2008 Author Share Posted August 4, 2008 That was my fault. I added space inbetween the lines. I promise I won't touch it again. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608024 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 Lol, okay, I figured as much. And are you sure that it's exactly what you have on the web server? If I copy and paste the code from the code box you posted, I get no syntax errors. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608027 Share on other sites More sharing options...
codenoobie Posted August 4, 2008 Author Share Posted August 4, 2008 I'll try to post it again... Here it is. This is so strange. <?php //--------------------------Set these paramaters-------------------------- // Subject of email sent to you. $subject = 'TEAM:BARIATRICS Informational Packet Request'; // Your email address. This is where the form information will be sent. $emailadd = '[email protected]'; // Where to redirect after form is processed. $url = 'http://www.butneveragain.org/info/thankyou.html'; // 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. $req = '0'; // Email address put into Email Field $email_field = $_POST["Email"]; //Here is where I set the headers, you can add more if ever necessary $headers = "From: Team:Bariatrics <[email protected]>"; // --------------------------Do not edit below this line-------------------------- $auto_respond_subject = 'TEAM:BARIATRICS Informational Packet Request'; $auto_respond_body = "Thank you for requesting your free TEAM:BARIATRICS Informational Packet. \n You should recieve your packet within 5-10 business days.\n If you would like additional information, call 574.537.TEAM or visit us online at www.butneveragain.org."; mail($email_field, $auto_respond_subject, $auto_respond_body, $headers); $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: Online Request"); echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; ?> Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608034 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 Okay now the line 4 makes a bit more sense (the other code had nothing on line 4 >_<). And still it shows no syntax errors for me. What version of PHP do you use? (Not that it really matters at this point) Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608036 Share on other sites More sharing options...
codenoobie Posted August 4, 2008 Author Share Posted August 4, 2008 Ok, dont laugh.... But all that I know is that my server uses PHP5. Does that mean that's the PHP version that I'm using? Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608040 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 Yeah, it does. Okay. Just wanted to make sure that you weren't using PHP 4 (once again, wouldn't really matter for this script). I'm going to PM you for a second, just respond back. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608045 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 Hmm, okay. What I did was first of all add a check to make sure that the email address was sent to the page, otherwise redirect back to the index.html page (otherwise the mail() call will mess up). And then I just uploaded it, and it's not erroring any more. Try filling out the form and get back to me. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608058 Share on other sites More sharing options...
codenoobie Posted August 4, 2008 Author Share Posted August 4, 2008 Ok, I'm not getting the error anymore but I'm still getting the "ghsfiles@ box394.bluehost.com" in the "from" category. I wish there was a magic "PHP" button that fixes these things. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608065 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 Aha, I forgot that line. One second, I'll reupload. (Sorry, I was eating. >_<) Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608068 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 K, uploaded it. Check now. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608071 Share on other sites More sharing options...
codenoobie Posted August 4, 2008 Author Share Posted August 4, 2008 I don't think I've told you, but I love you, man. Thank you, thank you, thank you. Can you please tell me what all you've done so that I can learn? Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608072 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 First of all, I added this (albeit rudimentary) check to the top just to make sure that the mail gets to where it has to go: if (!$_POST['Email']) { header('Location: index.html'); } Secondly, I set up the $headers variable, and used it as the 4th parameter of mail(): $headers = "From: Team:Bariatrics <[email protected]>"; mail($email_field, $auto_respond_subject, $auto_respond_body, $headers); And that's all that I really did. I honestly have NO idea why it was giving you that error, and I've been doing this for quite a while. o-O Glad I could help, and good luck with your child (you'll need it). Also, if you need help with something that you just can't seem to get working, PM me and I'll probably be able to help a bit. Link to comment https://forums.phpfreaks.com/topic/118168-php-form-headers/#findComment-608075 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.