从表单发送文件附件使用phpMailer和PHP [英] Send File Attachment from Form Using phpMailer and PHP

查看:180
本文介绍了从表单发送文件附件使用phpMailer和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 example.com/contact-us.php 上有一个表格(简体):

 < form method =postaction =process.phpenctype =multipart / form-data> 
< input type =filename =uploaded_fileid =uploaded_file/>
< input type =hiddenname =MAX_FILE_SIZEvalue =10000000/>
< / form>

在我的 process.php 文件中,I下面的代码利用 PHPMailer()发送一封邮件:

 要求( phpmailer.php); 

$ mail = new PHPMailer();

$ mail-> From = me@example.com;
$ mail-> FromName =我的名字;
$ mail-> AddAddress(me@example.com,John Doe);

$ mail-> WordWrap = 50;
$ mail-> IsHTML(true);

$ mail-> Subject =联络表单已提交;
$ mail-> Body =这是消息的正文。;

电子邮件正确发送正文,但没有附件 uploaded_file < code $。

我的问题

我需要档案 uploaded_file 从表单中附加到电子邮件中,并发送。我不在意在 process.php 脚本在电子邮件中发送它之后保存文件。我知道我需要添加 AddAttachment(); 某处(我假设在 Body line)来发送附件。但是...


  1. 我在 process.php 文件拉入文件 uploaded_file ?像使用 $ _ FILES ['uploaded_file'] 从contact-us.php页面拉取文件的东西?

  2. AddAttachment(); 里面附加和发送文件以及这个代码需要去的地方



    1. 请帮忙和提供代码!谢谢!

      解决方案

      if(isset($ _FILES ['uploaded_file'])&&
      $ _FILES ['uploaded_file'] [

        'error'] == UPLOAD_ERR_OK){
      $ mail-> AddAttachment($ _ FILES ['uploaded_file'] ['tmp_name'],
      $ _FILES ['uploaded_file'] ['name']) ;



      $ b $基本例子也可以找到这里



      函数定义 AddAttachment 是:

        public function AddAttachment($ path,
      $ name ='',
      $ encoding ='base64',
      $ type ='application / octet-stream')


      I have a form on example.com/contact-us.php that looks like this (simplified):

      <form method="post" action="process.php" enctype="multipart/form-data">
        <input type="file" name="uploaded_file" id="uploaded_file" />
        <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
      </form>
      

      In my process.php file, I have the following code utilizing PHPMailer() to send an email:

      require("phpmailer.php");
      
      $mail = new PHPMailer();
      
      $mail->From     = me@example.com;
      $mail->FromName = My name;
      $mail->AddAddress(me@example.com,"John Doe");
      
      $mail->WordWrap = 50;
      $mail->IsHTML(true);
      
      $mail->Subject  =  "Contact Form Submitted";
      $mail->Body     =  "This is the body of the message.";
      

      The email sends the body correctly, but without the Attachment of uploaded_file.

      MY QUESTION

      I need the file uploaded_file from the form to be attached to the email, and sent. I do NOT care about saving the file after the process.php script sends it in an email.

      I understand that I need to add AddAttachment(); somewhere (I'm assuming under the Body line) for the attachment to be sent. But...

      1. What do I put at the top of the process.php file to pull in the file uploaded_file? Like something using $_FILES['uploaded_file'] to pull in the file from the contact-us.php page?
      2. What goes inside of AddAttachment(); for the file to be attached and sent along with the email and where does this code need to go?

      Please help and provide code!Thanks!

      解决方案

      Try:

      if (isset($_FILES['uploaded_file']) &&
          $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
          $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
                               $_FILES['uploaded_file']['name']);
      }
      

      Basic example can also be found here.

      The function definition for AddAttachment is:

      public function AddAttachment($path,
                                    $name = '',
                                    $encoding = 'base64',
                                    $type = 'application/octet-stream')
      

      这篇关于从表单发送文件附件使用phpMailer和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆