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 "ghsfiles@box394.bluehost.com" 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 = 'aubreyburkhart@comcast.net'; // 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.'">'; ?> Quote Link to comment 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 = 'aubreyburkhart@comcast.net'; // 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 <ghsfiles@box394.bluehost.com>"; // --------------------------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.'">'; ?> Quote Link to comment 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"); Quote Link to comment 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. Quote Link to comment 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@server.com\r\n"; $out .= "RCPT TO: email2@server.com\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! Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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 = 'aubreyburkhart@comcast.net'; // 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 <ghsfiles@box394.bluehost.com>"; // --------------------------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.'">'; ?> Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 = 'aubreyburkhart@comcast.net'; // 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 <ghsfiles@box394.bluehost.com>"; // --------------------------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.'">'; ?> Quote Link to comment 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) Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. >_<) Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 K, uploaded it. Check now. Quote Link to comment 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? Quote Link to comment 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 <ghsfiles@box394.bluehost.com>"; 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.