The email send works in perl but on in shell. Is my shell script right ?
If Perl script is works looks like the server SMTP conf is right.How can i find out what is causing the email not to work in shell script ?
My perl script:
#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SMTP;
sub sendmail();
my $email = 'abc#xyz.com';
my $smtp_server = '10.233.1.199';
sub sendmail() {
my $s = Net::SMTP->new($smtp_server);
$s->mail($email);
$s->to($email);
$s->data();
$s->datasend("Subject: Test");
$s->datasend("\n");
$s->datasend("Testing\n");
$s->dataend();
$s->quit;
}
sendmail();
Shell script:
#!/bin/sh
SUBJECT="some subject"
smtp=10.233.1.199
EMAIL=abc#xyz.com
echo "some message" | mailx -s "$SUBJECT" "$EMAIL"
You have to export the smtp variable, otherwise mailx will not see it. Like this:
#!/bin/sh
SUBJECT="some subject"
export smtp=10.233.1.199
EMAIL=abc#xyz.com
echo "some message" | mailx -s "$SUBJECT" "$EMAIL"
Btw the smtp variable is not mentioned in man mailx in my system nor in another Linux system I have access to. But I found it mentioned in an answer on superuser.com. That is to say, I'm not sure this would work on systems where it is not mentioned in man mailx, but if it works for you that's great.
Related
I am attempting to add the x-header X-APP-VOLT: Yes to the header of my email with a .tar attachment. I only have access to usr/sbin/sendmail and mailx. I do not have root access so I can't download other versions of mailx or mutt.
I can add the x-header to usr/sbin/sendmail using the below code, but I can't figure out how to add the .tar attachment.
/usr/sbin/sendmail -i -- toemail << END
To: toemail
Subject: Test
X-APP-VOLT: Yes
Hope this works! END
I can attach a .tar file to mailx using the the below code, but I can't figure out how to add a x-header. My mailx also does not have the -a option.
cat file | uuencode filename | mailx -s "Test" toemail
Thank you
One way is to construct your input in a temporary file:
cat > tmpfile$$ << END
To: toemail
Subject: Test
X-APP-VOLT: Yes
Hope this works!
END
uuencode filename < file >> tmpfile$$
/usr/sbin/sendmail -i -- toemail < tmpfile$$
Also, I usually use sendmail's -t flag in this case, rather than repeating the recipient:
/usr/sbin/sendmail -i -t < tmpfile$$
If you don't want to use a temporary file, if you want to use a pure pipeline, you can use ( ) to create a subshell to do the construction:
(
echo "To: toemail"
echo "Subject: Test"
echo "X-APP-VOLT: Yes"
echo
echo "Hope this works!"
echo
uuencode filename < file
) | /usr/sbin/sendmail -i -t
(Of course, these days most recipients will probably find it easier to deal with MIME attachments rather than uuencode. It's pretty straightforward to create MIME attachments with a shell script, also.)
I am writing a perl script to login in to a server with ssh and do some shell commands on the server. The problem is that the server is only accessible by first logging into another server.
(I am using password-less login with ssh keys).
The following bash script is working correctly, and illustrates the problem:
#! /bin/bash
server1="login.uib.no"
server2="cipr-cluster01"
ssh "$server1" "ssh $server2 \"echo \\\"\\\$HOSTNAME\\\"\""
It prints the correct host name to my screen: cipr-cluster01. However, when trying to do same thing in Perl:
my $server1="login.uib.no";
my $server2="cipr-cluster01";
print qx/ssh "$server1" "ssh $server2 \"echo \\\"\\\$HOSTNAME\\\"\""/;
I get the following output: login.uib.no. So I guess, there is some problems with the quoting for the perl script..
qx works like double quotes. You have to backslash some more:
print qx/ssh "$server1" "ssh $server2 \"echo \\\\"\\\$HOSTNAME\\\\"\""/;
Using single quotes might simplify the command a lot:
print qx/ssh "$server1" 'ssh $server2 "echo \\\$HOSTNAME"'/;
You can simplify the quoting a bit by using the ProxyCommand option that tells ssh to connect to $server2 via $server1, rather than explicitly running ssh on $server1.
print qx/ssh -o ProxyCommand="ssh -W %h:%p $server1" "$server2" 'echo \$HOSTNAME'/;
(There is some residual output from the proxy command (Killed by signal 1) that I'm not sure how to get rid of.)
You can use Net::OpenSSH that is able to do the quoting automatically:
my $ssh_gw = Net::OpenSSH->new($gateway);
my $proxy_command = $ssh_gw->make_remote_command({tunnel => 1}, $host, 22);
my $ssh = Net::OpenSSH->new($host, proxy_command => $proxy_command);
$ssh->system('echo $HOSTNAME');
I have a script that makes of stdin. It also includes internal calls to curl, that are constructed based on the stdin data, and that require password authentication via form variables in the url.
I'd like to be able to type my password when I run the script, rather than storing it somewhere, but stdin is "taken". What's a good way to write a script like this? I'd be interested in either a good way to keep it in the filesystem or if there's some conceivable way to get it as live input without fouling up the incoming pipe.
Read it from the TTY:
if terminal=$(tty < /dev/tty || tty || tty 0<&1 || tty 0<&2) 2> /dev/null
then
echo "Enter password: " > "$terminal"
IFS= read -r password < "$terminal"
else
echo "There is no terminal to read a password from." >&2
exit 1
fi
This tries to get the terminal associated with /dev/tty, or stdin/stdout/stderr if the OS doesn't support it, and uses it to read and write directly to the user.
If it doesn't have to be portable, e.g. when using use Bash on Linux or FreeBSD, you can simplify and improve this:
if ! IFS= read -rs -p "Enter password: " password < /dev/tty 2> /dev/tty
then
echo "Password entry failed"
exit 1
fi
At risk of muddying the question, I needed to switch over to perl and thought it might be useful to some people who find themselves here if I posted a perl translation of "that other guy"'s answer. (Don't know if this is the cleanest way, but it worked for me.)
open (TTY, "+< /dev/tty" )
or eval 'sub Term::ReadLine::findConsole { ("&STDIN", "&STDERR") }';
die $# if $#;
print TTY "Enter your password: ";
use Term::ReadKey;
ReadMode('noecho', *TTY);
my $password = ReadLine(0, *TTY);
chomp $password;
ReadMode('normal', *TTY);
print TTY "\n";
close (TTY);
This particular piece of code below worked correctly on AIX
( echo "mailbody";
uuencode a.txt 'arenamed.txt';
uuencode ab.txt 'abrenamed.txt';
uuencode abc.txt 'abcrenamed.txt';
uuencode abcd.txt 'abcdrenamed.txt'; ) | mailx -s "$subject" $emailaddress;
But on Linux, any occurrence of uuencode is printing begin 644 blocks in the body of the email viewed on Outlook 2010.
begin 644 abc.txt
5:F%H<V1L:G-A"F%S9&MJ87-J9#L*
`
end
I have tried, using different variations of ( echo $body ; uuencode filename filenamechanged ) with echo first, uuencode later and vice versa but it doesn't help.
I would have used "mail -a" but I want to rename files which are emailed, so, was looking at uuencode.
Any suggestions other than using sendmail/mutt here?
This is what worked
(echo "Subject: $Mail_Subject";
echo "To:$Mail_List";
echo $Mail_Body;
uuencode $LOG_DIR/FileName1 'AttachmentDisplayName1';
uuencode $LOG_DIR/FileName2 'AttachmentDisplayName2') | sendmail -t $Mail_List
Hope this helps anyone who is looking for this kind of issue.
I'm trying to execute an Unix shell script to generate emails using mailsend. I need to format this as an HTML file using it's content type
My script as follows,
sender="xxxx#xxxx.com"
subject="My Subject"
server=000.000.000.000
message=`cat fun.txt`
email_add="test#test.com"
nohup ./mailsend -f $sender -t $email_add -u "$subject" -m "$message" -s $server
-l fcbulog
How can I achieve the HTML formatted email by using above script
You could set Content-Type to text/html and then just use the your html as body of the message.