无法使文件附件与Swiftmailer一起使用

时间:2016-02-28 19:45:43

标签: php swiftmailer

我正在尝试使用swiftmailer从表单上传文件,但我仍然遇到错误。 我真的不知道我哪里错了。任何帮助和解释将不胜感激。

index.html

     <form method="post" action="mail.php" enctype="multipart/form-data">                               
       Attach a file: <input type="file" id="attachment" name="attachment">
       <button type="submit">SUBMIT</button>
     </form>

mail.php (正确) 然后它导入 mail_sender.php

mail_sender.php

    <?php

    $file = 'attachment/'.basename($_FILES['attachment']['name']);

    $upload = move_uploaded_file($_FILES['attachment']['tmp_name'],$file);


    if(isset($timeZone) && $timeZone != ""){
        date_default_timezone_set($timeZone);
    }
    else{
        date_default_timezone_set("UTC");
    }

    // Require the Swift Mailer library
    require_once 'swift_required.php';

    $messageText = "";

    if($emailMethod == 'phpmail'){ 
        $transport = Swift_MailTransport::newInstance(); 
    }elseif($emailMethod == 'smtp'){
        $transport = Swift_SmtpTransport::newInstance( $outgoingServerAddress, $outgoingServerPort, $outgoingServerSecurity )
        ->setUsername( $sendingAccountUsername )     
        ->setPassword( $sendingAccountPassword );
    }

    $mailer = Swift_Mailer::newInstance($transport);

    // Creating the message text using fields sent through POST
    foreach ($_POST as $key => $value)
    {
        // Sets of checkboxes will be shown as comma-separated values as they are passed in as an array.
        if(is_array($value)){
            $value = implode(', ' , $value);
        }
        $messageText .= ucfirst($key).": ".$value."\n\n";
    }

    if(isset($_POST['email']) && isset($_POST['name']) ){
        $fromArray = array($_POST['email'] => $_POST['name']);
    }else{ $fromArray = array($sendingAccountUsername => $websiteName); }

    $message = Swift_Message::newInstance($emailSubject)
      ->setFrom($fromArray)
  ->addPart(strip_tags($messageText), 'application/pdf')
  ->attach(Swift_Attachment::fromPath($file))
  ->setTo(array($recipientEmail => $recipientName))->setBody($messageText, 'text/html');


    }

    // Send the message or catch an error if it occurs.
    try{
        echo($mailer->send($message));
    }
    catch(Exception $e){
        echo($e->getMessage());
    }
    exit;
    ?>

提前致谢

0 个答案:

没有答案