Skip to content

Comments

[Email] Add attachment functionality#10374

Open
CamilleBeau wants to merge 1 commit intoaces:mainfrom
CamilleBeau:2026_02_18_email_attachments
Open

[Email] Add attachment functionality#10374
CamilleBeau wants to merge 1 commit intoaces:mainfrom
CamilleBeau:2026_02_18_email_attachments

Conversation

@CamilleBeau
Copy link
Collaborator

Brief summary of changes

This PR adds to the Email.class.inc library file to be able to add attachments to emails sent by LORIS.

This is done by detecting whether an array of attachments was added, and if so, changing the Content-Type to multi-part and adding each attachment individually. If no attachments are passed as an argument, then the Email class continues as it did before.

  • Have you updated related documentation?

Testing instructions (if applicable)

  1. Add a 9th argument to an Email::send function somewhere in LORIS with a full filepath passed through
  2. Have the email sent to your own address, and make sure that the attachment is added to the sent email properly
  3. Try sending an email like normal without an attachment, and make sure that that works as usual.

Link(s) to related issue(s)

  • Resolves # (Reference the issue this fixes, if any.)

@github-actions github-actions bot added the Language: PHP PR or issue that update PHP code label Feb 18, 2026
@ridz1208
Copy link
Collaborator

@CamilleBeau FYI here is how I had implemented it. I really don't recall why I did moved away from the Email class but I rememebr trying and giving up at a certain point.

            //email
            $config =& NDB_Config::singleton();
            $from = ($config->getSetting('mail'))['From'];
            
            $email = new PHPMailer();
            $email->SetFrom($from);
            $email->Subject = "THIS IS THE SUBJECT LINE";
            $email->Body = "THIS IS THE BODY";
            $recipientEmail = $config->getSetting("mailto");
            $email->AddAddress($recipientEmail);
            $email->AddAddress("rida.abou-haidar@mcin.ca");
            $email->AddAttachment( $EOS_path , $EOS_file);
            $email->AddAttachment( $o_path , $o_file);
            $email->Send();


foreach ($attachments as $file) {

if (!file_exists($file['path'])) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably throw an exception or at least log an error if it's trying to email a file that doesn't exist or can't be accessed?

);

$filename = $file['name'] ?? basename($file['path']);
$filetype = $file['type'] ?? 'application/octet-stream';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're passing filename/paths you can try mime_content_type before falling back on application/octet-stream

string $bcc = '',
string $type="text/plain"
string $type="text/plain",
array $atachments = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason GitHub isn't giving me the "suggest changes" option but attachments is spelled wrong (so I'm not sure how this is working because it's spelled properly in the code..)


// Use chunk_split for proper line length
$fileContent = chunk_split(
base64_encode(file_get_contents($file['path']))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the format of $file hard to follow because it's not documented anywhere.

Does the array format have any fields that couldn't be derived from using an iterable of \SplFileInfo? If not, I recommend using that.. if so, maybe you should add an EmailAttachment class where the format could be more explicit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Language: PHP PR or issue that update PHP code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants