Book Creator
Add this page to your book
Add this page to your book
Book Creator
Remove this page from your book
Remove this page from your book
This is an old revision of the document!
Python
Snippet list
Edit file
The following functions can be used to edit any provided file. In the selected file a string can be searched that can then be replaced with a given string.
Edit a file with python
# search file for string def searchLineWithString(self, string, file): for num, line in enumerate(file, 1): if string in line: return num - 1 # edit line dependend on provided parameter def editLine(self, file, searchString, newLine): selectedFile = open(file, "r") readSelectedFile = selectedFile.readlines() selectedFile.close() # find specific line lineToEdit = searchLineWithString(self, searchString, readSelectedFile) # edit required line readSelectedFile[lineToEdit] = newLine # write lines to files selectedFile = open(file, "w") selectedFile.writelines(readSelectedFile) selectedFile.close()
Send mail
The following functions can be used to emails from python.
Send mails with python
EMAILRECEIVER = "" EMAILSENDER = "" MAILSERVER = "" MAILSERVERPORT = "" EMAILSENDER = "" def buildMail(self): mailText = "" # build your mail here string by string (mailText += "") return mailText def sendMail(self, hosts, awakeList, failedList, statusUnknownList): if EMAILRECEIVER and EMAILSENDER and MAILSERVER and MAILSERVERPORT: import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header smtp = smtplib.SMTP() smtp.connect(MAILSERVER, MAILSERVERPORT) # define subject here subject = "" msgRoot = MIMEMultipart("alternative") msgRoot['Subject'] = Header(subject, "utf-8") msgRoot['From'] = "wolserver <" + EMAILSENDER + ">" msgRoot['To'] = EMAILRECEIVER mailContent = MIMEText(wakeup.buildMail(self)) mailText = MIMEText(mailContent, "plain", "utf-8") msgRoot.attach(mailText) try: smtp.sendmail(EMAILSENDER, EMAILRECEIVER, msgRoot.as_string()) except: print(self, "Failed to send mail to " + EMAILRECEIVER + " | Check your settings!") print(traceback.format_exc()) else: print(self, "Mailing disabled or not configured properly.")
Send mails with python using AUTH
# MAILCONFIG MAILSERVER = "" MAILSERVERPORT = 587 MAILUSER = "" MAILPASSWORD = "" EMAILRECEIVER = "" EMAILSENDER = "" def buildMail(self): mailText = "" # build your mail here string by string (mailText += "") return mailText def sendMail(self): if EMAILRECEIVER and MAILUSER and MAILSERVER and MAILSERVERPORT: import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header smtp = smtplib.SMTP(MAILSERVER) smtp.connect(MAILSERVER, MAILSERVERPORT) smtp.ehlo() smtp.starttls() smtp.ehlo() smtp.login(MAILUSER, MAILPASSWORD) # define subject here subject = "" msgRoot = MIMEMultipart("alternative") msgRoot['Subject'] = Header(subject, "utf-8") msgRoot['From'] = EMAILSENDER msgRoot['To'] = EMAILRECEIVER mailContent = wakeup.buildMail(self) mailText = MIMEText(mailContent, "plain", "utf-8") msgRoot.attach(mailText) try: smtp.sendmail(MAILUSER, EMAILRECEIVER, msgRoot.as_string()) except: logging.writeError(self, "Failed to send mail to " + EMAILRECEIVER + " | Check your settings!") logging.writeExecError(self, traceback.format_exc()) else: logging.write(self, "Mailing disabled or not configured properly.")
Sourced from: stackoverflow.com - Python Email