简体   繁体   English

确认并发送带有附件的HTML电子邮件

[英]Valdidate and send HTML email with attachment

I have a half-working php contact form with html. 我有一个半工作的html与php联络表。 Half working is beacuse the validation isn't working, the mail will be send even if one of the fields is in an incorrect format. 由于验证不起作用,因此无法正常工作,即使其中一个字段的格式不正确,也会发送邮件。 Second thing is that I want to add to that contact form the attachment the user uploaded (only one file). 第二件事是我想将用户上传的附件添加到该联系人表单中(仅一个文件)。

<?php
header('Content-type: text/html; charset=utf-8');
$EmailFrom = "Sap_Prio_Hash@lander.comsign";
$EmailTo = "chenf@comda.co.il";
$Subject = "Lead from Sap/Prio/Hash lander";
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Tel = Trim(stripslashes($_POST['Tel']));
$Message = Trim(stripslashes($_POST['Message']));

function died($error) {
        // your error code can go here
        echo "Please check the field error:<br />";
        echo $error."<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['Tel']) ||
        !isset($_POST['Email']) ) {
        died('Error found in fields');       
    }

    $Tel = $_POST['Tel']; // required
    $Email = $_POST['Email']; // 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 .= 'Email is incorrect<br />';
  }

    $string_exp = "/^[0-9 .'-]+$/";

  if(!preg_match($string_exp,$Tel)) {
    $error_message .= 'Phone number is incorrect<br />';
  }

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

  if(strlen($error_message) > 0) {
    died($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);
    }

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

The form in the html is a simple one: html中的表单很简单:

<div id="formdiv">
    <form id='contactus' name="contactform" method="post" action="contacten.php">
        <input type="text" name="Pickpro" id="Pickpro" class="textbox" />
        <input type="text" name="Name" id="Name" class="textbox" />
        <input type="text" name="Email" id="Text1" class="textbox" />
        <input type="text" name="Tel" id="Tel" class="textbox" />
        <input type="text" name="Message" id="Message" class="textbox" />
        <input type="file" class="file" id="attachment" />
        <input type="submit" id="send" title="שלח" value="" />
    </form>
</div>

Exactly how do you expect your validation to work when you simply force it to be "ok" every time? 只是简单地每次强制执行“确定”,您究竟希望如何进行验证?

$validationOK=true;   <--- your validation never fails because of this
if (!$validationOK) {

And if you want to send attachments in that email, then don't use mail() . 如果要在该电子邮件中发送附件,请不要使用mail() It's pathetically useless "use only in case of emergency" garbage. 可悲的是,“仅在紧急情况下使用”垃圾是无用的。 Use a proper mailer package, like PHPMailer or Swiftmailer. 使用适当的邮件程序包,例如PHPMailer或Swiftmailer。 Both of those make sending mime emails+attachments trivial. 两者都使发送哑剧电子邮件和附件变得微不足道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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