Friday, September 22, 2017

PowerShell: Invoke-WebRequest SlashDot RSS Feed Emailer

Use the following code if you want an emailer using Slashdot's RSS service:


[xml]$slashdot = iwr http://rss.slashdot.org/Slashdot/slashdotMain
$articles = $slashdot.getelementsbytagname('item')

foreach ($a in $articles){
$link = $a.link
$title = $a.title
$comments = $a.comments
$department = $a.department
$description = $a.description
if ($a.subject -eq "iphone"){$subjecticon = "<img align=left vspace=2 hspace=5 align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/iphone_64.png>"}
if ($a.subject -eq "money"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/money_64.png>"}
if ($a.subject -eq "microsoft"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/microsoft_64100.png>"}
if ($a.subject -eq "privacy"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/privacy_64.png>"}
if ($a.subject -eq "eu"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/eu_64.png>"}
if ($a.subject -eq "encryption"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/encryption_64.png>"}
if ($a.subject -eq "firefox"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/firefox_64.png>"}
if ($a.subject -eq "security"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/security_64.png>"}
if ($a.subject -eq "social"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/social_64.png>"}
if ($a.subject -eq "drm"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/drm_64.png>"}
if ($a.subject -eq "business"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/business_64.png>"}
if ($a.subject -eq "games"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/games_64.png>"}
if ($a.subject -eq "china"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/china_64.png>"}
if ($a.subject -eq "tv"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/tv_64.png>"}
if ($a.subject -eq "medicine"){$subjecticon = "<img align=left vspace=2 hspace=5 src=https://a.fsdn.com/sd/topics/medicine_64.png>"}

$html = New-Object -ComObject "HTMLFile"
$html.IHTMLDocument2_write($a.description)
$text = $html.body.innertext
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($html)|out-null
$a.about = ("<table><tr><td><h3>" + $subjecticon + $title + "</h3><b>" + $comments + "</b> comments <b><i>(" + $department + ")</b></i></td></tr></table><table><tr><td style='font-family: Arial, sans-serif;font-size:13;'>" + $text + "</td></tr></table><hr>")
}

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "myemail@gmail.com"
$Password = "mypassword"
$to = "recipient@somemail.com"
$subject = ("Slashdot - " + (get-date).tolongdatestring())
$body = $articles.about
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $articles.about
$message.to.add($to)
$message.from = $username
$message.IsBodyHTML = $true
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)

No comments: