MODX Community

Attaching file uploaded from form to email with modmail

I am using the Login plugin, and on the register snippet I am trying to get the two files that the user has uploaded in the file inputs and attach them to an email using modmail in a post hook. The problem is that as of now the files are not attaching to the email. Here is my snippet call:

<?php
$values = $hook->getValues();
$email = $values['email'];
$company = $values['companyName'];
$liability = $values['liabilityFile'];
$comp = $values['compFile'];
$message = 'Hi, a new User signed up: '.$company . ' with email '. $email .'.';
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'send@email.com');
$modx->mail->set(modMail::MAIL_FROM_NAME,'Registration Files');
$modx->mail->set(modMail::MAIL_SUBJECT,"{$company} Files");
$modx->mail->address('to','receive@email.com');
$modx->mail->setHTML(true);

$modx->mail->mailer->addAttachment($liability);
$modx->mail->mailer->addAttachment($comp);
if (!$modx->mail->send()) {
    $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '. $modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
/* tell our snippet we're good and can continue */
return true;

Im wondering is anyone has any input.
Thanks in advance.

1 Like

According to this thread you need the full path to the file

$attachment = '/tmp/***.***';

Its an older thread but give it a try

Is this an existing file or from a form?

1 Like

Just tried

<?php
$values = $hook->getValues();
$email = $values['email'];
$company = $values['companyName'];
$liability = $_FILES['liabilityFile'];
$comp = $_FILES['compFile'];
$message = 'Hi, a new User signed up: '.$company . ' with email '. $email .'.';
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'send@email.com');
$modx->mail->set(modMail::MAIL_FROM_NAME,'Registration Files');
$modx->mail->set(modMail::MAIL_SUBJECT,"{$company} Files");
$modx->mail->address('to','receive@email.com');
$modx->mail->setHTML(true);

$modx->mail->mailer->addAttachment($liability['name'], $liability['tmp_name'], 'base64', $liability['type']);
$modx->mail->mailer->addAttachment($comp['name'], $comp['tmp_name'], 'base64', $comp['type']);
if (!$modx->mail->send()) {
    $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '. $modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
/* tell our snippet we're good and can continue */
return true;

also didnt work

1 Like

lol had the file names backwards…

had this:

$modx->mail->mailer->addAttachment($liability['name'], $liability['tmp_name'], 'base64', $liability['type']);
$modx->mail->mailer->addAttachment($comp['name'], $comp['tmp_name'], 'base64', $comp['type']);

supposed to be this:

$modx->mail->mailer->addAttachment($liability['tmp_name'],$liability['name'],  'base64', $liability['type']);
$modx->mail->mailer->addAttachment($comp['tmp_name'],$comp['name'],  'base64', $comp['type']);
2 Likes