none
Script to pick file from folder and email to associate employee RRS feed

  • Question

  • Hello Experts,

    Scenario:

    (OS - Windows 7 and Windows 10)

    We receives mobile bills of 50 employees in PDF individual files from telecom company monthly, each bill's file name saved as user mobile number. Each month I email (via MS outlook) 50 bills to 50 employees one by one manually

    Is there anyway I could create a script that will pick file from mobile bills folder and automatically email it to associate email address ?

    I will appreciate any possible support!

    Thanks & regards,


    Ali

    Tuesday, January 30, 2018 7:44 AM

Answers

  • Try this.

    #Get data from CSV file with columns Number, Mail
    $Users = Import-Csv -Path "c:\Temp\users.csv" -Delimiter ";"
    
    #Define where are your billings and get them
    $Billings_path = "C:\Temp\Billings"
    $Billings = Get-ChildItem -Path $Billings_path -Filter *.pdf -Name
    
    #Mail server and mail body info
    $mail_sender = "yourmailSenderAccount@domain.com"
    $smtp_server = "MyMailServer.domain.com"
    $date = Get-Date -Month
    $subject = "Your bill for month $date"
    $mail_body = "This is your bill. Pay IT ;)" 
    
    #For each bill find proper user email associated with phone number
    foreach ($Billing in $Billings){
        $user_mail = ($Users | Where-Object {$_.Number -eq ($Billing.Split("."))[0]}).Mail
            
            #If email found send billing else alert no email for billing
            if([bool]$user_mail.count){
            Write-Host "Send mail to $user_mail" -ForegroundColor Green
    
            $attachment = Join-Path $Billings_path $Billing
            Send-MailMessage -From $mail_sender -To $user_mail -Subject $subject -SmtpServer $smtp_server -Attachments $attachment -Body $mail_body
            }
            else
            {
                Write-Host "There is no user or email to bill" -ForegroundColor Red
            }
        
        }
    


    Best Regards,
    Łukasz Antoniak


    Please do take a moment to "Vote as Helpful" and/or "Mark as Answer", wherever applicable. Thanks!

    Tuesday, January 30, 2018 9:47 AM

All replies

  • Please carefully review the following to set your expectation of technical forums.

    This Forum is for Scripting Question Rather than script requests

    Script Gallery.

    Learn PowerShell  

    Script requests


    \_(ツ)_/

    Tuesday, January 30, 2018 7:56 AM
  • Try this.

    #Get data from CSV file with columns Number, Mail
    $Users = Import-Csv -Path "c:\Temp\users.csv" -Delimiter ";"
    
    #Define where are your billings and get them
    $Billings_path = "C:\Temp\Billings"
    $Billings = Get-ChildItem -Path $Billings_path -Filter *.pdf -Name
    
    #Mail server and mail body info
    $mail_sender = "yourmailSenderAccount@domain.com"
    $smtp_server = "MyMailServer.domain.com"
    $date = Get-Date -Month
    $subject = "Your bill for month $date"
    $mail_body = "This is your bill. Pay IT ;)" 
    
    #For each bill find proper user email associated with phone number
    foreach ($Billing in $Billings){
        $user_mail = ($Users | Where-Object {$_.Number -eq ($Billing.Split("."))[0]}).Mail
            
            #If email found send billing else alert no email for billing
            if([bool]$user_mail.count){
            Write-Host "Send mail to $user_mail" -ForegroundColor Green
    
            $attachment = Join-Path $Billings_path $Billing
            Send-MailMessage -From $mail_sender -To $user_mail -Subject $subject -SmtpServer $smtp_server -Attachments $attachment -Body $mail_body
            }
            else
            {
                Write-Host "There is no user or email to bill" -ForegroundColor Red
            }
        
        }
    


    Best Regards,
    Łukasz Antoniak


    Please do take a moment to "Vote as Helpful" and/or "Mark as Answer", wherever applicable. Thanks!

    Tuesday, January 30, 2018 9:47 AM