# Sending Report over a mail

**URL:** <https://forum.inductiveautomation.com/t/sending-report-over-a-mail/71887>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 31, 2023, 8:30am UTC](https://forum.inductiveautomation.com/t/sending-report-over-a-mail/71887 "2023-03-31T08:30:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [March 31, 2023, 8:30am UTC](https://forum.inductiveautomation.com/t/sending-report-over-a-mail/71887/1 "2023-03-31T08:30:46Z")

</div>

Hi I would like to send a report that looks like below,

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/3/932e185f31a663a43367d85f6b22983799d5341d.png)  
Parameters used: StartDate, EndDate,iProcess  
I want the screen to append for iProcess from 1 to 5 and send this report over a mail.  
I am triggering this function via memory tag with script below,

```auto
if currentValue.value and not initialChange:
	    import system
	
	    # Get logger
	    logger = system.util.getLogger("Report")
	
	    # Define report path and date parameters
	    reportPath = "Graphical Loss Report"
	    StartDate = system.tag.readBlocking("[.]../../Global/ShiftStart")[0].value
	    EndDate = system.tag.readBlocking("[.]../../Global/ShiftEnd")[0].value
	
	    # Reset trigger tag
	    system.tag.writeBlocking("[.]ShiftNotificationTrigger", [0])
	
	    # Initialize flag variable to track exceptions
	    hasExceptions = False
	
	    # Initialize report bytes
	    reportBytes = bytearray()
	
	    # Loop through iProcess values and add each process page to report
	    for iProcess in range(1, 5):
	        logger.info("Adding process page for iProcess %d" % iProcess)
	
	        # Set report parameters and get process page bytes
	        params = {"StartDate": StartDate, "EndDate": EndDate, "iProcess": iProcess}
	        try:
	            processBytes = system.report.executeReport(
	                path=reportPath,
	                project="XYZ",
	                parameters=params,
	                fileType="pdf"
	            )
	        except Exception as e:
	            # Catch report generation exceptions and set flag variable
	            logger.error("Error generating report for iProcess %d: %s" % (iProcess, str(e)))
	            hasExceptions = True
	            continue
	
	        # Append process page bytes to report bytes
	        reportBytes.extend(processBytes)
	
	    if hasExceptions:
	        # Send email indicating that report generation failed for one or more iProcess values
	        system.net.sendEmail(
	               smtpProfile="XYZ",
	            fromAddr="ignition-noreply@xxx.tech",
	            to=["xysz.com"],
	            subject="Combined Report for all iProcess - Error",
	            body="Failed to generate report for one or more iProcess values.",
	        )
	    else:
	        # Send email with report bytes
	        system.net.sendEmail(
	            smtpProfile="XYZ",
	            fromAddr="ignition-noreply@xxx.tech",
	            to=["xysz.com"],
	            subject="Combined Report for all iProcess",
	            body="Please find attached the combined report for all iProcess.",
	            attachments=[("CombinedReport.pdf", reportBytes)]
	        )
	
	        logger.info("Combined report sent")

```

I am getting a mail without attachment.  
IDK what is missed/wrong here.  
Kindly assist in resolving this

---

<div class="post-metadata">

**Author:** ![jonathan.taylor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jonathan.taylor/32/4048_2.png) [@jonathan.taylor](https://forum.inductiveautomation.com/u/jonathan.taylor)\
**Post date:** [March 31, 2023, 9:03am UTC](https://forum.inductiveautomation.com/t/sending-report-over-a-mail/71887/2 "2023-03-31T09:03:35Z")

</div>

You're using system.report.executeReport, try using [system.report.executeAndDistribute](https://docs.inductiveautomation.com/display/DOC81/system.report.executeAndDistribute) as this has the ability to send it via email.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [March 31, 2023, 11:55am UTC](https://forum.inductiveautomation.com/t/sending-report-over-a-mail/71887/3 "2023-03-31T11:55:54Z")

</div>

I tried the same with below script,

```auto
		if currentValue.value and not initialChange:
		import system
		
		# Get logger
		logger = system.util.getLogger("Report")
		
		# Define report path and date parameters
		reportPath = "Graphical Loss Report"
		StartDate = system.tag.readBlocking("[.]../../Global/ShiftStart")[0].value
		EndDate = system.tag.readBlocking("[.]../../Global/ShiftEnd")[0].value
		# Reset trigger tag
		system.tag.writeBlocking("[.]ShiftNotificationTrigger", [0])
		# Loop through iProcess values and generate report for each one
		for i in range(1, 5):
			iProcess = i
			logger.info("Generating report for iProcess %d" % iProcess)
			
			# Set report parameters and execute distribution
			params = {"StartDate": StartDate, "EndDate": EndDate, "iProcess": iProcess}
			logger.info(str(params))
			system.report.executeAndDistribute(path=reportPath,project="XYZ_MES",action="email",parameters= params,
			actionSettings={"to": ["dharani.t@batpl.com"],"smtpServerName": "XYZ_SMTP","subject": "Report for iProcess %d" % iProcess,"from": "ignition-noreply@XYZ.tech","attachmentName": "Loss"})
			logger.info("Report for iProcess %d sent" % iProcess)
	        
	        

```

But am getting separate mail for each process but i need all the process as page by page.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 31, 2023, 3:38pm UTC](https://forum.inductiveautomation.com/t/sending-report-over-a-mail/71887/4 "2023-03-31T15:38:59Z")

</div>

> [@Dharani\_T](#):
>
> ```auto
> # Initialize report bytes
> reportBytes = bytearray()
> 
> ...
> # Append process page bytes to report bytes
> reportBytes.extend(processBytes)
> 
> ```

That's not doing what you want to be doing.  
If you want to email five _separate_ PDF attachments on one email, you can make a **list** , execute each report, and store the generated data into that list to pass in to sendEmail in the attachmentData parameter.

You **cannot** directly combine the binary data for different PDFs and expect a valid PDF as the output, though. There are headers and other information that will _at best_ leave you with a totally invalid PDF-like binary blob. Ignition doesn't have any internal facility to combine PDFs, so I would highly recommend just sending five attachments. If that's really not an option, you will have to either integrate a third party Java library that can stitch together PDFs, or use a third party tool to merge the PDFs.

> [@Multiple reports with single schedule based on parameter](https://forum.inductiveautomation.com/t/multiple-reports-with-single-schedule-based-on-parameter/40613/8):
>
> Not directly within Ignition. PDFs are complicated. There’s lots of options for merging PDFs, I can’t speak to any particular one. This thread on SO is Linux-focused, so may not be helpful, but some of these tools may be available on Windows:

> [@Merge PDFs with the Similar name](https://forum.inductiveautomation.com/t/merge-pdfs-with-the-similar-name/54145):
>
> Hello Everyone, I have bunch of files in single folder. Now, out of those files,I will have to find a files with similar name and that would look like “Filename\_invoice” and “Filename\_return”. Basically I will have to find those files(PDFs). Merge them and print them. Do you think merging can be done in ignition without any external module? Let me know if anyone have done this before. Thank you

> [@Execute the same report x times and perspective download as multiple pages in a single file](https://forum.inductiveautomation.com/t/execute-the-same-report-x-times-and-perspective-download-as-multiple-pages-in-a-single-file/29407/2):
>
> Combining PDFs is not as simple as just appending the byte arrays to each other - in fact, it’s decidedly non-trivial. Looks like there are some command line tools you could use: [https://stackoverflow.com/questions/2507766/merge-convert-multiple-pdf-files-into-one-pdf](https://stackoverflow.com/questions/2507766/merge-convert-multiple-pdf-files-into-one-pdf) - you’d have to execute the reports and save them somewhere on the gateway, then run an external command, then load those bytes into system.perspective.download.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [April 1, 2023, 4:32am UTC](https://forum.inductiveautomation.com/t/sending-report-over-a-mail/71887/5 "2023-04-01T04:32:03Z")

</div>

> [@paul-griffith](#):
>
> ive _separate_ PDF attachments on one email,

But i require a single report pdf in which each process is appended

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 1, 2023, 11:53am UTC](https://forum.inductiveautomation.com/t/sending-report-over-a-mail/71887/6 "2023-04-01T11:53:55Z")

</div>

Then you will need to use an external tool to combine them. One of the solutions Paul linked should do.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [April 3, 2023, 6:32am UTC](https://forum.inductiveautomation.com/t/sending-report-over-a-mail/71887/7 "2023-04-03T06:32:08Z")

</div>

Or is there a way to use script data source so that report charts will be dynamically arranged and hence now we will be having a single report?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 3, 2023, 12:04pm UTC](https://forum.inductiveautomation.com/t/sending-report-over-a-mail/71887/8 "2023-04-03T12:04:14Z")

</div>

No.

The closest you could come would be to use a table group.
