0
votes

I have an Azure Function written in PowerShell with input and output binding as same Blob storage which stores input and output csv files. I was able to send an email from the function with the blob attachment but the email has the attached file of Type File instead of csv. So I have to download and manually add .csv extension to it. How can I send my output csv file with the extension in the attachment? I have my email code as below:

# Send Email using SendGrid with file attachment
$username ="Username"
$password = ConvertTo-SecureString "Password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, 
$password
$SMTPServer = "smtp.sendgrid.net"
$emailFrom = "[email protected]"
[string[]]$emailTo = "some-email"
$subject = "Sending sample email using SendGrid Azure and PowerShell"
$body = "This is sample email sent using Sendgrid account create on Microsoft 
Azure. The script written is easy to use."
$attachment = $outputBlob
Send-MailMessage -smtpServer $SMTPServer -Credential $credential -Usessl - 
Port 587 -From $emailFrom -To $emailTo -Subject $subject -Body $body - 
BodyAsHtml -Attachments $attachment

I have the output file created in the same code, but I'm not sure how to attach it to email.

The outputBlob is a csv file called outputfile.csv which is stored in the same container as the inputfile. I am creating this output file in this same script. So my container name is say "mycsvcontainer" and the path for my blob in the Azure Function binding is "mycsvcontainer/outptfile.csv" the name of the blob is outputBlob which i use in my script above. Similar are the names for inputBlob.

1
What is the type of the outputblob ? Because -Attachments expects the path + filename. - Thomas
The outputBlob is a csv file called outputfile.csv which is stored in the same container as the inputfile. I am creating this output file in this same script. So my container name is say "mycsvcontainer" and the path for my blob in the Azure Function binding is "mycsvcontainer/outptfile.csv" the name of the blob is outputBlob which i use in my script above. Similar are the names for inputBlob. - Payal
@Thomas - do you have any idea how to do this please? - Payal

1 Answers

0
votes
        $StorageAccountName = 'mystorageaccount'
        $StorageAccountKey = "storageaccountkey"
        $StorageContext = New-AzureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey
        $FileName = 'filename'
        $ContainerName  = 'mycontainer'

        #Get attachment from blob storage
        $a = Get-AzureStorageBlobContent -Container $ContainerName -Blob $FileName -Context $StorageContext
        $attachment = $a.Name

        $MyCredential = "SendEMail" 
        $EmailBody = "Test"
        $Body = $EmailBody.replace("|","`n`r")
        $subject = "Tets"
        $userid= "[email protected]"
        $emailTo = "[email protected]"

        #Get the PowerShell credential 
        $Cred = Get-AutomationPSCredential -Name $MyCredential 


        Send-MailMessage `
        -To $emailTo `
        -Subject $subject `
        -Body $Body `
        -UseSsl `
        -Port 587 `
        -SmtpServer 'smtp.sendgrid.net' `
        -Attachments $attachment `
        -From $userid `
        -Credential $Cred `