Send mail with image from clipboard

Hello,

I am new to powershell and have recently alot experimented with sending emails using powershell. My latest objective was to send an email with a .png that is saved to the clipboard using the “printscreen”-button. I tried sending the image as a attachment, and when I do that I get a mail but no picture. I also tried sending an embedded image instead of a attachment, but I had no luck there either. Is it possible to send the picture that’s on the clipboard? If so, I would appreciate if anyone could help me with it.

Edit: Just for clarifications, I press the “printscreen”-button before I run the script so I have a image on clipboard.

Here is the code:

 

$Username = "mail";
$Password = "password";
$path = Get-Clipboard -Format Image;

function Send-ToEmail([string]$email, [string]$attachmentpath){

    $message = new-object Net.Mail.MailMessage;
    $message.From = "mail";
    $message.To.Add($email);
    $message.Subject = "subject text here...";
    $message.Body = "body text here...";
    $attachment = New-Object Net.Mail.Attachment($attachmentpath);
    $message.Attachments.Add($attachment);

    $smtp = new-object Net.Mail.SmtpClient("smtp.outlook.com", "587");
    $smtp.EnableSSL = $true;
    $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
    $smtp.send($message);
    write-host "Mail Sent" ; 
    $attachment.Dispose();
 }
Send-ToEmail  -email "to-mail" -attachmentpath $path;

… just an idea to maybe simplify your task. Instead of dealing with the clipboard you could press <Windows>+<Printscreen>. That will safe the screenshot as a file to your pictures folder. You could attach this to your email. It might be easier as well to use Send-Mailmessage instead of the Outlook method.

[quote quote=206799]… just an idea to maybe simplify your task. Instead of dealing with the clipboard you could press <Windows>+<Printscreen>. That will safe the screenshot as a file to your pictures folder. You could attach this to your email. It might be easier as well to use Send-Mailmessage instead of the Outlook method.

[/quote]
That would work aswell but I want to be able to repate this. Considering that all screenshots get unique name it would be hard to reffer to what screenshot I want to send.

I could remove the screenshot after sending it, that would mean that if I take a new screenshot it would have the same name as the last one and then I can reffer to the same image name in the script. But for this to work I have to assume that there are no screenshots in the screenshots folder before I run the script because if there are any, I would not be able to delete the latest screenshot as I do not know how it will be named. And as I want to do it on multiple computers I don’t think this idea is good in my case.

Simply choose the last one. :wink: You could even check its age and only allow pictures wtih a maximum age of a few seconds or so.

Press ‘PrintScreen’ button before sending email. This will ask for credentials (no need to hardcode username/password), save clipboard image to current directory then attach it.

Function Send-MyMail ([string]$Email){
# Prompt for username/password
    $cred = Get-Credential

# Get and save image from clip
    $image = Get-Clipboard -Format Image
    $image.Save('.\image.png')

# Set email properties
    $prop = @{
        From = 'mail'
        To = $Email
        Subject = 'subject'
        Body = 'body'
        Attachments = '.\image.png'
        SmtpServer = 'smtp.outlook.com'
        Port = '587'
        UseSsl = $true 
        Credential = $cred
    }

# Add parameters to cmdlet
    Send-MailMessage @prop
}

# Test Send-MyMail Function
Send-MyMail -Email addressto@mail.mil

Google shows several options to get the ScreenShot with code … here is just one …

https://www.pdq.com/blog/capturing-screenshots-with-powershell-and-net/

And to get REALLY complex …

https://www.powershellgallery.com/packages/RoughDraft/0.1/Content/Get-Screenshot.ps1

 

 

Yes that would work. But I tried making something like this but realized the script would get kinda complicated. It would be easier if I just save the screenshot on the clipboard to a desired map with a desired name using the Get-clipboard command. I could then change the original script to send the file with the desired name, and after sending it I could force remove the same file. That way I can repeate this without having to assume anything.

I searched online on how to do this and got 2 scripts but none of them work:

$img = get-clipboard -format image
$img.save("c:\temp\temp.jpg")
Add-Type -AssemblyName System.Windows.Forms
$clipboard = [System.Windows.Forms.Clipboard]::GetDataObject()
if ($clipboard.ContainsImage()) {
    $filename='c:\temp\test3.png'         
    [System.Drawing.Bitmap]$clipboard.getimage().Save($filename, [System.Drawing.Imaging.ImageFormat]::Png)
    Write-Output "clipboard content saved as $filename"
} else {
    Write-Output "clipboard does not contains image data"
}

Do you know how to save image from clipboard using powershell?

 

[quote quote=206823]Press ‘PrintScreen’ button before sending email. This will ask for credentials (no need to hardcode username/password), save clipboard image to current directory then attach it.

PowerShell
28 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Function Send-MyMail ([string]$Email){
# Prompt for username/password
$cred = Get-Credential
# Get and save image from clip
$image = Get-Clipboard -Format Image
$image.Save('.\image.png')
# Set email properties
$prop = @{
From = 'mail'
To = $Email
Subject = 'subject'
Body = 'body'
Attachments = '.\image.png'
SmtpServer = 'smtp.outlook.com'
Port = '587'
UseSsl = $true
Credential = $cred
}
# Add parameters to cmdlet
Send-MailMessage @prop
}
# Test Send-MyMail Function
Send-MyMail -Email addressto@mail.mil
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] Thanks you, that was exactly what I was looking for. But as you can see from my previous post, I have problems with the Get-Clipboard part.

I tried doing that command alone by writing:

$image = Get-Clipboard -Format Image; $image.Save('C:\Users\chris\Pictures\Screenshots\tesst.png')

but I always get a error on the $image.save part. Do you know what the problem is?

EDIT:

You can ignore what I said, I got it working and your script works flawlessly. Now I will try make the script force remove the .png after sending the email and it will be perfect. Thanks again.

May want to re-think something that is automatically sending something you can’t see. You are not validating what is in clipboard is actually an screenshot or what you expect. Don’t know about you, but I’ve had times where the clipboard gets wonky and doesn’t work as expected. You think it’s emailing a screen and you just sent someones performance review or other sensitive information because you aren’t validating before sending. IMHO, super risky and could have a security impact. Just because it can be automated doesn’t always mean it should.

[quote quote=206874]May want to re-think something that is automatically sending something you can’t see. You are not validating what is in clipboard is actually an screenshot or what you expect. Don’t know about you, but I’ve had times where the clipboard gets wonky and doesn’t work as expected. You think it’s emailing a screen and you just sent someones performance review or other sensitive information because you aren’t validating before sending. IMHO, super risky and could have a security impact. Just because it can be automated doesn’t always mean it should.

[/quote]
I understand your concern but the script will only be run on my personal computers, with no sensitive information on them. And really the main goal here was to educate myself and to learn if it was possible to send images on clipboard via powershell, so I can apply this to other scripts I am working on.