Want to send file contents as the body of an email

I have a powershell script that is triggered by a deployment in Octopus that creates an html file showing the results of a process. I would like to add another step to the project that would send an email with the content of that file as the email body.
Can you think of a way to accomplish this? For example, is there a way for my script to save the html content as the value of an Octopus variable that could then be accessed by an email step in Octopus? Or is there a way to script the email sending, rather than using an email step?

Hi Jake,

Thanks for getting in touch!

You may be able to save the html contents to an Octopus Variable and refer to it in an Email step (see this blog post for more info), however I’m not entirely sure how well HTML will get processed. It’s definitely worth a try. There’s a similar support request from someone that might help as well.

If that doesn’t work, you might need to write a specific PowerShell step to retrieve the HTML file and send an email. Unfortunately I don’t have any examples on that, and you would have to consider that it runs on the Tentacle as well.

Hope that helps!

Damo

Hi Jake,

I’m reliably informed by Vanessa that saving the contents to an Octopus Output variable will work!

Thanks,
Damo

In my script where I’m trying to save the value to an Octopus variable, like so:
Set-OctopusVariable -name ‘e2eResultsHtml’ -value ‘$e2eResultsHtml’

I’m getting this error:
The term ‘Set-OctopusVariable’ is not recognized as the name of a cmdlet, function, script file, or operable program.

Hi Jake,

Can I confirm that you’re executing this PowerShell within the context of a deployment?

Can you tell me more about the step that’s running it (including the script if possible)?

The more information you can send the better!

Damo

Yes. It’s being done as part of a deployment.
The script in the step details in Octopus is simply this:

Set-ExecutionPolicy Unrestricted -Force
powershell -File “$e2eLocalFolder\E2E_Master.ps1” -reportPath $e2eLocalFolder\e2eResults\ -reportPrefix e2eResults- -summaryPrefix e2eSummary- -useGrid true

The “E2E_Master.ps1” file contains most of the logic we want to run – so it’s more accessible to the developers and is in source control. Near the end of that script is the code to save the data to the Octopus variable. Is there some way to give that script the ability to use the Set-OctopusVariable cmdlet?
I was wondering about passing scope to that script or something. Or perhaps referencing something like Octopus.Client.Model.EnvironmentResource in the script (we’re using that elsewhere).

Another option might be to put logic into the step in Octopus to read a file and save the data to the variable there rather than in the E2E_Master script.

Hi Jake,

Ahh, I see the issue. Within your script, you’re starting a new instance of PowerShell - because it’s a new session, it won’t have any of the Octopus Cmdlets (like Set-OctopusVariable).

The easiest way to do this is to use the Call operator (&) instead of calling powershell. That means it will run in the same context as Octopus, so those Cmdlets will be available.

So your command would become:

Set-ExecutionPolicy Unrestricted -Force
& "$e2eLocalFolder\E2E_Master.ps1" -reportPath $e2eLocalFolder\e2eResults\ -reportPrefix e2eResults- -summaryPrefix e2eSummary- -useGrid true

Hope this helps,

Damo