Goal: I need to send an email with a pdf attachment in Drupal 8.
Progress
I am able to send an email with subject and message but have never been able to get the attachment. When I turn on the logic to attach the file to the email, the email never gets sent but the mail service returns True as if the email was sent anyway. There are no errors and the logs say nothing.
I have the following modules installed:
- SMTP
- MailSystem
- MimeMail
What I've tried
- Tried using
$message['params']['files'][] = $params['attachments']
instead of$message['params']['attachments'][] = $params['attachments']
like some examples have recommended - Tried changing
$file->uri
to$file->filepath
Tried attaching the file like so:
$params['attachments']['filecontent'] = $this->generatePDF($foa_list); $params['attachments']['filename'] = 'MyFormsOfAssistance.pdf'; $params['attachments']['filemime'] = 'application/pdf';
Tried attaching the file by path alone with an array:
$attachment = array( 'filepath'=>'sites/default/files/MyFormsOfAssistance.pdf', ); $params['attachments'] = $attachment;
Tried attaching the file a different way:
$attachment = array( 'filepath' => 'sites/default/files/MyFormsOfAssistance.pdf', 'filename' => 'MyFormsOfAssistance.pdf', 'filemime' => 'application/pdf' ); $params['attachments'][] = $attachment;
Setup
- There is email landing page in which a user enters the recipient's email address, subject for the email, and message for the email.
- When user clicks Send button, that data is routed to my Email Controller
- Email controller creates the pdf file, attempts to attach the pdf to the email, and then sends the email.
- I have the hook_mail() function defined in my module file.
Here is the code for my Email Controller:
public function submit_email($foa_list = null) {
$lang = "en";
$email_to = "[email protected]";
$params['subject'] = "Test subject";
$params['body'] = "Test body";
// -------------------- Attachment Logic -----------------------
// Create pdf file and store file
file_put_contents('sites/default/files/MyFormsOfAssistance.pdf', $this->generatePDF($foa_list));
// Attach pdf file to email params
$file = new \stdClass();
$file->uri = 'sites/default/files/MyFormsOfAssistance.pdf';
$file->filename = 'MyFormsOfAssistance.pdf';
$file->filemime = 'application/pdf';
$params['attachments'][] = $file;
// Send email
\Drupal::service('plugin.manager.mail')->mail('email', 'email', $email_to, $lang, $params);
$response = new Response();
$response->setContent("true");
return $response;
}
Here is the hook_mail() code in my Email module file:
// Note that print_and_email is what my module is called.
function print_and_email_mail($key, &$message, $params) {
switch($key) {
case 'email':
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
$message['headers'] = [
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
];
// $message['params']['files'][] = $params['attachments']; // Note: I have seen other examples implement attachments this way
$message['params']['attachments'][] = $params['attachments'];
break;
}
}
The Drupal 8 documentation isn't really helpful and I would like to be able to figure this out without installing SwiftMailer, like some sources have recommended. Really not sure why this isn't working as it has worked for others. What am I missing?