0

I write mail.py(use webpy) to send me the ip address of each machine.

#!/usr/bin/env python  
#coding=utf-8

import web  
def send_mail(send_to, subject, body, cc=None, bcc=None):  
    try:  
        web.config.smtp_server = 'xxxxx'   
        web.config.smtp_port = 25    
        web.config.smtp_username = 'xxx'  
        web.config.smtp_password = 'xxx'   
        web.config.smtp_starttls = True  
        send_from = 'xxx'    

        web.sendmail(send_from, send_to, subject, body, cc=cc, bcc=bcc)  
        return 1  #pass  
    except Exception, e:  
        print e  
        return -1 #fail  
if __name__=='__main__':  
    print "in mail.py"
    f=file('/home/spark/Desktop/ip.log') 
    f1=f.read()
    f.close()
    send_to = ['xxxx']          
    subject = 'xxxx'  
    body = 'ip:',f1 
    send_mail(send_to, subject, body) 

rc.local

bash deploy.sh &
exit 0

deploy.sh

#!/usr/bin/env
cd /home/spark/Desktop
python mail.py  >>deploy.log
echo "-----------------------------------------------------------"

I can receive email if I do 'python mail.py'.But when I put it in rc.local, I can not receive the email, the message in deploy.log outputs [Errno -2 ] Name or service not known.

I am puzzled with this output.

makeapp
  • 181
  • 3
  • 11
  • I changed web.config.smtp_server with the ip address instead of the domain_name and it worked. I think the error is because some configurations and services are not available during the rc.local process. Helpful links are [sendmail](https://wiki.list.org/DOC/Mail%20delivery%20fails%20with%20%22(-2,%20'Name%20or%20service%20not%20known')%22,%20aka%20I%20am%20not%20receiving%20any%20notification%20messages%20from%20Mail) [unknown service](http://stackoverflow.com/questions/23777121/why-am-i-getting-socket-gaierror-errno-2-from-python-httplib) – makeapp Aug 08 '16 at 15:41
  • [tricks to debug rc.local](http://stackoverflow.com/questions/7783341/run-script-with-rc-local-script-works-but-not-at-boot) – makeapp Aug 08 '16 at 15:42

1 Answers1

1

This might happen because PATH is different when rc.local runs. Specifically, web.sendmail might expect to find sendmail in the path, but it's not there yet. See docs here.

The paths might be specific to your system. To debug this you could dump things from inside rc.local to a file such as /tmp/rc.local.log and inspect it when the system is up: e.g., env >>/tmp/rc.local.log.

Note, if you have multiple drives being mounted during startup, the drive containing sendmail might not be mounted yet at that point. This is a pain to deal with. To double check, add mount >>/tmp/rc.local.log.

Matei David
  • 2,322
  • 3
  • 23
  • 36