Jump to content

Recommended Posts

I have been trying to make one of those pre-written online web site letters (using a form) that the site visitor just provides the name and email address on and sends the letter (form).

It's on http://spga.ca/pet-friendly-tenancies-bc.html half way down the web page

It's been a long long time since I have made one and this one is in php which I never seem to have any luck with.

In brief:

 

  • the server has been updated to php version 8.2 that's maybe part of the problem?
  • I wanted to include multiple email addresses (the politicians whom the letter is addressed to) and the php script can't seem to do that without a parsing error
  • The processing script is on contact-form-process.php 
<?php

if (isset($_POST['Email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "email1@website.com", "email2@whatever.com";  
    $email_subject = "Pet Friendly Tenancies for BC";

    function problem($error)
    {
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br><br>";
        echo $error . "<br><br>";
        echo "Please go back and fix these errors.<br><br>";
        die();
    }

    // validation expected data exists
    if (
        !isset($_POST['Name']) ||
        !isset($_POST['Email']) ||
        !isset($_POST['Message'])
    ) {
        problem('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $name = $_POST['Name']; // required
    $email = $_POST['Email']; // required
    $message = $_POST['Message']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if (!preg_match($email_exp, $email)) {
        $error_message .= 'The Email address you entered does not appear to be valid.<br>';
    }

    $string_exp = "/^[A-Za-z .'-]+$/";

    if (!preg_match($string_exp, $name)) {
        $error_message .= 'The Name you entered does not appear to be valid.<br>';
    }

    if (strlen($message) < 2) {
        $error_message .= 'The Message you entered do not appear to be valid.<br>';
    }

    if (strlen($error_message) > 0) {
        problem($error_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 .= "Name: " . clean_string($name) . "\n";
    $email_message .= "Email: " . clean_string($email) . "\n";
    $email_message .= "Message: " . clean_string($message) . "\n";

    // create email headers
    $headers = 'From: ' . $email . "\r\n" .
        'Reply-To: ' . $email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
?>

    <!-- include your success message below -->

    Thank you!

<?php
}
?>

If there is an online script or service that already has a current php script I would be interested. 

Edited by requinix
please use the Code <> button when posting code

The main problem is that this

$email_to = "email1@website.com", "email2@whatever.com";

isn't going to work. You can't just list multiple email address strings like that.

But before that, the other problem is that you're manually trying to send emails. That's almost always bad: emails are hard, and doing it yourself is pretty much always going to go badly.

Switch to a library like PHPMailer or SwiftMailer, which will not only be able to do emails properly but also make it easier to do things like add multiple recipients.

  • Great Answer 2
  • 3 weeks later...

The world of php when simple scripts like this were copy and paste jobs is long gone now.  The libraries requinix mentioned are certainly the way to go, but also typically require the use of the php dependency management tool composer, and a basic understanding of object oriented programming, in that these libraries are oop.  

There is also the question of email deliverability.  These days, you really need to understand a good deal about how email works, and the various ways email is authenticated, or you can end up with a script that will send emails only to have them rejected or black-holed, or spam filtered, defeating the entire purpose of your endeavor.

I'm sorry to say that this is just beyond what a neophyte can handle competently.  

I can't really add on to requinix and gizmola's excellent answers. Do note that you don't have to use phpmailer - its documentation also has examples from SwiftMailer and the Mail component from Laminas. Since I don't have a server with me right now, I found some examples from phpmailer's github repository.

Even if you're not entirely sure what to do, I recommend going through the examples (at least) to see what needs to be done.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.