1
  1. I am using business rules custom action to access the submission data on entity insert.
  2. I am able to get the file id and ultimately the realpath.
  3. the real path is sites/default/files/private/webform/[webform_name]/_sid_/[file_name].
  4. When I try to attach this file it is as if it were not there and ultimately gets moved into sites/default/files/private/webform/[webform_name]/[webform_submission_id]/[file_name].
  5. I have tried to use the above path but it as if the file had not been written yet.
  6. After the action has been completed I can attach the file with no problem.

The issue I am having is how can I get this illusive file and attach it to an email pragmatically.

here is the snipet of code that will generate the "/_sid_/" path

$file_id = $event['entity']->getData()[$file_field];
$file = \Drupal\file\Entity\File::load($file_id);
$realpath = \Drupal::service('file_system')->realpath($file->getFileUri());

Thank you for your responses and help on this.

0

Well, as first posts go, it ended up as a "stupid" mistake on my end. The webfrom module is well documented and the solution was very straight forward. (In short I was pushing an array into the ->AddAtachment method in PHPMailer.) For reference to anyone else here is my working code:

[module_name]\src\Plugin\BusinessRulesAction\SMTPAuthEmail.php

namespace Drupal\[module_name]\Plugin\BusinessRulesAction;
use Drupal\[module_name]\Controller\[module_name]Controller;
use Drupal\business_rules\ActionInterface;
use Drupal\business_rules\Events\BusinessRulesEvent;
use Drupal\business_rules\ItemInterface;
use Drupal\business_rules\Plugin\BusinessRulesActionPlugin;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render;
use Drupal\Core\File\FileSystem;
use Drupal\file\Entity\File;
class SMTPAuthEmail extends BusinessRulesActionPlugin {
    public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
        //var_dump($item);
        $settings['email_settings'] = [
            '#type'          => 'fieldset',
            '#title'         => t('Email Settings'),
        ];
//this is just to show that I have a form for business rules that is being referenced later below.
    public function execute(ActionInterface $action, BusinessRulesEvent $event) {
    $send_email = new [module_name]Controller;
    $message = $action->getSettings();
    $css = file_get_contents(drupal_get_path('module', '[module_name]') . '/css/email.css');
    $entity_type = $event['entity']->getEntityTypeID();
    $node_storage = \Drupal::entityTypeManager()->getStorage($entity_type);
    $view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity_type);
    $node = $node_storage->load($event['entity']->id());
    $build = $view_builder->view($node, 'table');
    $file_fields = $event['entity']->getWebform()->getElementsManagedFiles();
     if($file_fields){
        foreach ($file_fields as $file_field) {
            if (!empty($event['entity']->getData()[$file_field])) {
                $file_id = $event['entity']->getData()[$file_field];
                $file = \Drupal\file\Entity\File::load($file_id);
                $realpath = \Drupal::service('file_system')->realpath($file->getFileUri());
                $message['attachment'][] = $realpath;
            }
        }
    }
    $message['body'] = '<style>' . $css . '</style>' . render($build);
    $send_email->smtp_email($message);

}

[module_name]\src\Controller[ModuleName]Controller.php

class [ModuleName]Controller extends ControllerBase {

    public function smtp_email($data) {

        $mail = new PHPMailer();
        $mail->IsHTML(true);
        $mail->IsSMTP(); // enable SMTP
        $mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
        $mail->SMTPAuth = true; // authentication enabled
        if($data['tls']){
            $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
        }
        $mail->Host = $data['host'];
        $mail->Port = $data['port']; // or 587

        $mail->Username = $data['username'];
        $mail->Password = $data['password'];
        $mail->SetFrom($data['from']);
        $mail->Subject = $data['subject'];
        $mail->Body = $data['body'];
        if(isset($data['attachment'])) {
            foreach ($data['attachment'] as $attachment) {
                $mail->AddAttachment($attachment);
            }
        }
        foreach(explode(";",$data['to']) as $recipient) {
            $mail->AddAddress($recipient);
        }
        if(!$mail->Send()) {
            $message = "Mailer Error: " . $mail->ErrorInfo;
            $message_type = 'error';
        } else {
            $message =  "Message has been sent";
            $message_type = 'notice';
        }
        \Drupal::messenger()->addMessage(t($message), $message_type);
    }

}

I hope this helps someone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.