如何从Laravel电子邮件传输中的SwiftAttachment对象获取文件扩展名



这是Laravel 8。我正在扩展Illuminate\Mail\Transport\Transport类以创建自定义邮件传输,以便使用Illuminat\Mail\Mailable使用公司的自定义邮件API。我已经完成了大部分工作,包括文件附件,但是Swift_Mime_SimpleTimeEntity及其派生的类包含getBody((、getFilename((、getSize((和getContentType((,但没有提取文件扩展名的方法。

<?php
namespace AppCustomMailDriver;
use GuzzleHttpClientInterface;
use IlluminateMailTransportTransport;
use IlluminateSupportFacadesHttp;
use IlluminateSupportFacadesLog;
use Swift_Mime_SimpleMessage;
class CustomTransport extends Transport
{
/**
* Guzzle client instance.
*
* @var GuzzleHttpClientInterface
*/
protected $client;
/**
* API key.
*
* @var string
*/
protected $key;
/**
* The API URL to which to POST emails.
*
* @var string
*/
protected $url;
/**
* Create a new Custom transport instance.
*
* @param  GuzzleHttpClientInterface  $client
* @param  string|null  $url
* @param  string  $key
* @return void
*/
public function __construct(ClientInterface $client, string $url, string $key)
{
$this->key = $key;
$this->client = $client;
$this->url = $url;
}
/**
* {@inheritdoc}
*/
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);
$payload = $this->getPayload($message);

try {
// ignore ssl (esp when working in DEV/QA)
$response = Http::withoutVerifying()->withHeaders([
'X-Authorization' => $this->key 
])->post($this->url, $payload);
Log::info($response->body());
} catch (Exception $e) {
Log::error($e->getMessage());
}
$this->sendPerformed($message);
return $this->numberOfRecipients($message);
}
/**
* Get the HTTP payload for sending the message.
*
* @param  Swift_Mime_SimpleMessage  $message
* @return array
*/
protected function getPayload(Swift_Mime_SimpleMessage $message): array
{
// to
if (!empty($message->getTo())) {
$payload['payload']['to']['email'] = key($message->getTo());
}
// cc
if (!empty($message->getCc())) {
$payload['payload']['cc']['email'] = key($message->getCc());
}
// bcc
if (!empty($message->getBcc())) {
$payload['payload']['bcc']['email'] = key($message->getBcc());
}
// subject
$payload['payload']['subject'] = $message->getSubject();
// html
$payload['payload']['message']['html'] = $message->getBody();
// message children contains plain text, attachments, etc
$children = $message->getChildren();
if (!empty($children)) {
foreach($children as $child) {
// attachments
if (get_class($child) === 'Swift_Attachment') {
$payload['payload']['attachments'][] = [
'content' => base64_encode($child->getBody()),
'filename' => $child->getFilename(),
];
}
// plain text
if (get_class($child) === 'Swift_MimePart') {
$payload['payload']['message']['text'] = $child->getBody();
}
}
}
return $payload;
}
}

我不得不走不同的路。我没有使用Swift_Mime_SimpleMessage在Transport类中搜索扩展名,而是将带有原始扩展名的文件名从Illuminate\Mail\Mailable 传递到Transport类

public function build()
{
$tempUpload = request()->file('file_attachment');
$filename = $tampUpload->getClientOriginalName() . "." . $tempUpload->getClientOriginalExtension();
return $this->from($this->from_email, $this->from_name)
->subject('subject line')
->attach($tempUpload, ['as' => $filename)
->view('emails.gce.supplier_supplier')
->with($this->data);
}

最新更新