0

I am trying to send email via power shell command to thunder bird but i am facing a problem and did all research dint find the solution

my problem is when i run the below command the power shell adds the provided information but then it opens the window and i have to send it manually but that send also should be done by powershell auto am unable to do that

param(
    [string]$attachment='',
    [string]$to='test@outlook.com', # used to specify the email of the recipient
    [string]$cc='', # used to specify the email of the recipient of a copy of the mail
    [string]$bcc='', # used to specify the email of the recipient of a blind copy of the mail*
    [string]$subject='test@outlook.com', # subject of the mail
    [string]$body='test@outlook.com', # body of the mail
    [string]$exec= '"C:\Program Files `(x86`)\Mozilla Thunderbird\thunderbird.exe"' # mail client, if not default type path to Thunderbird installation, eg. C:\Program Files (x86)\Mozilla\Thunderbird\thunderbird.exe
    )

# ------ Get default mail client -------

if ($exec) {
    $MailClient = $exec + "-compose `"%1`""
}
else {
    $node = Get-ItemProperty HKCU:\Software\Classes\mailto\shell\open\command
    if (!$node) {
          $node = Get-ItemProperty HKLM:\Software\Classes\mailto\shell\open\command
    }

    $MailClient = $node.'(default)'
}
# TODO check if default client is compatible

# ------ Read email fields -------------

$cmdParams = New-Object Collections.Generic.List[string]

$emailFields = @{"to"=$to; "cc"=$cc; "bcc"=$bcc; "subject"= $subject; "body" = $body}

$emailFields.Keys | % {     $value = $emailFields[$_] 
    if ($value) {    
        $cmdParams.Add("$_=`'$value`'");
     }

}

# Assign ATTACHMENTS
if ($attachment) {

    if (Test-Path($attachment)) {
        $fullPath = (Get-Item $attachment).FullName;
        $cmdParams.Add("attachment=`'$fullPath`'"); 

    } else {
        echo "The path `"$file`" does not exists."
    }
} elseif (@($input).Count -gt 0) {
    # Try to assign ATTACHMENTS parameter from pipeline 
    $input.reset()
    $attachParam = "attachment=`'"
    foreach ($filename in $input) {
         $fullPath = $filename.FullName + ","
         $attachParam += $fullPath
    }

    $attachParam += "`'"
    $cmdParams.Add($attachParam)
}


# ----- Build & run command -------------

$command = ''

if ($cmdParams.Count -gt 0) {
    foreach ($param in $cmdParams) {
            $command += $param + ","
    }
}

$command = $MailClient -replace "`%1", $command

Invoke-Expression "& $command"
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
Shaik
  • 930
  • 1
  • 20
  • 48

1 Answers1

1

If there is no actual requirement to use Thunderbird Send-MailMessage might be an alternative to use.

Using your baseline it could look like the following:

param(
    [string]$attachment='C:\Temp\file.txt',
    [string]$to='test@outlook.com', # used to specify the email of the recipient
    [string]$cc='', # used to specify the email of the recipient of a copy of the mail
    [string]$bcc='', # used to specify the email of the recipient of a blind copy of the mail*
    [string]$subject='test@outlook.com', # subject of the mail
    [string]$body='test@outlook.com', # body of the mail
    )

# ------ Get default mail client -------

Send-MailMessage -To $to -Cc $cc -Bcc $bcc -Subject $subject -Body $body -Attachment $attachment -From powershell@example.com -SmtpServer mail.example.com

Most of the fields accept string arrays in case you have multiple CC recipients or simiar.

Notice that you will have to tell Send-MailMessage how to connect to the mail server as it won't magically read your Thunderbird configuration. So you will likely have to include -From, -SmtpServer and maybe even -Credential and -Port depending on how your mail server is configured.

Depending on your requirements you might have to dig a bit deeper and directly use Net.Mail.SmtpClient. There are other questions with examples for it like Send mail via gmail with PowerShell V2's Send-MailMessage.

Seth
  • 1,215
  • 15
  • 35