0

I would like to create a batch file to send mails with the local mail client. The script should prompt the user for the receiver and the amount, which is used as an url parameter later on. Currently I have this script

@echo off
set /p "to=To: "
set /p "amount=Amount: "
start mailto:%to%^
?subject=Test^
&body="this is a longer text with 
file line breaks, text linebreaks\n
and some urls with parameters like https://www.foo.bar/%amount%"
pause

and I'm struggling with the body string. I would like to

  • add line breaks within the file to increase the readability
  • add line breaks for the string to format the mail text
  • remove the double quotes from the text

How would I do that? Any help will be appreciated.

  • Why not use powershell instead? – Gerhard Jan 14 '20 at 13:23
  • sorry, I'm new to this, what do you mean? –  Jan 14 '20 at 13:23
  • Powershell is a built in windows scripting language, pretty much replacing batch files and for certain things, like this one, is better to use than batch file. – Gerhard Jan 14 '20 at 13:25
  • have a look at the answer I posted [here](https://stackoverflow.com/questions/58373405/how-to-send-email-to-distribution-list-in-outlook-using-task-scheduler/58373828#58373828) for instance. – Gerhard Jan 14 '20 at 13:26
  • oh, thank you very much for your example. But I have to do it with batch :S –  Jan 14 '20 at 13:27
  • 1
    ok, I understand :) will see if I have time now to post an answer for you. – Gerhard Jan 14 '20 at 13:28
  • and btw. is there a command for sending it directly instead of opening the mail client window? –  Jan 14 '20 at 13:29
  • Yes there is, powershell's send-mail function :). you can add it as a line in your batch file. – Gerhard Jan 14 '20 at 13:50
  • taking your post on how to send an email using the send-mail command, I don't know the SMTP name .. and I think I don't need to provide the "from" field because I would like to use the current signed in Outlook user –  Jan 14 '20 at 13:58
  • Ok, well I can just help you on the newline and removing double quote portion, I do not have too much time to investigate the mail options right now :) – Gerhard Jan 14 '20 at 14:00

2 Answers2

1

Specifically looking at your direct requirements only.

@echo off
set nl=^


rem Above two empty lines must not be deleted as they are critical for this to work.

set /p "string=Enter String to send (use !nl! for newline)
setlocal enabledelayedexpansion
echo %string:"=%

The above example is to simply show how to add line breaks to a string. So in the given example if you run this script and at the prompt type something like:

"Hello World!nl!I am new here.!nl!Cheers"

It will simply respond as:

Hello Wold
I am new here.
Cheers

So instead of echo in my example, in your example, you would use the variable %string:"=% as the body text. `%string% is predefined, so simply do:

&body="%string%"

Additionally, you can replace the following four lines:

echo To:
set /p to=
echo Amount:
set /p amount=

with these two lines:

set /p "to=To: "
set /p "amount=Amount: "
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • thanks for your help. I currently have https://hatebin.com/hjqajhmgaq When running it the mail body only prints `The` .. what am I missing? –  Jan 14 '20 at 14:45
  • because your variable's value is not wrapped in double quotes. problem is, it is not going to create the text in newlines as you think it will. using caret is for readable format in window only, not the output. – Gerhard Jan 14 '20 at 16:19
1

Based upon the content of your linked script, I would ask that you give this untested idea a try.

@Echo Off
Set /P "to=To: "
Set /P "amount=Amount: "
Set content=The mail receiver is ^
%to%%%0D%%0A^
and the URL is ^
https://www.google.de/%amount%
Start "" ^"mailto:%to%?^
subject=Test^
&body=%content%^"

The carriage return, (0x0A or \n), character and line feed, (0x0D or \r), are added where required using %%0D%%0A, which will translate to %0D%0A in the command line.

Compo
  • 36,585
  • 5
  • 27
  • 39