A bit of PHP help

CBCB Ƹ̵̡Ӝ̵̨̄ƷDer Millionendorf- Icrontian
edited April 2014 in Internet & Media

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

  • RyderRyder Kalamazoo, Mi Icrontian

    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.

    CB
  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian
    edited April 2014

    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

    CB
  • ardichokeardichoke Icrontian

    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).

    BobbyDigiCBLinc
  • BobbyDigiBobbyDigi ? R U #Hats ! TX Icrontian
    edited April 2014

    @ardichoke said:
    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.

    -Digi

    CB
  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian

    That's all true and I can also recommend mailchimp. Used it for a previous orgs mailing list.

    CB
  • +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.

    BobbyDigiCB
  • CBCB Ƹ̵̡Ӝ̵̨̄Ʒ Der Millionendorf- Icrontian

    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.

  • MiracleManSMiracleManS Chambersburg, PA Icrontian

    @Lincoln said:

    • The regular expression for matching emails looks overly simplistic to me (quick example: .coffee is a TLD now, at lincoln@icrontic.coffee would fail. Also, non-ASCII.)

    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"

  • mertesnmertesn I am Bobby Miller Yukon, OK Icrontian

    @Lincoln said:

    • Don't put the opening bracket on its own line (ok this is just me being pedantic but srsly no)

    Thus were the flames of the bracket wars reignited.

    MiracleManSTushonGargsharkydart
  • ardichokeardichoke Icrontian

    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{ > *

  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian

    Thusly we are reminded of the terrible time in the Helium Wars

    Garg
  • IvanIvan

    gasses up his dirigible.

    Icrontian

    gasses up his dirigible.

  • mertesnmertesn I am Bobby Miller Yukon, OK Icrontian

    vi or GTFO

    GHoosdumErrorNullTurnip
  • ardichokeardichoke Icrontian
    edited April 2014

    @CB said:
    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.

    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.

    sharkydartCathartis
  • LincLinc Owner Detroit Icrontian
    edited May 2014

    (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.)

    mertesn
  • mertesnmertesn I am Bobby Miller Yukon, OK Icrontian

    It is a rare thing when coders agree on standards.

    ...and you're still wrong about the braces.

  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian

    Opening brace on same line.

  • CrazyJoeCrazyJoe Winter Springs, FL Icrontian

    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.

  • mertesnmertesn I am Bobby Miller Yukon, OK Icrontian

    @CrazyJoe said:
    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.

  • GHoosdumGHoosdum Icrontian

    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.

  • CrazyJoeCrazyJoe Winter Springs, FL Icrontian

    @mertesn said:

    Right, I completely agree. It would make it much easier to see where blocks start/stop.

  • georgehgeorgeh Canton, MI Icrontian

    @Lincoln said:
    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.

    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.

    Linc
  • LincLinc Owner Detroit Icrontian
    edited May 2014

    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.

    georgeh
  • LincLinc Owner Detroit Icrontian

    @mertesn said:
    some people do four spaces, some do three, some do tabs.

    We're moving from 3 to 4 to match other projects (no one cared), and everyone agrees tabs are dicks. :D

  • mertesnmertesn I am Bobby Miller Yukon, OK Icrontian

    @Lincoln said:
    We're moving from 3 to 4 to match other projects (no one cared), and everyone agrees tabs are dicks. :D

    Agreed, doesn't matter the number of spaces as long as it's consistent. Tab spacing sucks.

Sign In or Register to comment.