简体   繁体   中英

swiftmailer contact form required fields

I have built a contact form on a website which is handled by swiftmailer. At the moment it sends correctly with an image attachment and some input fields. How do i make some of the fields "required" and output an error message on those if left empty? Is this something that needs to happen before the swiftmailer library comes into it?

Apologies if this is simple stuff but im new to PHP and cant find a quick answer to this anywhere

<?php

$_SESSION["post"] = $_POST; 
$name = $_POST["Name"]; $email = $_POST["Email"]; $phone = $_POST["Phone"]; $dob = $_POST['DOBDay'] ."\t" .$_POST['DOBMonth'] ."\t" .$_POST['DOBYear'];$address = $_POST['AddressLine1'] ."\n" .$_POST['AddressLine2'] ."\n" .$_POST['PostCode'];$experience = $_POST["Experience"];$height = $_POST["Height"]; $size = $_POST["DressSize"];$bra = $_POST["Bra"];$waist = $_POST["Waist"];$hipwidest = $_POST["HipWidest"];$bicep = $_POST["Bicep"];$thigh = $_POST["Thigh"];$shoe = $_POST["Shoe"];    

require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('xxx@gmail.com')
->setPassword('xxx');



// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Be A Model application: Girls') 

// Set the From address with an associative array
->setFrom(array($email => $name))

// Set the To addresses with an associative array
->setTo(array('xxx@xxx.com', 'xxx@xxx.com' => 'contact test'))

// Give it a body
->setBody('Name: ' .$name ."\n"
.'Email: ' .$email ."\n"
.'Phone: ' .$phone ."\n"
.'Address: ' .$address ."\n"
.'DOB: ' .$dob ."\n"
.'Experience: ' .$experience ."\n"
.'Height: ' .$height ."\n"
.'Dress Size: ' .$size ."\n"
.'Bra: ' .$bra ."\n"
.'Waist: ' .$waist ."\n"
.'Hip at Widest: ' .$hipwidest ."\n"
.'Bicep: ' .$bicep ."\n"
.'Thigh: ' .$thigh ."\n"
.'Shoe Size: ' .$shoe ."\n" );


// And optionally an alternative body
//->addPart('<q>Here is the message itself</q>', 'text/html');

// Attachment  
$message->attach(
Swift_Attachment::fromPath($_FILES['fileatt']['tmp_name'])->setFilename($_FILES['fileatt']['name'])
);

// Send the message
$result = $mailer->send($message);

if ($result)
{
header('Location: http://www.modelmeasures.co.uk/thankyou.html');
}
echo $result;

?>

There are two types of data validation for websites, Client Side and Server Side.

Client Side Validation - This type of validation is typically done using javascript, often as you complete the form. You've seen this on sites that show a 'X', or turn the field colour red, beside an invalid form field BEFORE you even submit it.

Client side validation is useful because it lets the user know that there is a problem before they even submit the form, giving them a chance to correct it.

Server Side Validation - This is where you check the form values when received by the server to make sure it is in the format you expect, doesn't contain invalid information, etc. You see this validation in use when you complete a form, submit it, and the page reloads and tells you there are errors.

You should be doing this type of validation regardless if you validate on the client side or not. It is easy to disable javascript, and if you are only using client side validation people could enter anything they want. This is a security risk.

What I usually do is set my pages up, and use server side validation. This ensures that there are no security issues, and that I am checking the data the user enters. Once that is working I also add client side javascript validation, to make the form more user-friendly. Doing it this way the javascript validation makes sure that the user is entering the correct information, but if something goes wrong, or javascript is disabled, my server validates the data anyways.

So, to answer your question, you really should be doing server side validation at the very least. It would be important to have the validation occur before Swiftmailer actually sends the email, so that emails are not send if invalid data has been entered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM