Form Code

Hi Guys i want to know how to code this form.
https://www.mywebchambers.co.uk/tell

the form action is refer.php. I want to send the email to the addresses inputted in the boxs with a prefixed message and then a thank you page.

Any advice appreciated.

Thanks
Michael

How to code it how? As in, writing the html?

Just download and use PHPMailer following the documentation and redirect with the header() function afterwards, lookup it at php.net.

the php code for refer.php

What have you tried or thought about?

PHPMailer is one option to handle sending it, as are Swift mailer, Mailgun, and Sendgrid.

Then it is just a matter of either concatenating the emails or looping thru and sending them through the chosen mailer method.

i’ve got this far but in the email the recipient recieves it also reveals the other e mail addresse its been sent to ? how do i hide this?

<?php


// This following statement must not be changed.
// Copyright (c) Michael Bloch and Taming The Beast.  
// Tell-A-Friend script V 2.1 Updated September 19 2006
// Taming the Beast.net - http://www.tamingthebeast.net
// Free Web Marketing and Ecommerce Resources and Tools
// By using this script you agree to indemnify Taming the Beast
// from from any liability that might arise from its use. 
// The preceding statement must not be changed. 

if(count($_POST)) {
# This part strips out nasty code that a malicious
# person may try to inject into the form

foreach(array('email1','email2','email3','youremail','yourname') as $key) $_POST[$key] = strip_tags($_POST[$key]);
if(!is_secure($_POST)) { die("Hackers begone");}

# This part submits a notification to you when 
# the form is submitted

// Email address for copies to be sent to - change to suit
$emailto = "my email address"; 

// Notification email subject text for copies
$esubject = "Recommendation form submission"; 

// Email body text for notifications
$emailtext = "
$_POST[yourname] has used your recommendation form using an email address of $_POST[youremail]

The people the recommendation has been submitted to are:

$_POST[email1]
$_POST[email2]
$_POST[email3]

The page recommended:

$_POST[refurl]

";

# This function sends the email to you

@mail("$emailto", $esubject, $emailtext, "From: $_POST[youremail]");

# This part is the function for sending to recipients

// Page to display after successful submission
// Change the thankyou.htm to suit

$thankyoupage = "thankyou.html"; 

// Subject line for the recommendation - change to suit

$tsubject = "A web page recommendation from $_POST[yourname]";

// Change the text below for the email 
// Be careful not to change anyt "$_POST[value]" bits

$ttext = "
Hi,

$_POST[yourname], whose email address is $_POST[youremail] thought you may be interested in this web page. 

$_POST[refurl]

$_POST[yourname] has used our Tell-a-Friend form to send you this note.

We look forward to your visit!

";

# This sends the note to the addresses submitted
@mail("$_POST[email1],$_POST[email2],$_POST[email3]", $tsubject, $ttext, "FROM: $_POST[youremail]");

# After submission, the thank you page
header("Location: $thankyoupage");
exit;

}

# Nothing further can be changed. Leave the below as is

function is_secure($ar) {
$reg = "/(Content-Type|Bcc|MIME-Version|Content-Transfer-Encoding)/i";
if(!is_array($ar)) { return preg_match($reg,$ar);}
$incoming = array_values_recursive($ar);
foreach($incoming as $k=>$v) if(preg_match($reg,$v)) return false;
return true;
}

function array_values_recursive($array) {
$arrayValues = array();
foreach ($array as $key=>$value) {
if (is_scalar($value) || is_resource($value)) {
$arrayValues[] = $value;
$arrayValues[] = $key;
}
elseif (is_array($value)) {
$arrayValues[] = $key;
$arrayValues = array_merge($arrayValues, array_values_recursive($value));
}
}
return $arrayValues;
}

?>

First, you don’t want to suppress errors, which is what the @ does.That will also tell you if it was sent, which you might want to know about.

Like I said previously, I am not a fan of the mail function and use MailGun, but to answer your question, you need to add headers to the argument list - OR - loop through the list and send them individually.

$headers[] = 'Bcc: [email protected]';

However, you still have tidying up to do. Your form code shouldn’t list the email inputs individually. They should be something like this,

<input type="email" name="email[]">

That will give you an array of emails. Then you just need to either compress them like this:

$emails = implode('; ', $_POST['email']);

// using them to send blindly would look like this
if(mail(null, $tsubject, $ttext, "FROM: $_POST[youremail]", $headers)) {
    // sent successfully
} else {
   // sending failed to send
}

Or send them in a loop

foreach($_POST['email'] as $email) {
    if(mail($email, $tsubject, $ttext, "FROM: $_POST[youremail]")) {
        // sent successfully
    } else {
       // sending failed to send
    }
}
1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service