Page 1 of 1

php script to send form fields in an email with multiple attachments

Posted: Thu Jun 03, 2021 8:07 am
by jjq2r01h
Hi
I am using the following php script to collect form data and send it in an email. Can anybody please provide the php code to attach multiple attachments in the email that I am sending. I have googled but haven't found a good answer yet. Any help would be greatly appreciated.

<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
header('Refresh: 0; URL=/index.html');
exit;
}

function Valid_Input($data){
list($data) = preg_split('/\r|\n|%0A|%0D|0x0A|0x0D/i',ltrim($data));
return $data;
}

function Valid_Email($data){
$pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
if (preg_match($pattern,$data)){
return $data;
}
else{
return $GLOBALS['mailto'];
}
}

$message = "";

$name = Valid_Input($_POST['name']);
$emailaddr1 = Valid_Email($_POST['emailaddr1']);
$emailaddr2 = Valid_Email($_POST['emailaddr2']);
$phonenumber = Valid_Input($_POST['phonenumber']);
$targetinmiles = Valid_Input($_POST['targetinmiles']);
$prefinale = Valid_Input($_POST['prefinale']);
$grandfinale = Valid_Input($_POST['grandfinale']);


foreach ($_POST as $key => $value){
if (!is_array($value)){
$message .= "\n".$key." : ".$value;
}
else{
foreach ($_POST[$key] as $itemvalue){
$message .= "\n".$key." : ".$itemvalue;
}
}
}

$emailfrom = "chalisa4harefield@gmail.com";
$mailto = $emailaddr1;
$subject = "Hanuman Chalisa for Harefield 2021";
$organisers = "Hanuman Chalisa 4 Harefield";

$header = "From: ".$organisers." <".$emailfrom.">\n";
$header .= "Reply-To: ".$emailfrom."\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/plain; charset=utf-8\n";
$header .= "Content-Transfer-Encoding: 8bit\n";
$header .= "X-Mailer: PHP v".phpversion();
mail($mailto, $subject, stripslashes($message), $header) or exit('Fatal Mail Error!');
?>

The project can be downloaded from here if anybody wants to have a look at the form.

https://1drv.ms/u/s!Ajp8jGjiAz6Shqt3BxC ... g?e=xDJaQl

Re: php script to send form fields in an email with multiple attachments

Posted: Thu Jun 03, 2021 9:06 am
by Pablo
The built-in form processor supports multiple attachments when mail engine is set to phpmailer.

Re: php script to send form fields in an email with multiple attachments

Posted: Thu Jun 03, 2021 10:58 am
by jjq2r01h
OK thanks, I will give it a try but I have never used the built-in form processor so will have to learn it all. Any idea where I can get all the information from or an exaple?

I also want to write the form data to an excel file. Will this be possible?

Re: php script to send form fields in an email with multiple attachments

Posted: Thu Jun 03, 2021 11:22 am
by Pablo
I cannot help you with custom coding, but this may be helpful:
https://icodemag.com/how-to-send-an-ema ... and-gmail/

Re: php script to send form fields in an email with multiple attachments

Posted: Tue Jun 08, 2021 10:40 pm
by GrahamW
maybe have a look at my dynamic attachments script, you can add it to any WB forms you already have in place. There is also a demo of it as well.

Check it out here
https://shop.grahamscomputers.net/webshop.html

Graham