[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

Batch File output formatting issue with SendEmail

Hello,

I have created the following batch file. It is a simple ask for input, store the variables, and then e-mail the information. The issue I'm running into is that if the input is more than two words it will jumble the lines together. Do I need to export the variable to a text file and then read the text file back in to avoid this issue or is there another solution that I'm just not aware of? Thanks for the help.

Here's the incorrect output when the "Employee Name" section is more than just a name.


Hi Lee,
 
Please setup the following individual with a phone extension and voicemail.
 
 
 ------------------------------------------------------------------
 
Employee Name:  This is for Trevor in the warehouse office Office Location:  Tulsa Assigned Department:  IT Cisco IP Phone Model Number:  7945 MAC Address:  123456789
 
 ------------------------------------------------------------------
 
Kind regards,
 
Marcus



Here's the correct output when just two words are used for the "Employee Name".


Hi Lee,
 
Please setup the following individual with a phone extension and voicemail.
 
 
 ------------------------------------------------------------------
 
Employee Name:  Marcus Horner
Office Location:  Tulsa
Assigned Department:  IT
Cisco IP Phone Model Number:  7942
MAC Address:  123456789
 
 ------------------------------------------------------------------
 
Kind regards,
 
Marcus

@echo off
IF Exist C:\email.txt del c:\email.txt

::
::
::
:: -----------------------------------------------------------------------------------------------------------
::
::                                        New_Phone_Extension.bat
::
:: This batch file submits the person's name, department, and extension in an e-mail to Janice.
:: It was created to automate the process.
:: Original file created by Marcus Horner on 03-09-2011.
:: Last modified by Marcus Horner on 03-09-2011
::
::-------------------------------------------------------------------------------------------------------------
::
::                                             Instructions
::
:: Simply click on the file name to run.
::
:: ------------------------------------------------------------------------------------------------------------
::
:: Start of actual commands
::
:: ------------------------------------------------------------------------------------------------------------
@echo off
TITLE New Phone User Notification
COLOR 27
:: The following command sets the drive letter so we can refer to where the files are located for our commands.
FOR %%i IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF EXIST %%i:\Programs_R.here SET varDriveLetter=%%i:
:: The following is a variable that changes to the specified drive our files are stored on.
SET varRoot=cd /d %varDriveLetter%
:: The follow variable is the location where our files will be copied to that we will be using later in the script.
SET local=c:\IT_Resources\
:: The following variable sets the file name for the message to be pulled from on your local machine.
SET _msg=c:\email.txt
:: The following variable sets the e-mail address to send the message to.
SET _email=marcus.horner@email.com
::
::
::
:: Gathering information for the e-mail.
::--------------------------------------------------------------------------------------------------------------------
SET _varSaveFile=c:\IT_Resources\Logs\%username%_%PC%_Survey.txt
echo.
echo.
echo                   ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» 
echo                   º      Request for a New Phone Setup      º   
echo                   º        Created by Marcus Horner         º          
echo                   ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ  
echo.
echo.         
echo. 
echo.
set /p _name=       What is the name of the individual?  
echo.
set /p _office=     What Office are they in?  
echo.
set /p _dept=       What department are they in?  
echo.
set /p _model=      What is the model of the phone?    
echo.
set /p _mac=        What is the MAC Address?  
::--------------------------------------------------------------------------------------------------------------------
::
::
:: Output text to the text file
::--------------------------------------------------------------------------------------------------------------------
echo Hi Lee, >> %_msg%
echo. >> %_msg%
echo Please setup the following individual with a phone extension and voicemail. >> %_msg%
echo. >> %_msg%
echo. >> %_msg%
echo. ------------------------------------------------------------------ >> %_msg%
echo. >> %_msg%
echo Employee Name:  %_name% >> %_msg%
echo Office Location:  %_office% >> %_msg%
echo Assigned Department:  %_dept% >> %_msg%
echo Cisco IP Phone Model Number:  %_model% >> %_msg%
echo MAC Address:  %_mac% >> %_msg%
echo. >> %_msg%
echo. ------------------------------------------------------------------ >> %_msg%
echo. >> %_msg%
echo Kind regards, >> %_msg%
echo. >> %_msg%
echo Marcus >> %_msg%
::---------------------------------------------------------------------------------------------------------------------
::
::
:: E-mail Command 
@echo off
cls
echo.
echo.
::
::----------------------------------------------------------------------------------------------
:SENDEMAIL
cls
echo.
echo.
echo I'm now sending the e-mail to %_email%.
echo.
echo.
sendemail.exe -t "%_email%" -f "Cisco Call Manager <marcus.horner@email.com>" -u "We've added a new phone user..." -o message-file=%_msg% -s "email_server"
echo.
echo.
:: End of commands

Open in new window

0
DrPepperGuy
Asked:
DrPepperGuy
  • 7
  • 7
1 Solution
 
sjklein42Commented:
Hi. I cannot reproduce your problem.  Please post the exact inputs that cause the bug to show.  Thanks.
0
 
DrPepperGuyAuthor Commented:
Screen shot
0
 
sjklein42Commented:
Seems to work fine for me.  Here's the output email.txt:

c:\temp>type c:\email.txt
Hi Lee,

Please setup the following individual with a phone extension and voicemail.


 ------------------------------------------------------------------

Employee Name:  This is for Trevor in the shop area.
Office Location:  here
Assigned Department:  there
Cisco IP Phone Model Number:  red
MAC Address:  12-12-12-12

 ------------------------------------------------------------------

Kind regards,

Marcus

c:\temp>

Open in new window


Try modifying your bat file (for a test) to not prompt the user, but instead put the actual values into the set commands that now have /p

Then see if it still fails.

If so, post the new bat file and we can all try it.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
DrPepperGuyAuthor Commented:
It works when you just type it in. But if you ask for input it does not.
0
 
sjklein42Commented:
Very odd.

Please attach as a file (so I can dump it in binary) one of the "bad" email.txt files you've created.

I am still not able to make your script fail.
0
 
DrPepperGuyAuthor Commented:
I can't make it fail now either. Maybe it was just a glitch for a little bit. Is there any way to apply formatting to it? I'd like to bold the portions like "Employee Name:" and "Office Location:".
0
 
sjklein42Commented:
Well, at least you have the screen capture as evidence that it wasn't a hallucination.  :)

To get bold characters you would have to send the mail something other than plain text, like HTML.  A fair amount of work.
0
 
DrPepperGuyAuthor Commented:
Okay, I have managed to figure out how to submit the code into HTML. My only issue now is to send the e-mail in HTML format. I'm using "SendEmail.exe" to send the file. The photo below shows the code for using auto, text, or html but I get the following error. Any ideas on what I am missing. I'll also include the updated code. sendEmail-v1.56.png
@echo off
IF Exist C:\email.html del c:\email.html

::
::
::
:: -----------------------------------------------------------------------------------------------------------
::
::                                        New_Phone_Extension.bat
::
:: This batch file submits the person's name, department, and extension in an e-mail to Janice.
:: It was created to automate the process.
:: Original file created by Marcus Horner on 03-09-2011.
:: Last modified by Marcus Horner on 03-09-2011
::
::-------------------------------------------------------------------------------------------------------------
::
::                                             Instructions
::
:: Simply click on the file name to run.
::
:: ------------------------------------------------------------------------------------------------------------
::
:: Start of actual commands
::
:: ------------------------------------------------------------------------------------------------------------
@echo off
TITLE New Phone User Notification
COLOR 27
:: The following command sets the drive letter so we can refer to where the files are located for our commands.
FOR %%i IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF EXIST %%i:\Programs_R.here SET varDriveLetter=%%i:
:: The following is a variable that changes to the specified drive our files are stored on.
SET varRoot=cd /d %varDriveLetter%
:: The follow variable is the location where our files will be copied to that we will be using later in the script.
SET local=c:\IT_Resources\
:: The following variable sets the file name for the message to be pulled from on your local machine.
SET _msg=c:\email.html
:: The following variable sets the e-mail address to send the message to.
SET _email=marcus.horner@email.com
::
::
::
:: Gathering information for the e-mail.
::--------------------------------------------------------------------------------------------------------------------
SET _varSaveFile=c:\IT_Resources\Logs\%username%_%PC%_Survey.txt
echo.
echo.
echo                   ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» 
echo                   º      Request for a New Phone Setup      º   
echo                   º        Created by Marcus Horner         º          
echo                   ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ  
echo.
echo.         
echo. 
echo.
set /p _submitter=  What is your name?  
echo.
set /p _emailFrom=  What is your e-mail address?  
echo. 
set /p _name=       What is the name of the individual?  
echo.
set /p _office=     What Office are they in?  
echo.
set /p _dept=       What department are they in?  
echo.
set /p _model=      What is the model of the phone?    
echo.
set /p _mac=        What is the MAC Address?  
echo.
set /p _desc=       What should the phone description be?  
echo.
set /p _notes=      Are there any additional notes to add?  
::--------------------------------------------------------------------------------------------------------------------
::
::
:: Output text to the text file
::--------------------------------------------------------------------------------------------------------------------
echo ^<table width="800" border="1" align="center" cellpadding="0" cellspacing="0"^> >> %_msg%
echo ^<tr^> >> %_msg%
echo ^<td colspan="3" align="center"^>^<img src="http://www.mediafire.com/imgbnc.php/2ba93e456152f3c3f7df44f5c03af6e542fbc7b51c50e917278e3aca8c89489d5g.jpg" width="300" height="204" /^>^</td^> >> %_msg%
echo ^</tr^> >> %_msg%
echo ^<tr^> >> %_msg%
echo ^<td colspan="3" align="left"^>^<p^>^<strong^>Hello members of the HelpDesk,^</strong^>^</p^> >> %_msg%
echo ^<p^>We have a need for a new IP phone. Please see the details below. Should you have any questions, please feel free to contact ^<strong^>%_submitter%^</strong^> at ^<strong^>%_emailFrom%^</strong^>.^</p^> >> %_msg%
echo ^<p^>Kind regards,^</p^> >> %_msg%
echo ^<p^>%_submitter%^</p^>^</td^> >> %_msg%
echo ^</tr^> >> %_msg%
echo ^<tr^> >> %_msg%
echo ^<td width="158" align="right"^>^<strong^>Employee Name:^</strong^>^</td^> >> %_msg%
echo ^<td width="13"^>&nbsp;^</td^> >> %_msg%
echo ^<td width="615"^>%_Name%^</td^> >> %_msg%
echo ^</tr^> >> %_msg%
echo ^<tr^> >> %_msg%
echo ^<td align="right"^>^<strong^>Office Location:^</strong^>^</td^> >> %_msg%
echo ^<td^>&nbsp;^</td^> >> %_msg%
echo ^<td^>%_Office%^</td^> >> %_msg%
echo ^</tr^> >> %_msg%
echo ^<tr^> >> %_msg%
echo ^<td align="right"^>^<strong^>Department:^</strong^>^</td^> >> %_msg%
echo ^<td^>&nbsp;^</td^> >> %_msg%
echo ^<td^>%_dept%^</td^> >> %_msg%
echo ^</tr^> >> %_msg%
echo ^<tr^> >> %_msg%
echo ^<td align="right"^>^<strong^>Model of Phone:^</strong^>^</td^> >> %_msg%
echo ^<td^>&nbsp;^</td^> >> %_msg%
echo ^<td^>%_model%^</td^> >> %_msg%
echo ^</tr^> >> %_msg%
echo ^<tr^> >> %_msg%
echo ^<td align="right"^>^<strong^>MAC Address:^</strong^>^</td^> >> %_msg%
echo ^<td^>&nbsp;^</td^> >> %_msg%
echo ^<td^>%_mac%^</td^> >> %_msg%
echo ^</tr^> >> %_msg%
echo ^<tr^> >> %_msg%
echo ^<td align="right"^>^<strong^>Phone Description:^</strong^>^</td^> >> %_msg%
echo ^<td^>&nbsp;^</td^> >> %_msg%
echo ^<td^>%_desc%^</td^> >> %_msg%
echo ^</tr^> >> %_msg%
echo ^<tr^> >> %_msg%
echo ^<td align="right"^>^<strong^>Additional Notes:^</strong^>^</td^> >> %_msg%
echo ^<td^>&nbsp;^</td^> >> %_msg%
echo ^<td^>%_notes%^</td^> >> %_msg%
echo ^</tr^> >> %_msg%
echo ^<tr^> >> %_msg%
echo ^<td^>&nbsp;^</td^> >> %_msg%
echo ^<td^>&nbsp;^</td^> >> %_msg%
echo ^<td^>&nbsp;^</td^> >> %_msg%
echo ^</tr^> >> %_msg%
echo ^</table^> >> %_msg%



::---------------------------------------------------------------------------------------------------------------------
::
::
:: E-mail Command 
@echo off
cls
echo.
echo.
::
::----------------------------------------------------------------------------------------------
:SENDEMAIL
cls
echo.
echo.
echo I'm now sending the e-mail to %_email%.
echo.
echo.
sendemail.exe -t "%_email%" -f "Cisco Call Manager <marcus.horner@email.com>" -u "We've have a new phone user..." -o message-content-type=html -o message-file=%_msg% -s "email_server"
echo.
echo.
:: End of commands

Open in new window

mailError.png
0
 
sjklein42Commented:
Try adding these tags around the rest of your message output:

<x-html>
<html>
...
...
</html>
</x-html>

Open in new window

0
 
DrPepperGuyAuthor Commented:
I still get the same error.
0
 
sjklein42Commented:

I am not familiar witih the particular sendemail.exe program you are using.  If see nothing wrong with what you are doing, but if you leave off the message-content-type option, does it work then?  (Keep the HTML tags in the message.)

As an aside, you may need to be careful about sending HTML mail messages without a plaintext fallback unless you know that the recipient(s) have HTML email viewing enabled.  (I don't, for example.)
0
 
DrPepperGuyAuthor Commented:
So long as I attach the file it works. It just won't display in HTML in the e-mail. I can live with that I reckon. Thanks for helping me troubleshoot this.
0
 
DrPepperGuyAuthor Commented:
Turns out there wasn't really a clear solution to this issue as it stands but credit is still do as I do appreciate the help.

Thank you.
0
 
sjklein42Commented:
Thanks.  It does seem odd that sendemail.exe doesn't really accept the switches it says it does.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now