A bit of PHP help
I've never worked with PHP before. I usually just write all my pages in old-school HTML. If you couldn't tell from the minimalism. I need a place for people to sign up for a newsletter now though (since Facebook is now completely useless for letting people know what's going on with my projects), and for that I need a bit of PHP, but it's not working quite right.
So, I've got this on the html page:
<form name="contactform" method="post" action="form.php"> <table width="450px"> <tr> <td valign="top"> <label for="email">Email Address</label> </td> <td valign="top"> <input type="text" name="email" maxlength="80" size="30"> </td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="Submit"> </td> </tr> </table> </form>
and this in form.php, which I wrote while teaching myself from examples on the web:
<?php if(isset($_POST['email'])) { $email_to = "form@manawaker.com"; $email_subject = "Newsletter subscribe"; // error code function died($error) { echo "Something was wrong with the form you submitted."; die(); } // validation if(!isset($_POST['email'])) { died('Something was wrong with the form you submitted.'); } // form field $email_from = $_POST['email']; $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'That is not an email address.<br />'; } if(strlen($error_message) > 0) { died($error_message); } // message $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Email: ".clean_string($email_from)."\n"; // email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); // success return ?> Thank you! <?php } ?>
Seems simple and straightforward, and it looks like it works, mostly. If I enter a non-email address and hit submit, it returns the error, if I enter a real address and hit submit, it returns the success message.
But I never receive an e-mail.
So, any idea what I've done wrong? Did I forget to close a bracket somewhere or something?
Thanks for taking a look.
Comments
Is there a mail server configured on your webserver? Sorry for jumping in and not really knowing, but I believe there has to be an SMTP instance that the webserver has access to.
The <?php } ?> at the end is probably not supposed to be there. No idea if that'll fix it by itself, as I've only messed with PHP once myself. I don't think there is supposed to be a @ in front of mail either, based on php.net/manual/en/function.mail.php
Yeah, typically you will need to have a mail server running and properly configured on your webserver for PHP mail functions to work.
That said, have you considered just using a third party mailing list service instead? There are legal considerations you have to keep in mind when running a mailing list, due to the CAN SPAM act. Also, home-brewed PHP mailers are juicy targets for abuse of all sorts. It's typically better to let someone else who does it for a living deal with those sorts of considerations. I've heard good things about MailChimp AND it's free for up to 2,000 subscribers (at a rate of up to 12,000 emails per month).
^All of this. +1 for Mailchimp. Customizable sign up pages or code you can drop into a site.
That's all true and I can also recommend mailchimp. Used it for a previous orgs mailing list.
+1 MailChimp
If you want to learn PHP / mail servers we can keep going, but if you want to author words, not code, I'd save yourself headache and check it out.
Thanks guys. I'll check out mailchimp, and be back for more PHP help if that's not a better solution. I didn't realize there was the possibility of a free solution for newsletterness; never even crossed my mind.
I strongly agree with using a third-party service.
That said, I can't help but look at PHP code. Your overall issue is indeed likely a lack of sendmail (or other mailer) on the server. The ampersand before the 'mail' function call suppresses errors, which would make it difficult to know what's going on.
Oh god, trying to be RFC compliant with a regex is going to destroy anyone. You're better off with "try to send an email to that address, if it fails, ignore it"
A good alternative: http://blog.kicksend.com/how-we-decreased-sign-up-confirmation-email-bounces-by-50/
Thus were the flames of the bracket wars reignited.
It's fine to have a different opinion about this.
As long as everyone understands a different one is wrong.
And as yours is the different opinion, I fully agree with your statement
I never used to put opening brackets on a new line... until I found certain syntax rules in vim that broke when I didn't.
\n{ > *
Thusly we are reminded of the terrible time in the Helium Wars
gasses up his dirigible.
gasses up his dirigible.
Thanks guys.
Not only does Monkeymail look like a much better solution than me managing my own newsletter, but also, @GHoosdum has convinced me to finally give up my 'handcode everything in early-90s HTML' ways, and is helping me move the site to WordPress.
vi or GTFO
If you're going to use WordPress, make sure that Mod Security is installed along with a decent ruleset on your server (if possible). WordPress has become a prime target for script kiddies thanks to it's wide install base and the fact that many people fail to keep their WP installs up to date.
(Posting this here too because relevant; just said this on Facebook)
Today at Vanilla we had our first developers meeting many months; I don't even remember our last one. It was our first since at least 2 developers have joined. The co-founder called the other 5 us into the conference room and the general response was "meeting... wait... what?"
Anyway, we're talking about establishing new coding standards, the thing no 2 developers can agree on in any way: spacing, capitalization, naming - the works. But get this: we all agreed on every single stupid miniscule point.... except whether the opening brace after a class/method/function definition belongs on a new line or not. On this single, fine point there is a 3 vs 3 holy war in the office.
I love it. (But seriously: new line before opening brace is a crime against code.)
It is a rare thing when coders agree on standards.
...and you're still wrong about the braces.
Opening brace on same line.
I haven't coded anything in quite some time, so my opinion really doesn't count for much, but wouldn't putting a new line before an opening brace make it easier to distinguish between each class/method/function? Seems like I'm in the new line camp, for whatever it's worth.
For me it's a visual cue for blocks of code beginning and ending - seems much easier to find a missing open/close brace. I'm also pretty adamant about indenting within those blocks...which is a whole other holy war - some people do four spaces, some do three, some do tabs.
When I code, I put the opening brace inline with the function or whatever is opening, and an inline comment after the closing brace denoting which the function it applies to.
Right, I completely agree. It would make it much easier to see where blocks start/stop.
Everyone agreed on almost everything except for one point? Madness.
In my experience it's better to accept that everyone is going to be unhappy with part of a coding standard, so just pick a popular one and don't talk it to death. I don't think that applies to your situation, so just keep fighting the good fight to keep them on the same line.
The basic argument goes like this:
Team "Yes Newline" says "Because standards!"
Team "Fuck No Newline" says "It looks like shit!"
I think I won the day by demonstrating that Linux kernel & the largest PHP projects (WordPress, Drupal, Laravel, Facebook, Wikimedia) all use same-line opening brace, PSR-2 be damned.
We're moving from 3 to 4 to match other projects (no one cared), and everyone agrees tabs are dicks.
Agreed, doesn't matter the number of spaces as long as it's consistent. Tab spacing sucks.