Search code examples
pythonpython-3.xautomation

How to automate python scripts on a web apache server


I want to create an automatic script in python that will inform me with an email when a new order is placed on my website. So far, I found I can use cronjobs from the apache server every minute, but it seems to be overkill since I don't get orders every minute.

So I'm looking for a better solution.


Solution

  • Here’s the code you can call this code whenever the order is placed by the user you don’t need to automate

    If you are using django you can use this:

    from django.core.mail import send_mail
    
    send_mail(
        'Subject here',
        'Here is the message.',
        'from@example.com',
        ['to@example.com'],
        fail_silently=False,
    )
    

    Without django you can use this :

    import smtplib
    
    sender = 'from@example.com'
    receivers = ['to@example.com']
    
    message = “This is a test e-mail message."
    
    try:
       smtpObj = smtplib.SMTP('localhost')
       smtpObj.sendmail(sender, receivers, message)         
       print "Successfully sent email"
    except SMTPException:
       print "Error: unable to send email"