Search code examples
powershellemailsmtpsmtpclientfilezilla

PowerShell Script for Email Notification


I am using a FileZilla Server as an FTP. I want to create a PowerShell script that will send an email as soon as a file is uploaded.

I have been using the knowledge in this article: https://richjenks.com/filezilla-email-notifications/

and below is the code of my fn.ps1 file

$EmailFrom = "SENDER_EMAIL"
$EmailTo = "RECIPIENT_EMAIL"
$Subject = "New File Uploaded to FileZilla"
$Body = "A new file has been uploaded to the FTP server on FileZilla"

$SMTPServer = "127.0.0.1"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 21)
$SMTPClient.EnableSsl = $true

$SMTPClient.Credentials = New-Object 
System.Net.NetworkCredential("GMAIL_ADDRESS", "GMAIL_PASSWORD");

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

The only problem is that no email is actually sent? I can see the PowerShell pop up briefly after a file is uploaded so it is being run upon file upload, however no email is actually sent anywhere.

Any help would be greatly appreciated.

EDIT: I am not entirely sure what is the relationship between "SENDER_EMAIL" and "GMAIL_ADDRESS", I found the above solution on another website and they are different, should these not be the same?


Solution

  • Make sure you understand that the script actually has nothing to do with your FTP server. It is just a plain PowerShell script that sends a fixed email. So everything in the script is related to your SMTP mail server, not to your FTP server. So...

    • What goes to SmtpClient constructor is the address of your SMTP mail server. You seem to be using an address of your FTP server.

      $SMTPServer = "127.0.0.1"
      $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 21)
      

      Check the example you have based this on. They use smtp.gmail.com:587.

    • Similarly, what goes to SmtpClient.Credentials are credentials to your SMTP mail server. So some real values, not placeholders like these:

      $SMTPClient.Credentials = New-Object 
      System.Net.NetworkCredential("GMAIL_ADDRESS", "GMAIL_PASSWORD");
      

      For Gmail they would be your Gmail address and password.