Sunday, September 24, 2017

PowerShell: Invoke-WebRequest Android Central RSS Feed Emailer

RSS Feed to emailer for Android Central:

I configured the images to have the text wrap around them in order to save space.  You can remove the align=left and other img tag options to suit your needs.


[xml]$ac = iwr http://feeds2.feedburner.com/androidcentral
$articles = $ac.getelementsbytagname('item')

foreach ($a in $articles){
$title = $a.title
$link = $a.link
$html = New-Object -ComObject "HTMLFile"
$encoded =  ($a.encoded.'#cdata-section')
$html.IHTMLDocument2_write($encoded)
$text = $html.body.innerhtml
if ($text -ne $nul){$text = $text.replace("<IMG","<img width=300 align=left hspace=10 vspace=10 ")}
if ($text -eq $nul){
$text = $a.encoded
$text = $text -replace ('<style>+','~')
$text = $text.split('~')[0]
$text = $text.replace("<IMG","<img width=300 align=left hspace=10 vspace=10 ")
}
$a.pubdate = ("<table width=100%><tr><td><h3>$title</h3></td></tr><tr><td style='font-family: Arial, sans-serif;font-size:13;'>" + $text + "</td></tr></table><hr style='border: 2px solid mintcream;'>")
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($html)|out-null
}

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "myemail@gmail.com"
$Password = "mypassword"
$to = "recipient@somemail.com"
$subject = ("Android Central - " + (get-date).tolongdatestring())
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $articles.pubdate
$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)