【问题标题】:swiftmailer contact form required fieldsswiftmailer 联系表必填字段
【发布时间】:2013-04-09 10:29:54
【问题描述】:

我在一个由 swiftmailer 处理的网站上建立了一个联系表格。目前,它使用图像附件和一些输入字段正确发送。我如何使某些字段为“必需”并在留空的情况下输出错误消息?这是需要在 swiftmailer 库进入之前发生的事情吗?

抱歉,这很简单,但我是 PHP 新手,无法在任何地方快速找到答案

<?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;

?>

【问题讨论】:

    标签: php forms validation swiftmailer


    【解决方案1】:

    网站的数据验证有两种类型,客户端和服务器端。

    客户端验证 - 这种类型的验证通常使用 javascript 完成,通常在您填写表单时完成。您在提交之前就在无效表单字段旁边显示“X”或将字段颜色变为红色的网站上看到了这种情况。

    客户端验证很有用,因为它让用户在提交表单之前就知道存在问题,让他们有机会更正。

    服务器端验证 - 在这里您检查服务器接收到的表单值,以确保它是您期望的格式,不包含无效信息等。当您完成一个验证时,您会看到此验证正在使用表单,提交它,然后页面重新加载并告诉您有错误。

    无论您是否在客户端进行验证,您都应该进行此类验证。禁用 javascript 很容易,如果您只使用客户端验证,人们可以输入他们想要的任何内容。这是一个安全风险。

    我通常做的是设置我的页面,并使用服务器端验证。这确保不存在安全问题,并且我正在检查用户输入的数据。一旦工作正常,我还会添加客户端 javascript 验证,以使表单更加用户友好。这样做,javascript 验证可确保用户输入正确的信息,但如果出现问题或 javascript 被禁用,我的服务器仍然会验证数据。

    所以,要回答您的问题,您确实应该至少进行服务器端验证。在 Swiftmailer 实际发送电子邮件之前进行验证非常重要,这样如果输入了无效数据,就不会发送电子邮件。

    【讨论】:

      猜你喜欢
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 2016-04-17
      相关资源
      最近更新 更多