Send email with PDF

# This script sends a pdf via email
from System import IO, Net, DateTime
from System.Net import Mail, Mime
from System.Text import Encoding

#1. Configure email
#gmail settings    : smtp.gmail.com,  port:587 encription=True credentials=True)
#corporate settings: smtp.contoso.com,port:25  encription=False credentials=False)
SMTPClient = "smtp.gmail.com"
SMTPPort = 587
useEncription = True
useCredentials = True

fromEmail = "your@gmail.com"
toEmail ="someone@acompany.com"
fromEmailUsr = fromEmail
fromEmailPwd = "y0urP4$$w0rd!"
filename = "test.pdf"
myMailSubject = "PDF email from a spotfire geek"
myMailBody = "Attached find pdf"
PDFattachment= "C://temp//test.pdf"

#2. Prepare email
MyMailMessage = Mail.MailMessage()
MyMailMessage.From = Mail.MailAddress(fromEmail)
MyMailMessage.To.Add(toEmail)
MyMailMessage.Subject = myMailSubject
MyMailMessage.Body = myMailBody
ct = Mime.ContentType(Mime.MediaTypeNames.Application.Pdf)

#3. Attach PDF file
attach = Mail.Attachment(PDFattachment, ct) 
attach.ContentDisposition.FileName = filename 
MyMailMessage.Attachments.Add(attach)
Mail.Attachment(PDFattachment, ct)

#4. Send email
emailSender = Mail.SmtpClient(SMTPClient)
emailSender.Port = SMTPPort
if useCredentials: emailSender.Credentials = Net.NetworkCredential(fromEmailUsr, fromEmailPwd)
emailSender.EnableSsl = useEncription
emailSender.Send(MyMailMessage)

print "Email sent!"

No comments: