kootenpv / yagmail

Send email in Python conveniently for gmail using yagmail
MIT License
2.66k stars 265 forks source link

help me with ping server #151

Closed hidalgo-vntu closed 4 years ago

hidalgo-vntu commented 4 years ago

How I can with you module ping server my code:

import yagmail import os import datetime import sys import time

hostname = "server" response = os.system("ping -c 1 " + hostname)

if response == 0: body = print (hostname, 'is up!') else: text = (hostname, "is down!") receiver = "login"

yag = yagmail.SMTP("login", "pass")
yag.send(
to=receiver,
subject="Server Info",
 contents=body, 
) 
old-adapdr commented 4 years ago

Hey @hidalgo-vntu I think this is what you're looking for ?

import yagmail
import os

hostname = "1.1.1.1"
receiver = "john@example.com"
subject = "Server Info"
host_up = [True if os.system("ping -c 1 " + hostname) == 0 else False]

if host_up:
    print(hostname, 'is up!')
    contents = f'{hostname} is up!'
else:
    print(hostname, 'is down!')
    contents = f'{hostname} is down!'

yag = yagmail.SMTP("john@example.com", "1234-secure-pw")
yag.send(
    to=receiver,
    subject=subject,
    contents=contents,
)
old-adapdr commented 4 years ago

@kootenpv Suggested solution is given, you can probably close this issue and re-open if @hidalgo-vntu still needs help?

kootenpv commented 4 years ago

@alexpdr Thanks a lot, looks good! Indeed, feel free to reopen.