PHP尝试添加向工作联系我们表单发送附件的功能

时间:2012-06-20 00:39:14

标签: php email attachment email-attachments

我有一位朋友要我修改她网站的联系表格,以申请电话号码以及添加照片附件的功能。我已经添加了请求和发送电子邮件所需的代码,但是添加附加照片文件的功能超出了我目前的知识范围。有人可以看一下代码并告诉我如何添加将文件附加到邮件的功能吗?我所要做的就是在当前消息字段下面添加它,但事实证明它比我预期的更多。由于我尝试的所有内容都打破了当前的表单,我已经删除了我的代码尝试添加附件,并且我正在发布当前正常工作的代码。我希望有更好掌握php的人可以帮助我们。

    <?php
      //set the level of error reporting
        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);  

      // Set-up these 3 parameters
      $to = 'any@somewhere.net';
      $subject = 'Question from The website';
      $contact_submitted = 'Your message has been sent.';

      function email_is_valid($email) {
        return preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$email);
      }
      if (!email_is_valid($to)) {
        echo '<p style="color: red;">You must set-up a valid (to) email address before this contact page will work.</p>';
      }
      if (isset($_POST['contact_submitted'])) {
        $return = "\n\r";
        $youremail = trim(htmlspecialchars($_POST['your_email']));
        $yourname = stripslashes(strip_tags($_POST['your_name']));
        $yourphone = trim(htmlspecialchars($_POST['your_phone']));
        $yourmessage = stripslashes(strip_tags($_POST['your_message']));
        $contact_name = "Name: ".$yourname;
        $contact_email = "Email Address: ".$youremail;
        $contact_phone = "Phone: ".$yourphone;
        $message_text = "Message: ".$yourmessage;
        $user_answer = trim(htmlspecialchars($_POST['user_answer']));
        $answer = trim(htmlspecialchars($_POST['answer']));
        $message = $contact_name . $return . $contact_email . $return . $contact_phone . $return . $return . $message_text;
        $headers = "From: ".$youremail;
        if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yourphone != "" && $yourmessage != "" && substr(md5($user_answer),5,10) === $answer) {
          mail($to,$subject,$message,$headers);
          $yourname = '';
          $youremail = '';
          $yourphone = '';
          $yourmessage = '';
          echo '<p style="color: blue;">'.$contact_submitted.'</p>';
        }
        else echo '<p style="color: red;">Please enter your name, a valid email address, your message and the answer to the simple maths question before sending your message.</p>';
      }
      $number_1 = rand(1, 9);
      $number_2 = rand(1, 9);
      $answer = substr(md5($number_1+$number_2),5,10);
    ?>
    <form id="contact" action="contact.php" method="post">
      <div class="form_settings">
        <p><span>Name:</span><input class="contact" type="text" name="your_name" value="<?php $yourname; ?>" /></p>
        <p><span>Email Address:</span><input class="contact" type="text" name="your_email" value="<?php $youremail; ?>" /></p>
        <p><span>Phone Number:</span><input class="contact" type="text" name="your_phone" value="<?php $yourphone; ?>" /></p>
        <p><span>Message:</span><textarea class="contact textarea" rows="1" cols="50" name="your_message"><?php $yourmessage; ?></textarea></p>
        <p style="line-height: 1.7em;">To help prevent spam, please enter the answer to this question:</p>
        <p><span><?php echo $number_1; ?> + <?php echo $number_2; ?> = ?</span><input type="text" name="user_answer" /><input type="hidden" name="answer" value="<?php echo $answer; ?>" /></p>
        <p style="padding-top: 15px"><span>&nbsp;</span><input class="submit" type="submit" name="contact_submitted" value="send" /></p>
      </div>
    </form>

如果有人能想到这一点,我将永远感激。

1 个答案:

答案 0 :(得分:1)

您将遇到的第一个问题是,如果没有指明

,您将无法上传任何文件
enctype="multipart/form-data"
表格标签内的

其余信息,您可以参考此tutorial