Solved

Batch File output formatting issue with SendEmail

Posted on 2011-03-16
14
375 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now