Hello Everyone,
I have a question: is it possible to automatically send the generated PDF file in SuiteCRM? I created a custom Generate PDF using TCPDF in SuiteCRM, and now I want this to be automatically sent to email instead of downloading it.
I hope someone can help me with my concern.
Sorry, that is not available in SuiteCRM, that’s one of the features I think is quite useful so I added it to my add-on PowerReplacer.
@pgr Probably it is possible through customization, right? I am now looking into how I am going to implement this one on my end…
Everything can be done through customization…
Where do you want to trigger this from?
Is it an action button on the Detail view that should send the email?
Hi @pgr , Yes, you are correct. I am going to implement this one in DetailView. I added a custom action button there. Right now, I am only able to update the records status using that custom button, and I want to include an automation of sending an email with an attachment upon updating the records status.
Hello @pgr , I was able to implement this one. But my problem is that the variable I`ve set in the email template from the Opportunity module does not appear on my email.
Email Template
Email that I`ve Receive
This is how I implemented this one.
public function attachSAFPDF()
{
$this->format = 'A4';
$this->setHeaderMargin(4);
$this->setFooterMargin(12);
$this->SetLeftMargin(8);
$this->SetRightMargin(8);
$this->SetAutoPageBreak(true, 5); // originally 8
$this->writePage1();
$this->writePage2();
$pdf_title = "{$this->bean->name}.pdf";
$temp = $this->Output($pdf_title, 'S');
$pdfLocation = 'upload/SAFPDF/' . $pdf_title;
file_put_contents($pdfLocation, ltrim($temp));
$focus = \BeanFactory::getBean('Opportunities', $this->bean->id);
$this->sendEmail($focus,$emailAddress,$pdfLocation,$pdf_title);
}
function sendEmail($focusBean, $emailAddress, $pdfLocation, $filname)
{
require_once('modules/EmailTemplates/EmailTemplate.php');
$emailTemplate = new EmailTemplate();
$emailTemplate = $emailTemplate->retrieve('5b7c511a-d452-adc6-6e4f-64014c9fe482');
$emailTemplate->parsed_entities = null;
$temp = array();
$template_data = $emailTemplate->parse_email_template(
array(
"subject" => $emailTemplate->subject,
"body_html" => $emailTemplate->body_html,
"body" => $emailTemplate->body
),
$focusBean->module_dir,
$focusBean,
$temp
);
$email_body = $template_data["body_html"];
$email_subject = $template_data["subject"];
require_once('include/SugarPHPMailer.php');
$emailObj = new Email();
$defaults = $emailObj->getSystemDefaultEmail();
$mail = new SugarPHPMailer();
$mail->setMailerForSystem();
$mail->From = $defaults['email'];
$mail->FromName = $defaults['name'];
$mail->Subject = (!empty($email_subject)) ? $email_subject : " ";
$mail->Body = from_html($email_body);
$mail->IsHTML(true);
$mail->prepForOutbound();
$mail->AddAddress($emailAddress);
$mail->AddAttachment($pdfLocation, $filname, 'base64', mime_content_type($pdfLocation));
if (!@$mail->Send()) {
$GLOBALS['log']->fatal("An error occurred while sending pdf for" . $mail->ErrorInfo);
unlink($pdfLocation);
$is_send = 'notsend';
} else {
$is_send = 'send';
}
return $is_send;
}
Probobly my error was on this code below:
$focus = \BeanFactory::getBean('Opportunities', $this->bean->id);
The $this->bean->id is the id of the opportunity record.
well, you can check if this is ok or not, by setting a breakpoint in your IDE and inspecting the variable. Or by logging some values.
@pgr , The value of the bean is correct. It will fetch the values of the opportunity base on record id, but it cant parse data for the email template that I
ve set in the Email Template Module.
Sample below:
I used the out of the box variable in the template
Based on the screenshot shown above, I assigned or used the opportunity name,
$opportunity_name.
I call the email template through this code below:
require_once('modules/EmailTemplates/EmailTemplate.php');
$emailTemplate = new EmailTemplate();
$emailTemplate = $emailTemplate->retrieve('5b3db9b4-9817-24bc-d271-64055ccd2c8b');
//$emailTemplate->parsed_entities = null;
$temp = array();
$template_data = $emailTemplate->parse_email_template(
array(
"subject" => $emailTemplate->subject,
"body_html" => $emailTemplate->body_html,
"body" => $emailTemplate->body
),
$focusBean->module_dir,
$focusBean,
$temp
);
$email_body = $template_data["body_html"];
$email_subject = $template_data["subject"];
On the email that I`ve received, there is no value for the variable, just blank.
Are you using a debugger? What do you see when you step inside the parse_email_template function?