What are best practices for encoding and decoding email attachments in PHP?

When encoding and decoding email attachments in PHP, it is best practice to use base64 encoding to ensure that the data is properly formatted for email transmission. This involves encoding the attachment before sending it via email and decoding it when receiving and processing the email attachment.

// Encode attachment before sending email
$attachment_data = file_get_contents('path/to/attachment.pdf');
$encoded_attachment = base64_encode($attachment_data);

// Decode attachment when receiving email
$decoded_attachment = base64_decode($received_attachment_data);
file_put_contents('path/to/save/attachment.pdf', $decoded_attachment);