[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Batch File output formatting issue with SendEmail

Posted on 2011-03-16
14
Medium Priority
?
394 Views
Last Modified: 2012-05-11
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
Comment
Question by:DrPepperGuy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 35150532
Hi. I cannot reproduce your problem.  Please post the exact inputs that cause the bug to show.  Thanks.
0
 

Author Comment

by:DrPepperGuy
ID: 35150582
Screen shot
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35150699
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:DrPepperGuy
ID: 35150849
It works when you just type it in. But if you ask for input it does not.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35151207
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
 

Author Comment

by:DrPepperGuy
ID: 35152327
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
 
LVL 16

Expert Comment

by:sjklein42
ID: 35152395
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
 

Author Comment

by:DrPepperGuy
ID: 35153693
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
 
LVL 16

Expert Comment

by:sjklein42
ID: 35153847
Try adding these tags around the rest of your message output:

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

Open in new window

0
 

Author Comment

by:DrPepperGuy
ID: 35154835
I still get the same error.
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 2000 total points
ID: 35155048

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
 

Author Comment

by:DrPepperGuy
ID: 35158010
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
 

Author Closing Comment

by:DrPepperGuy
ID: 35158034
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
 
LVL 16

Expert Comment

by:sjklein42
ID: 35158063
Thanks.  It does seem odd that sendemail.exe doesn't really accept the switches it says it does.
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question