简体   繁体   English

PHP将文件附加到电子邮件

[英]PHP attach file to email

I want to be able to attach a file to an outgoing email. 我希望能够将文件附加到外发电子邮件中。 I have the code for how to do this in a separate file called upload.php, but I want to know if I can put it all in index.php, since index is where the form action is pointing to. 我在单独的名为upload.php的文件中有执行此操作的代码,但我想知道是否可以将其全部放入index.php中,因为index是表单操作指向的位置。 I'm just not sure where to put it in index so that it will work... 我只是不确定将其放在索引中的哪个位置以便它可以工作...

This code is from the section of index.php (where $action = send-message): 这段代码来自index.php部分(其中$ action = send-message):

case 'send-message':
    if(send_message($to, $cc, $subject, $message)) {
      echo "<p style=\"padding-bottom: 100px\">Message sent!</p>";
    } else {
      echo "<p style=\"padding-bottom: 100px\">Could not send message.</p>";
    }
break;

Then I have the following code, which displays the form to make a new message (output.php): 然后,我得到以下代码,该代码显示生成新消息的表单(output.php):

  <table cellpadding="4" cellspacing="0" border="0" width="<?php echo $table_width; ?>">
  <form action="index.php?action=send-message" method="post">
  <tr>
    <td bgcolor="#cccccc">To Address:</td>
    <td bgcolor="#cccccc">
      <input type="text" name="to" value="<?php echo $to; ?>" size="60" />
    </td>
  </tr>
  <tr>
    <td bgcolor="#cccccc">CC Address:</td>
    <td bgcolor="#cccccc">
      <input type="text" name="cc" value="<?php echo $cc; ?>" size="60" />
    </td>
  </tr>
  <tr>
    <td bgcolor="#cccccc">Subject:</td>
    <td bgcolor="#cccccc">
      <input type="text" name="subject" value="<?php echo $subject; ?>" size="60" />
  </tr>
  <tr>
    <td bgcolor="#cccccc">Upload a file:</td>
    <td bgcolor="#cccccc">
      <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
      <input type="file" name="userfile" id="userfile"/>
      <input type="submit" value="Attach File">
    </td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="#cccccc">
      <textarea name="message" rows="10" cols="72"><?php echo $message; ?></textarea>
    </td>
  </tr>
  <tr>
    <td colspan="2" align="center" bgcolor="#cccccc">
      <?php display_form_button('send-message'); ?>
    </td>
  </tr>
  </form>
  </table>

I hope this makes sense and isn't too confusing! 我希望这是有道理的,不要太困惑了! Thanks for the help! 谢谢您的帮助!

I wouldn't put all this logic into one page. 我不会将所有这些逻辑放到一页上。 Try and separate it out into different php files that do specific things. 尝试将其分成执行特定功能的不同php文件。 Also don't roll your own mail classes etc. Use something robust such as Zend_Mail . 也不要滚动自己的邮件类,等等。使用诸如Zend_Mail之类的健壮工具。

That's about as much help as i can be without knowing more about your project. 在不进一步了解您的项目的情况下,这将为我提供尽可能多的帮助。

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

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