Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sendmail faq

Status
Not open for further replies.

ksbrace

Programmer
May 13, 2000
501
US
Hello,
I'm attempting to use faq for sending mail using sendmail:

I am getting a "broken pipe" error. Maybe another set of eyes will catch my error. Thanks in advance for any and all help.
Code:
#!/usr/bin/perl

# Open a stream, piped to your sendmail program.  You
# should send the process to the background and send all
# output to /dev/null so that your program does not hang
# if the mail queue takes awhile

$title="LOC Retrieval";
$subject="LOC Retrieval";
$email_addr="kelly.brax@.suny.edu";
$return_name="LOC";
$return_addr="val@suny.edu";
open (MAIL, "| /usr/sbin/sendmail -t >& /dev/null");

# Print the To: field to your mail stream.  You can either
# enter the recipient email by itself, or in the standard
# format of Name <address>

print MAIL qq~To: "$title" <$email_addr>\n~;

# If you want to send to a list, send the mail only once,
# but make each address a blind carbon copy.

#print MAIL qq~Bcc: ~;
#my $address = pop(@LIST);
#print MAIL qq~$address~;
#foreach $address (@LIST) {print MAIL qq~, $address~;}

# Include the remaining header info

print MAIL qq~\nFrom: "$return_name" <$return_addr>\n~;
print MAIL qq~Reply-To: $return_addr\n~;
print MAIL qq~Subject: $subject\n~;

# To send HTML formatted emails, your Content-Type needs 
# to be text/html.  Otherwise it is text/plain.  This being
# the last line before the body, there are two newlines at
# the end of this line.

print MAIL qq~Content-Type: text/plain\n\n~;

# Then, print the body of your email to the mail stream
$body_text="test";
print MAIL qq~$body_text\n~;

# And close with a period on a line of its own.  This is
# not necessary, since the mail will send when you close
# the stream, but it is the correct sendmail format.

print MAIL qq~.~;

# Close your mail stream, and your email will be sent to
# the sendmail queue to be sent out.  Sendmail will figure
# out the best way of doing this for you.  If you have a
# large list, it may send a few emails every few minutes.

close(MAIL);
 
try this method...
Code:
my $sendmail = "/usr/sbin/sendmail -t";
my $reply_to = "Reply-to: foo\@bar.org";
my $subject  = "Subject: Confirmation of your submission";
my $content  = "Thanks for your submission.";
my $to       = "recipient\@domain.com";

open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL $reply_to;
print SENDMAIL $subject;
print SENDMAIL $to;
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $content;
close(SENDMAIL);

I use Net::SMTP; personally and wrote this routine, not sure if it will work for *nix as I use AS Perl.
Code:
##############################
###### SENDMAIL ROUTINE ######
##############################

sub send_mail {

#############################
# Use Net::SMTP For mailing #
#############################

use Net::SMTP;

# you need to set smtp_domain to the correct ip/url of smtp server (I use local)

my SMTP_DOMAIN = "127.0.0.1";

#_[0] = To
#_[1] = From
#_[2] = Subject
#_[3] = Body Text
#_[4] = Bcc

my (@Bcc, @Eadds, @recip);

# Create new instance of SMTP
my $smtp = Net::SMTP->new(SMTP_DOMAIN, Debug => 0, Hello => SMTP_DOMAIN,) || die "Can't open mail connection: $!";

# convert recipients to array from CSV
@recip = split(/\,/, $_[0]);

# add addressess together
push @Eadds, @recip;

# Check for Bcc
if($_[4]){
    @Bcc = split(/\,/, $_[4]);
    push @Eadds, @Bcc;
}

# Send Mail
$smtp->mail($_[1]);
$smtp->recipient(@Eadds);
$smtp->data();
$smtp->datasend("To: @recip\n");
$smtp->datasend("From: $_[1]\n");

if($_[4]){
    $smtp->datasend("Bcc: @Bcc\n");
}

$smtp->datasend("Subject: $_[2]\n");
$smtp->datasend("Content-type: text/html\n\n");
$smtp->datasend("$_[3]\n");
$smtp->dataend();
$smtp->quit();

}

you then call the routine simply
Code:
my $body = "This is email body<br>I can write any html i want here.<br><br>regards,<br>me!";

&send_mail("recipient\@mydomain.com","sender\@mydomain.com","This is the email subject",$body);

hope this helps

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top