LocoTechCJ
asked on
Run Linux command in Perl script as different user
I think this is more a Linux command question, but it resides in a perl script so I'll post in both...
I am running a perl script that automates a linux server account setup and then emails the users (using the mail command) to let them know that the account is ready. Currently, to send an email I do the following:
# Open for mail output
open(MAIL, "|mail -s 'Accout is Ready' -b $blindCopyEmails $userEmail ");
# add message for mail
print MAIL $emailMessage;
# Close/Send mail
close(MAIL);
When the mail is sent out, it is sent from the root account because the automated perl script is ran by the root account, so the email shows from root@thisdomain.com, but I want it to come from another user account. If I can run the "mail" command as another user, then it will send the email from that user. I don't believe the mail command has an arguement for changing the from: field, only the subject, cc, bcc, and sendto. It is required that the actual perl script is run by the root user.
Any suggestions??
Thanks,
LTCJ
I am running a perl script that automates a linux server account setup and then emails the users (using the mail command) to let them know that the account is ready. Currently, to send an email I do the following:
# Open for mail output
open(MAIL, "|mail -s 'Accout is Ready' -b $blindCopyEmails $userEmail ");
# add message for mail
print MAIL $emailMessage;
# Close/Send mail
close(MAIL);
When the mail is sent out, it is sent from the root account because the automated perl script is ran by the root account, so the email shows from root@thisdomain.com, but I want it to come from another user account. If I can run the "mail" command as another user, then it will send the email from that user. I don't believe the mail command has an arguement for changing the from: field, only the subject, cc, bcc, and sendto. It is required that the actual perl script is run by the root user.
Any suggestions??
Thanks,
LTCJ
POSIX::setuid()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had considered using sudo, but it would only give me permission errors running the mail command.
Adam314: That's a great idea... not sure why I hadn't thought of that. That works great. Thanks.
PS: Must add a ; on the BCC line. I was scratching my head at first, wondering why it wasn't working. :)
--LTCJ
Adam314: That's a great idea... not sure why I hadn't thought of that. That works great. Thanks.
PS: Must add a ; on the BCC line. I was scratching my head at first, wondering why it wasn't working. :)
--LTCJ
use sudo command