I am sending an email with an attachment using Yii2 and Swiftmailer. When I try to open a file in my Google account, Google writes that the file is damaged. I tried to download the file and open it, but my excel also told me that the file is damaged.
BUT, I tried to open the file before sending, and it opens perfectly. I also tried to send the file through my email client and then the file also opens up nicely.
I tried using different SMTP servers, nothing changes.
What could be the problem?
I enclose the code.
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'XXX',
'username' => 'XXX',
'password' => 'XXX',
'port' => '465',
'encryption' => 'ssl',
],
],
and
$emailManager = new EmailManager();
$mailer = $emailManager->actionSendfile($address, $filename, $subject, [
'contentType' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
]);
and
public function actionSendfile($to, $filePathArray, $subject, $options = [])
{
$send = Yii::$app->mailer->compose()
->setFrom(['noreply@example.com'])
->setTo($to)
->setSubject($subject)
->setHtmlBody("Some text");
if(is_array($filePathArray)){
foreach($filePathArray as $value){
$send->attach($value, $options);
}
} else {
$send->attach($filePathArray, $options);
}
return $send->send();
}