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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using sendmail for attachments

Status
Not open for further replies.

melsterTEK

Programmer
Feb 6, 2007
27
0
0
US
Does anyone now how to use sendmail for sending an attached file? Is sendmail good for this purpose?
 
Attachments are sent like this. I use this in a script to send a text and html email. I think all you have to do is change the second content type (after the boundary) to match the type of file. You can use anything for the boundary string, just be sure its not in the attachment! But I've never used for an attached file so I could wrong. use real email addresses where I have variables in $to and $from
Some file types need binmode for the open I believe, but not sure. Anyway this piece of code is just to help a little, unless I'm totally wrong and I'll read the responses and learn too.

Code:
$to= qq|$payer_email\nMIME-Version: 1.0\nContent-Type: multipart/alternative; boundary="_jkkdsffdsr4332432dlkjifewks_"|;
$from=$customer_service;
$subject="Thank You!";
$body= qq|--_jkkdsffdsr4332432dlkjifewks_
Content-Type: text/plain; charset="iso-8859-1"
Dear you
Thank you 
--_jkkdsffdsr4332432dlkjifewks_
Content-Type: text/html; charset="iso-8859-1"
# you would insert/append file contents here instead of HTML
<html><body>
<p>Dear you<br />Thank you</p>
</body></html>
|;
    open(MAIL, "|/usr/sbin/sendmail -t");

    print MAIL "To: $to\n";
    print MAIL "From: $from\n";
    print MAIL "Subject: $subject\n\n";

    print MAIL "$body\n";

    close(MAIL);
 
Hi, I found this a lot easier to work for me.

Here's my code:
#
# using sendmail and mailing an attachment
#
$msg = MIME::Lite->new(From => 'youremailhere',
To => 'youremailhere',
Subject => 'My File',
Type => 'multipart/mixed');

$msg->attach(Type => 'TEXT',
Path => '/yourpath/name.txt',
Filename => 'name.txt',
Disposition => 'attachment');

$msg->send( ); # default is to use sendmail(1)
 
Yeah, that's a lot simpler. I'll definitely give it a try in the future
 
One thing I was wondering was how to send attachments with html + plain text email
I realized that I had some emails like that. Can Mime::Lite handle that too? If not then those need to be constructed something like what follows (from an emails source):

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="0-1572042833-1170970278=:20156"
Content-Transfer-Encoding: 8bit
Message-ID: <616287.20156.qm@web56502.mail.re3.yahoo.com>
X-Nonspam: Statistical 64%
X-Antivirus: AVG for E-mail 7.5.441 [268.17.32/677]

--0-1572042833-1170970278=:20156
Content-Type: multipart/alternative; boundary="0-1096044455-1170970278=:20156"

--0-1096044455-1170970278=:20156
Content-Type: text/plain; charset=iso-8859-1

Content-Transfer-Encoding: 8bit

Hi,
Here is some more reading for you.
Dad

--0-1096044455-1170970278=:20156
Content-Type: text/html; charset=iso-8859-1

Content-Transfer-Encoding: 8bit

Hi,<br>Here is some more reading for you.<br>Dad<br>
--0-1096044455-1170970278=:20156--
--0-1572042833-1170970278=:20156
Content-Type: application/msword; name="useless.doc"

Content-Transfer-Encoding: base64
Content-Description: 1986615652-useless.doc
Content-Disposition: attachment; filename="useless.doc"

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAAB
AAAAJAAAAAAAAAAAEAAAJgAAAAEAAAD+////AAAAACMAAAD/////////////
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==

--0-1572042833-1170970278=:20156
Content-Type: application/msword; name="uselesstoo.doc"

Content-Transfer-Encoding: base64
Content-Description: 3685550062-uselesstoo.doc
Content-Disposition: attachment; filename="uselesstoo.doc"

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAAB
AAAAIQAAAAAAAAAAEAAAIwAAAAEAAAD+////AAAAACAAAAD/////////////
////////////////////////////////////////////////////////////
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAA==

--0-1572042833-1170970278=:20156--

everything is nested with multipart/mixed (red) starting things off, with multipart/alternative (blue) as a sub part
 
I think it would work. Let me know if you are able to test it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top