Share via


PowerShell send mail with links

Question

Tuesday, May 8, 2012 7:27 AM

Hi there,

I have a script that is outputting to an Excel file that at the end of the mail I want to attach and email the link to where it is stored on the server.

Here is the script information;

# Mail send command
function Send-Mail{
param($smtpServer,$from,$to,$subject,$body)
$smtp = new-object system.net.mail.smtpClient($SmtpServer)
$mail = new-object System.Net.Mail.MailMessage
$mail.from = $from
$mail.to.add($to)
$mail.subject = $subject
$mail.body = $body
$mail.IsBodyHtml = $true
$smtp.send($mail)

and the output file and mail body of the mail:

# Exported file info
$exported_csv = $RootFolder + "\Results.csv"

$body += "The report is available here:" + $newline
$body += $exported_csv + $newline

What i'd like is to have the $exported_csv file appear as a hyperlink to the stored file and not plain text. Then, what command to I add to the $mail section to add the file as an attachment?
Please can you assist?

All replies (7)

Tuesday, May 8, 2012 7:49 AM âś…Answered

This will send a hyperlink in the body of the email:

$mailParams = @{body = '<a href="http://www.microsoft.com"></a>'to = '[email protected]'from = '[email protected]'subject = 'Test URL'smtpserver = 'Server1'}Send-MailMessage @mailParams -BodyAsHtml

Grant Ward, a.k.a. Bigteddy

What's new in Powershell 3.0 (Technet Wiki)


Tuesday, May 8, 2012 7:34 AM

Are you using Powershell v1 or v2?  (Type get-host to check).  If v2, use Send-Mailmessage.

Grant Ward, a.k.a. Bigteddy

What's new in Powershell 3.0 (Technet Wiki)


Tuesday, May 8, 2012 8:04 AM

Hi Bigteddy, I am using v2.

My current mail commands are:    $mail.body = $body

then, for the mail:

$body += "Dear " + $AdminName + ","+ $newline + $newline
$body += "The following users' passwords are expiring soon or have already expired. Report is available here:" + $newline
$body += **$exported_csv ** + $newline + $newline

The mail will have plain text and then the url is located in the 3rd line ($exported_csv), please can you advise how to specify that it is just that line needs to contain the hyperlink?


Tuesday, May 8, 2012 8:14 AM

You need to use the Send-Mailmessage cmdlet, which has the -BodyAsHTML switch.  You then have to compose the body in HTML.  You cannot have part of the body as plain text, and part as HTML.

Grant Ward, a.k.a. Bigteddy

What's new in Powershell 3.0 (Technet Wiki)


Wednesday, May 9, 2012 8:19 AM

Hi,

To convert test to Html, please also refer to the below link:

Creating Formatted HTML Output

http://technet.microsoft.com/en-us/library/ff730936.aspx

Regards,

Yan Li

Yan Li

TechNet Community Support


Wednesday, May 9, 2012 5:56 PM

the OP already realized that the message needs to be specified as being in HTML format.

What I do in such situations is to use word to create the boilerplate text of the message, then save in HTML format. I then delete a lot of unnecessary HTML code, i.e. stuff like:

<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">

leaving just the text of the message, in which the link appears in the form of an HTML A tag:

<p>Here is the link to <a
href="http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/f19a73e4-17a2-4040-85b6-2538c98e326d%22%3Ewhat
you need to know</a></p>

Then I either add that to my script as a here string, or read it in from a text file.

Al Dunbar


Thursday, May 10, 2012 1:38 PM

Thank you all for the help - i've got this working now..

Regards,

John