Sponsored Content
Top Forums Shell Programming and Scripting E-Mail from command line for UNIX and Perl?? Post 32380 by jy2728 on Friday 22nd of November 2002 10:45:41 PM
Old 11-22-2002
The code is too long won't fit on one thread here is the rest of the code:

$smtp = $server; # save $server for use outside foreach loop

my $smtpaddr = inet_aton $server;
unless ($smtpaddr) {
$error .= "$server not found\n";
next; # next server
}

my $retried = 0; # reset retries for each server
while ( ( not $connected = connect S, pack_sockaddr_in($port, $smtpaddr) )
and ( $retried < $mailcfg{'retries'} )
) {
$retried++;
$error .= "connect to $server failed ($!)\n";
print "- connect to $server failed ($!)\n" if $mailcfg{'debug'} > 1;
print "retrying in $mailcfg{'delay'} seconds...\n";
sleep $mailcfg{'delay'};
}

if ( $connected ) {
print "- connected to $server\n" if $mailcfg{'debug'} > 3;
last;
}
else {
$error .= "connect to $server failed\n";
print "- connect to $server failed, next server...\n" if $mailcfg{'debug'} > 1;
next; # next server
}
}

unless ( $connected ) {
return fail("connect to $smtp failed ($!) no (more) retries!")
};

{
local $^W = 0; # don't warn on undefined variables
# Add info to log variable
$log .= "Server: $smtp Port: $port\n"
. "From: $fromaddr\n"
. "Subject: $mail{Subject}\n"
. "To: ";
}

my($oldfh) = select(S); $| = 1; select($oldfh);

chomp($_ = <S>);
if (/^[45]/ or !$_) {
return fail("Connection error from $smtp on port $port ($_)")
}

print S "HELO $localhost\015\012";
chomp($_ = <S>);
if (/^[45]/ or !$_) {
return fail("HELO error ($_)")
}

print S "mail from: <$fromaddr>\015\012";
chomp($_ = <S>);
if (/^[45]/ or !$_) {
return fail("mail From: error ($_)")
}

foreach $to (@recipients) {
if ($debug) { print STDERR "sending to: <$to>\n"; }
print S "rcpt to: <$to>\015\012";
chomp($_ = <S>);
if (/^[45]/ or !$_) {
$log .= "!Failed: $to\n ";
return fail("Error sending to <$to> ($_)\n");
}
else {
$log .= "$to\n ";
}
}

print S "data\015\012";
chomp($_ = <S>);
if (/^[45]/ or !$_) {
return fail("Cannot send data ($_)");
}

foreach $header (keys %mail) {
$mail{$header} =~ s/\s+$//o; # kill possible trailing garbage
print S "$header: ", $mail{$header}, "\015\012";
};


print S "\015\012",
$message,
"\015\012.\015\012";

chomp($_ = <S>);
if (/^[45]/ or !$_) {
return fail("message transmission failed ($_)");
}

print S "quit\015\012";
$_ = <S>;
close S;

return 1;
}

Can anyone also tell me what is a perl module and how to use it, the site i got the code from keep saying it is a module and need to download it, can unix do the same thing without using perl? Smilie
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

command line send mail

Hi, I am trying to send mail in ubuntu. I have installed postfix. In Redhat I could just type: mail name@domain.com I get the error that the command mail is not found. Does anyone know if there is a different command line to send mail? Can I install the command? Thanks, Eric (1 Reply)
Discussion started by: ejbrever
1 Replies

2. Shell Programming and Scripting

How to mail in command line.

Hi Can any one guide me how to mail in ssh command terminal with help of mutt or sendmail command from a proxy client machine . My proxy server is using squid proxy. (19 Replies)
Discussion started by: VijayakumarN
19 Replies

3. Shell Programming and Scripting

Unix mail with blank line on variable

I am testing a ksh script for email. In the subject/content of the mail there is some dynamic variables like date and charges. However these variables occupied the entire line erase other in that particular line For e.g. there is a mail message: This mail is intent... Your total... (2 Replies)
Discussion started by: balaji.rengaraj
2 Replies

4. UNIX for Dummies Questions & Answers

Sending a simple mail from command line

Hi, How would one send an email from the command line. Just a simple email. I used mailx -s "test" address@server.domain then hit enter. Nothing happens then, I hit Ctrl + C twice, then I can start a new command again. Any help? Tips? I did read up on this, but the examples is much more... (2 Replies)
Discussion started by: McGuywer
2 Replies

5. Shell Programming and Scripting

Using Perl to utilize Mail Command

Does anyone know a way to use the mail command in perl as a one-liner? I am trying to do something like system("mail username@gmail.com -s Tester) but the only catch is to place the period "." after using the mail command in order to complete the command. I do not want to install any other 3rd... (2 Replies)
Discussion started by: twhitmarsh
2 Replies

6. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

7. Shell Programming and Scripting

Send mail from command line

Hi All; I need to send emails from command line in some of my shell scripts. I tried : mail -s "Subject" member@body.com < testfile where test file contains body of the mail This work on my mac computer at work BUT not on my mac laptop. Does anyone have any ideas why? Thanks Walforum (1 Reply)
Discussion started by: walforum
1 Replies

8. UNIX for Dummies Questions & Answers

POP3 Mail Client (Command Line)

Hi list, I need a POP3 mail client which I can use from the command line within a bash shell script to download email to a folder location. Can anyone recommend a good POP3 client which can be used from the command line in bash? Using redhat enterprise linux 5 and 6. thanks, Land (2 Replies)
Discussion started by: landossa
2 Replies

9. Shell Programming and Scripting

Unable to set end of line in mail command

Hi , Am trying to send mail using the mail command, but the mail command is working but its not sending automatically after pressing .(dot) in the command prompt it sends . How to achieve that. Also it showing the below line after pressing the dot . /home/abc1/dead.letter... Saved message in... (5 Replies)
Discussion started by: rogerben
5 Replies

10. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies
MboxParser::Mail::Convertable(3pm)			User Contributed Perl Documentation			MboxParser::Mail::Convertable(3pm)

NAME
Mail::MboxParser::Mail::Convertable - convert mail for sending etc. SYNOPSIS
use Mail::MboxParser; [...] # $msg is a Mail::MboxParser::Mail-object my $mail = $msg->make_convertable; $mail->delete_from_header('date', 'message-id'); $mail->replace_in_header('to', 'john.doe@foobar.com'); $mail->add_to_header( ['cc', 'john.does.brother@foobar.com'], where => 'BEHIND' ); $mail->send('sendmail'); DESCRIPTION
This class adds means to convert an email object into something that could be send via SMTP, NNTP or dumped to a file or filehandle. Therefore, methods are provided that change the structure of an email which includes adding and removing of header-fields, MIME-parts etc and transforming them into objects of related modules. Currently, only basic manipulation of the header and sending using Mail::Mailer is provided. More is to come soon. This class works non-destructive. You first create a Convertable-object and do any modifications on this while the Mail-object from which it was derived will not be touched. METHODS
delete_from_header(header-fields) Given a list of header-field names, these fields will be removed from the header. If you want to re-send a message, you could for instance remove the cc-field cause otherwise the message would be carbon-copied to the addresses listed in the cc-field. add_to_header(array-ref) add_to_header(array-ref, where => 'BEFORE' | 'BEHIND') add_to_header() takes a reference to a two-element list whose first element specifies the header-field to add or to add to while the second elements specifies the data that should be added. 'where' specifies whether to add at the beginning or at the end of the header. Defaults to 'BEHIND' if not given. replace_in_header(header-field, new_data) First element must be the header-field to be replaced while the second argument must be a string indicating what will be the new content of the header-field. send(command, args) Literally inherited from Mail::Internet. Commands can be "mail" (using the UNIX-mail program), "sendmail" (using a configured sendmail or compatible MTA like exim), "smtp" (for using Net::SMTP) and "test" which will only display what would be sent using /bin/echo. Additional arguments will be passed on to Mail::Mailer->new() which is in fact what Mail::Internet->send() uses. For more details, see Mail::Mailer VERSION
This is version 0.55. AUTHOR AND COPYRIGHT
Tassilo von Parseval <tassilo.von.parseval@rwth-aachen.de> Copyright (c) 2001-2005 Tassilo von Parseval. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
Mail::Internet, Mail::Mailer perl v5.12.3 2005-12-08 MboxParser::Mail::Convertable(3pm)
All times are GMT -4. The time now is 05:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy