Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将UploadedFile对象转换为Swift_Mime_MimeEntity_Php_Forms_Symfony_Swiftmailer - Fatal编程技术网

Php 如何将UploadedFile对象转换为Swift_Mime_MimeEntity

Php 如何将UploadedFile对象转换为Swift_Mime_MimeEntity,php,forms,symfony,swiftmailer,Php,Forms,Symfony,Swiftmailer,我有一个Symfony 3.3联系人表单,可以发送电子邮件。现在我正在尝试向表单添加一个附件。我在sendEmail功能中插入以下行: ->attach($data["attachment"]) 。。。我得到以下错误: 传递给Swift_Mime_SimpleMessage::attach()的参数1必须实现 接口Swift\u Mime\u mimeetity,的实例 Symfony\Component\HttpFoundation\File\UploadedFile给

我有一个Symfony 3.3联系人表单,可以发送电子邮件。现在我正在尝试向表单添加一个附件。我在sendEmail功能中插入以下行:

        ->attach($data["attachment"])
。。。我得到以下错误:

传递给Swift_Mime_SimpleMessage::attach()的参数1必须实现 接口Swift\u Mime\u mimeetity,的实例 Symfony\Component\HttpFoundation\File\UploadedFile给定

所以我的问题是:如何将我上传的文件对象转换成SwiftMailer喜欢的东西?

====

编辑#1:我尝试了这个,但没有成功:

$fullFilePath = $data["attachment"]->getPath() . '/' . $data["attachment"]->getClientOriginalName();
$attachment = \Swift_Attachment::fromPath($fullFilePath);
附加该“附件”只会导致电子邮件无法发送,尽管应用程序的行为就像它发送了表单一样

====

编辑#2:进步!我现在可以得到一个有用的错误。此代码

        $extension = $data["attachment"]->guessExtension();
    if($extension !== 'rtf'){
        die('Please give us an rtf file. TODO: Put a better message here!');
    }
    $newFilePath = '/tmp';
    $newFileName = 'temporary.rtf';
    $data["attachment"]->move($newFilePath, $newFileName);
。。。给我一个这样的错误:

无法将文件“/tmp/phpnIqXDr”移动到“/tmp/temporary.rtf”()


。。。这非常令人沮丧,因为我知道
/tmp
是每个用户都可以写的。

以下是最终为我工作的代码:

private function sendEmail($data)
{
    $vgmsContactMail = self::contactMail;
    $mailer = $this->get('mailer');
    /* @var $uploadedFile UploadedFile */
    $uploadedFile = $data["attachment"];
    $extension = $uploadedFile->guessExtension();
    if(!in_array($extension, ['pdf','rtf']) ){
        die('Please upload a .pdf or .rtf file.');
    }
    $newFilePath = '/tmp';
    $newFileName = 'temporary' . rand(0,10000) . '.rtf';
    $uploadedFile->move($newFilePath, $newFileName);
    $attachment = \Swift_Attachment::fromPath('/tmp/' . $newFileName);
    $message = \Swift_Message::newInstance("VGMS Contact Form: ". $data["subject"])
        ->setFrom(array($vgmsContactMail => "Message by ".$data["name"]))
        ->setTo(array(
            $vgmsContactMail => $vgmsContactMail
        ))
        ->setBody($data["message"]."<br>ContactMail :".$data["email"])
        ->attach($attachment)
    ;
    return $mailer->send($message);
}
专用函数sendmail($data)
{
$vgmsContactMail=self::contactMail;
$mailer=$this->get('mailer');
/*@var$uploadedFile uploadedFile*/
$uploadedFile=$data[“附件”];
$extension=$uploadedFile->guessExtension();
if(!in_数组($extension,['pdf','rtf'])){
die('请上传.pdf或.rtf文件');
}
$newFilePath='/tmp';
$newFileName='temporary'.rand(010000)。'.rtf';
$uploadedFile->move($newFilePath,$newFileName);
$attachment=\Swift_attachment::fromPath('/tmp/'.$newFileName);
$message=\Swift\u message::newInstance(“VGMS联系方式:”.$data[“主题”])
->setFrom(数组($vgmsContactMail=>“$data[“name”])的消息)
->setTo(数组(
$vgmsContactMail=>$vgmsContactMail
))
->setBody($data[“message”]。“
ContactMail:”.$data[“email”]) ->附上($附件) ; 返回$mailer->send($message); }
您不需要移动文件,Symfony\Component\HttpFoundation\file\UploadedFile类返回路径,并具有获取文件名和mimetype的方法

此代码适用于我:

$message->attach(
    \Swift_Attachment::fromPath($data["attachment"])
        ->setFilename(
            $data["attachment"]->getClientOriginalName()
        )
        ->setContentType(
            $data["attachment"]->getClientMimeType()
        )
);
归功于