简体   繁体   中英

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. 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):

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):

  <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. Also don't roll your own mail classes etc. Use something robust such as Zend_Mail .

That's about as much help as i can be without knowing more about your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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