【问题标题】:PHP attach file to emailPHP 将文件附加到电子邮件
【发布时间】:2011-10-21 18:49:17
【问题描述】:

我希望能够将文件附加到外发电子邮件。我在一个名为 upload.php 的单独文件中有如何执行此操作的代码,但我想知道是否可以将其全部放入 index.php,因为 index 是表单操作指向的位置。我只是不确定将它放在索引中的哪个位置以便它可以工作......

此代码来自 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;

然后我有以下代码,它显示了发出新消息的表单(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>

我希望这是有道理的,不会太令人困惑!感谢您的帮助!

【问题讨论】:

  • 希望您使用PHPMailerSwiftmailer 来进行实际的电子邮件发送,否则您在尝试构建自己的MIME 消息和自己处理文件附件时会遇到很多麻烦。

标签: php forms email-attachments


【解决方案1】:

我不会将所有这些逻辑放在一页中。尝试将其分离为执行特定操作的不同 php 文件。也不要滚动你自己的邮件类等。使用强大的东西,比如Zend_Mail

在不了解您的项目的情况下,我可以提供尽可能多的帮助。

【讨论】:

    猜你喜欢
    • 2010-12-07
    • 2012-12-14
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2014-02-18
    • 2020-11-21
    • 2012-05-17
    相关资源
    最近更新 更多