Core\Mail\Attachment::type
$attachment->type(): stringReturns the media type of an attachment, such as application/pdf or image/png. The media type says what kind of file it is. A mail program uses it to choose how to open the file.
When the attachment was made with the type option, type() returns that type in small letters. Without the option, the type is detected from the first bytes of the content. A PDF file, an image or a ZIP file is detected. Plain text has no such start, so a CSV file or a text file gets application/octet-stream. Give the type option for those files.
The type option takes only the media type. A part after it, such as ; charset=utf-8, throws a RuntimeError when the attachment is made.
type() returns a tainted string, because the type can come from a user. In a test, Core\Test::sentMail()[0]->attachments() returns the attachments of the first mail. Compare the type() of each one with the type you expect.
The examples below show a detected type and a given one, the error for a type with a charset, and a test that checks the types.
Return value
Returns string. The type given when the attachment was made, or the detected type.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Mail\Attachment adds a file to a mail, from a path or from bytes. An inline image's src() is the cid: URL to write in an <img> element. |
Examples
The examples below run with this configuration file.
# The examples send mail through this block. `capture = true` keeps each message# and sends nothing, so no mail leaves the computer.[mail.shop]capture = truefrom = "shop@example.com"from_name = "Example Shop"
# The program may send mail through the `shop` block.[capabilities.mail]send = ["shop"]
# The program may read files in the `files` folder beside this file, and no other file.[capabilities.fs]read = ["files"]Detected or given
<?nvs// Shows where the type of an attachment comes from.
use Core\Mail\Attachment;
// The type of a PDF file is detected from its first bytes. This prints `application/pdf`.echo Attachment::fromFile('files/invoice-1042.pdf')->type(), "\n";
// A CSV file is plain text, so no type is detected. This prints `application/octet-stream`.echo Attachment::fromBytes('orders.csv', "order,total\n1042,59.90\n")->type(), "\n";
// The `type` option gives the type. Capital letters become small letters. This prints `text/csv`.echo Attachment::fromBytes('orders.csv', "order,total\n1042,59.90\n", { type: 'Text/CSV' })->type(), "\n";application/pdfapplication/octet-streamtext/csvA type with a charset
<?nvs// Shows the error for a `type` option that is not only a media type.
use Core\Mail\Attachment;
// The `type` option takes a media type, such as `text/csv`, and nothing after it.// A `; charset=utf-8` part is not allowed, so `fromBytes` throws a `RuntimeError`.try { Attachment::fromBytes('orders.csv', "order,total\n", { type: 'text/csv; charset=utf-8' }); echo "The file is attached.\n";} catch (RuntimeError $error) { echo $error->message, "\n";}Core\Mail\Attachment::fromBytes: "text/csv; charset=utf-8" is not a media type. Write one such as `image/png`.A test checks the types
<?nvs// Shows how to test the type of each file that a mail was sent with.// proof: test
use Core\Mail;use Core\Mail\Attachment;use Core\Test;
class MonthlyReport{ // Sends the report as a PDF file and its numbers as a CSV file. public static function send(string $email): void { Mail::send('shop', [$email], 'Report for November', 'The report is attached.', { attachments: [ Attachment::fromFile('files/invoice-1042.pdf', { name: 'report.pdf' }), Attachment::fromBytes('report.csv', "month,total\n11,4200.00\n", { type: 'text/csv' }), ], }); }}
class MonthlyReportTest{ #[Test] public function eachFileHasItsType(): void { MonthlyReport::send('accounts@example.com');
// The PDF type was detected, and the CSV type was given. array<Attachment> $files = Test::sentMail()[0]->attachments(); Test::assertSame($files[0]->type(), 'application/pdf'); Test::assertSame($files[1]->type(), 'text/csv'); }} MonthlyReportTest ✓ eachFileHasItsType
0 failed, 1 passed, 0 skipped, 0 flaky