【问题标题】:Email attachments that are uploaded from web form with SwiftMailer使用 SwiftMailer 从 Web 表单上传的电子邮件附件
【发布时间】:2014-09-29 10:18:32
【问题描述】:

我正在尝试使用 swftmailer - 从 html 网页上的表单上传文件(jpg、ppt、pdf、word 等,最大上传例如:6MB), - 通过电子邮件发送表单的内容通过 swiftmailer 附加文​​件。

我的表单已创建,我正在使用 $_POST 来捕获thankyou.php 页面中的字段值。电子邮件正在发送,但我无法获取要附加的文件!

上传/附加文件需要是一个选项,我在网上找到了一个资源,它只在文件上传时才发送信息,但情况并非总是如此,所以我转向了 swiftmailer,它看起来很棒,但在文档/示例中,它也指定了上传附件的路径,我只想能够读取文件(不指定路径)并将其作为附件与其他表单字段一起发送到给定的电子邮件地址, 姓名电子邮件、电话和消息。

任何帮助将不胜感激。

我的 HTML (contact.html)

 <div id="form-main">
  <div id="form-div">
    <form class="form" method="post" action="thank-you.php" enctype="multipart/form-data">

      <p class="name">
        <input name="fieldFormName" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
      </p>

      <p class="email">
        <input name="fieldFormEmail" type="email" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
      </p>

       <p class="phone">
        <input name="fieldFormPhone" type="number" class="validate[required,custom[onlyLetter],length[0,20]] feedback-input" placeholder="Phone" id="phone" />
      </p>

      <p class="text">
        <textarea name="fieldDescription" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"></textarea>
      </p>

      <p class="upload">
      <small>Send us a Drawing or file</small>
        <input name="fieldAttachment" id="attachment" class="feedback-input" type="file">
      </p>


      <div class="submit">
        <input type="submit" value="SEND" id="button-blue"/>
        <div class="ease"></div>
      </div>
    </form>
  </div>
  </div>

我的 PHP 代码:

<?php
require_once 'lib/swift_required.php';

$fromEmail = $_POST['fieldFormEmail']; 
$fromName = $_POST['fieldFormName'];
$fromPhone = $_POST['fieldFormPhone'];
$fromMessage = $_POST['fieldDescription'];
$fromAttachment = $_POST['fieldAttachment'];

// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();

// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
"sender@domain.com" => "Sender Name"
));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody(
"From: " . $fromName . "\n" .
"Email: " . $fromEmail . "\n" .
"Phone: " . $fromPhone . "\n" .
"Message: " . $fromMessage . "\n"
);
$message->setFrom("$fromEmail", "$fromName");

// *** This is the part I am struggling to understand *** //
$message->attach(Swift_Attachment::newInstance('$fromAttachment'));

// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);

?>

提前谢谢你

【问题讨论】:

    标签: php forms file-upload email-attachments swiftmailer


    【解决方案1】:

    替换你的代码

    $message->attach(Swift_Attachment::newInstance('$fromAttachment'));
    

    关于这个

    $message->attach(
    Swift_Attachment::fromPath($_FILES['fieldAttachment']['tmp_name'])->setFilename($_FILES['fieldAttachment']['name'])
    );
    

    【讨论】:

      【解决方案2】:

      我采用了 DarveL 的代码并添加了一个 if 语句来检查文件是否已上传,因为如果用户不附加文件,我仍然希望发送电子邮件。 下面的代码有效,所以现在我只需要添加一些文件类型/大小验证。

      if(is_uploaded_file($_FILES['fieldAttachment']['tmp_name'])) {
      
      
      $message->attach(
      
      Swift_Attachment::fromPath($_FILES['fieldAttachment']
      ['tmp_name'])->setFilename($_FILES['fieldAttachment']['name'])
      
      );
      
      
      }
      

      【讨论】:

        【解决方案3】:

        检查表单上传。也许你在这方面有错误。

        <form enctype="multipart/form-data" action="" method="POST">
        

        【讨论】:

        • 嗨 DarveL,谢谢您的回复,我已经在上面发布了我的 HTML,它似乎是正确的,我认为我使用以下行不正确:$message->attach(Swift_Attachment::newInstance ('$fromAttachment'));
        • 谁能帮我解决这个问题...拜托?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-18
        • 2019-03-07
        • 1970-01-01
        • 1970-01-01
        • 2012-02-06
        • 2013-08-14
        • 1970-01-01
        相关资源
        最近更新 更多