Send file(s) by email on a regular basis via batch file

11,000

XP Pro and higher for the date routine using WMIC:

In this line set fileattach="d:\myfolder\%datestamp%.txt" change the folder, and the datestamp is set to dd_mm-yyyy format so the filename should work for you as it is.

The text file will be attached but not zipped up.

Alter the server settings and passwords to suit you in the block of set statements, and test it.

You can schedule the batch file on a daily cycle, if that suits you.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%_%MM%-%YYYY%"
:: email.bat :::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal

:: use these settings to send from a gmail account
:: set port=465 and set SSL=True

:: use these settings for standard email SMTP port and no encryption
:: set port=25 and set SSL=False

:: Change these following items to use the same variables all the time
:: or use the command line to pass all the variables

set Port=25
set SSL=False
set From="[email protected]"
set To="[email protected]"
set Subject="Subject line"
set Body="Email Body in one line"
set SMTPServer="mailservername.myemailserver.com"
set User="username"
set Pass="password"
set fileattach="d:\myfolder\%datestamp%.txt"


:: This section sets the command line arguments
:: use this format:  CALL email.bat "[email protected]" "[email protected]" "Subject line" "Email Body in one line" "smtp.gmail.com"  "[email protected]" "password" "d:\folder\filename to attach.txt"


if "%~7" NEQ "" (
set From="%~1"
set To="%~2"
set Subject="%~3"
set Body="%~4"
set SMTPServer="%~5"
set User="%~6"
set Pass="%~7"
set fileattach="%~8"
)

set "vbsfile=%temp%\email-bat.vbs"
del "%vbsfile%" 2>nul
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
echo >>"%vbsfile%" Set objArgs       = WScript.Arguments
echo >>"%vbsfile%" Set objEmail      = CreateObject("CDO.Message")
echo >>"%vbsfile%" objEmail.From     = %From%
echo >>"%vbsfile%" objEmail.To       = %To%
echo >>"%vbsfile%" objEmail.Subject  = %Subject%
echo >>"%vbsfile%" objEmail.Textbody = %body%
if exist %fileattach% echo >>"%vbsfile%" objEmail.AddAttachment %fileattach%
echo >>"%vbsfile%" with objEmail.Configuration.Fields
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusing")        = 2 ' not local, smtp
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserver")       = %SMTPServer%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserverport")   = %port%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusername")     = %user%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendpassword")     = %pass%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpusessl")       = %SSL%
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpconnectiontimeout") = 30
echo >>"%vbsfile%"  .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send

cscript.exe /nologo "%vbsfile%"
echo email sent (if variables were correct)
del "%vbsfile%" 2>nul
goto :EOF
Share:
11,000

Related videos on Youtube

user3364639
Author by

user3364639

Updated on May 31, 2022

Comments

  • user3364639
    user3364639 almost 2 years

    I have a beginner's problem. I have a remote computer which collects data (amateur metheorology stuff). Data is stored on a daily basis in a text file. Each file is named after its date: 28_02-2014.txt, 01_03_2014.txt etc.

    Can anyone help me create a batch file which would somehow send such a text file over email automatically. Even an option of creating a zip file from all the txt files will be OK. I can delete the folder once a month, so even sending a zip file with 31 text files inside won't affect bandwidth (probably 300kb per month).

    My problems so far:

    Windows 7

    1. I don't know how to create a batch file which would ZIP all the files in one folder.
    2. I don't know how to send an automatic e-mail. The task scheduler doesn't allow to input server authentication (user/pass). I can only type the SMTP server name and that's it.
    3. Even if I manage to send e-mail somehow through the task manager, I would have to create an action for every day, which doesn't seem ideal.

    Please, could anyone help me with this? Whichever solution is simpler: either sending the newest file from a specific folder or zipping the whole folder into a file and sending it.

    • Matt Williamson
      Matt Williamson about 10 years
      How are you going to send the email? Do you have an SMTP server to use or possibly a gmail account? Either will work. As far as the zipping goes, can you install a program like 7-zip command line? Also, the structure of file names aren't the same. You have all underscores in one and underscore and dash in the other. Is this the case or a type o?
  • user3364639
    user3364639 about 10 years
    Thank you very much for the answer. I still have a problem. I have changed line 6 from 'set "datestamp=%DD%_%MM%-%YYYY%"' to 'set "datestamp=%MM%_%DD%_%YYYY%". Because I've realized that my files have such a structure. Further down I have set up all my server details and provided the correct path to the 'set fileattach' parameter. I didn't change anything further and my mail are arriving but without the attachment. Did I do something wrong?
  • foxidrive
    foxidrive about 10 years
    I need to see the line with set fileattach= in it to figure it out. Either you've made a typo or the file doesn't exist in the format that is in that line.
  • user3364639
    user3364639 about 10 years
    Here is the following line: set fileattach="C:\Users\Michał\AppData\Roaming\Meteo\logged_fil‌​es\%datestamp%.txt"
  • user3364639
    user3364639 about 10 years
    I have tried with the following line: set fileattach="C:\photo.jpg" and it attaches to the email just fine. So either there is something wrong with the path or maybe the 'datestamp' function is not working properly and does not recognize the files in the folder. Or maybe my computer name has a non latin character (Michał) and it should be represented differently in the bat file?
  • foxidrive
    foxidrive about 10 years
    The non-latin character is probably the issue. It may need a different code page to handle that character. After the line that starts setlocal then put on the next line chcp 1252 and then see if that sends the files. If not then change the 1252 to 850 and try it. I don't have experience with other code pages.