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 question regarding images

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

I wonder if it is possible to send emails with images using sendmail?

I tried this:

open (MAIL, "| $maillocation -t") || die "cant use $maillocation";
print MAIL "from: $from\n";
print MAIL "To: $email\n";
print MAIL "Reply-to: $from\n";
print MAIL "Subject: Test\n";
print MAIL '<IMG SRC=&quot;../images/app.jpg&quot; WIDTH=&quot;5&quot; HEIGHT=&quot;5&quot; BORDER=&quot;0&quot;>';
close (MAIL);

But it didn't arrive!

Regards,

Ron
 
It is possible.

First off, you need to create/adjust the &quot;Content-Type&quot; line so it reflects the MIME type of the file you're sending. For a jpeg, this would be &quot;image/jpeg&quot;. Then you need to encode your file with Base64 encoding and set the &quot;Content-Transfer-Encoding&quot; line to &quot;base64&quot;. Once you've done that, you can pipe it through sendmail or through a direct socket as normal.

If you want all of this to be done for you, I would recommend the MIME::Lite module available from CPAN. The module also has support for multipart messages which are an absolute pain to deal with by hand.

Hope this helps,

brendanc@icehouse.net
 
Thanks for your answer, Sophisticate. As I have no idea how to start doing all of what you suggested I looked at the module MIME::Lite.

I tried this:

#!/usr/bin/perl
print &quot;Content-type: text/html\n\n&quot;; #without this I get errors!

use MIME::Lite;

$msg = MIME::Lite->new(
To =>$email,
Subject =>'HTML with in-line images!',
Type =>'multipart/related'
);
$msg->attach(Type => 'text/html',
Data => qq{ <body>
Here's <i>my</i> image:
<img src=&quot;cid:myimage.gif&quot;>
</body> }
);
$msg->attach(Type => 'image/gif',
Id => 'myimage.gif',
Path => '../images/arrow.gif',
);
$msg->send(&quot;sendmail&quot;, &quot;/usr/sbin/sendmail -t -oi -oem&quot;);


I get an error message so probably the module is not installed on my server. Is there a way to check this?

Regards,
Ron
 
Try this it will tell the html reader that the email it is displaying is a html document.

open (MAIL, &quot;| $maillocation -t&quot;) || die &quot;cant use $maillocation&quot;;
print MAIL &quot;from: $from\n&quot;;
print MAIL &quot;To: $email\n&quot;;
print MAIL &quot;Reply-to: $from\n&quot;;
print MAIL &quot;Subject: Test\n&quot;;
print &quot;Content-type: text/html\n\n&quot;; #this has to be printed here
print MAIL '<IMG SRC=&quot;../images/app.jpg&quot; WIDTH=&quot;5&quot; HEIGHT=&quot;5&quot; BORDER=&quot;0&quot;>';
close (MAIL);

- Note that if it is not printed there it will just display your html code as plain text

- Anoher thing you have to display the full url of the image, something like you have above will not work for others, you have to display the full url, something like:

<IMG SRC=&quot; WIDTH=&quot;5&quot; HEIGHT=&quot;5&quot; BORDER=&quot;0&quot;>

Good Luck
 
If you're on a Linux machine, you can check currently installed modules with a one line shell script. All the modules are stored in one of the paths in the @INC list, so if you do a search within each of those directories, you'll be able to see what you've got.

The shell script is:
Code:
for x in `perl -e 'print join&quot;\n&quot;,@INC'`; do find $x -iname '*.pm'; done
Keep in mind that whenever you see a :: in a module name, that means the module spans multiple directories. For instance, MIME::Lite might be /usr/lib/perl5/site_perl/5.005/MIME/Lite.pm.

Hope this helps,

brendanc@icehouse.net
 
I will try that Sophisticate. I contacted my server and had a look at the module, I can simply install it in my lib folder.

But in the mean time I was just experimenting and I had a lucky guess. I did what P3rL suggested which works perfectly.

But I have another problem I'm trying to create a script to send an email to all members of the mailing list.

I can't even get it to work with a simple text mailing.

This is the particular part:

foreach $mailData(@mailData){
($name,$email,$init) = split(/\|/,$mailData);
open (MAIL, &quot;| $maillocation -t&quot;) || die &quot;aw, cant use $maillocation&quot;;
print MAIL &quot;To: $email\n&quot;;
print MAIL &quot;from: $from\n&quot;;
print MAIL &quot;Reply-to: $from\n&quot;;
print MAIL &quot;Subject: test html mail\n&quot;;
print MAIL &quot;Dear $name\n&quot;;
close (MAIL);
}

This results in internal server errors. When $email is replaced for an email address it works fine (the correct name is listed in the contents). When $mail is replaced for another string, defined outside the foreach statement, it also works fine. I checked the email addresses in the mailling list and they are correct.

Any ideas what could possibly cause the error?

Ron
 
It probably has something to do with the fact that you're using the same variable name in two different contexts in your loop. You actually don't need to declare a temporary variable for the loop... Perl provides one for you ($_). Change:
Code:
foreach $mailData(@mailData) {
($name,$email,$init) = split(/\|/,$mailData);
to:
Code:
foreach(@mailData) {
    my($name,$email,$init) = split /\|/;
or just use a separate variable name:
Code:
foreach my $m(@mailData) {
    my($name,$email,$init) = split /\|/,$m;

Hope this helps,

brendanc@icehouse.net
 
Hi sophisticate, I made some changes as you suggested. The first time I still received an error. The second time, without changing anything, it worked? Thanks!

I was thinking about using this for my mailling list. Am I right that I may run in problems if, in the ideal case, I will have a reasonable amount of members to email to. Will this peace of code be able to sent a good amount of emails. At which number should I expect problems?

Ron,

 
I am trying to send pdf files as an attachment from sendmail to outside email addresses by going through our Exchange server. Some people are getting the attachment encoded as base64. I was wondering if there was a setting I could do on my end to fix this so everyone can receive the attachment properly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top