Gossamer Forum
Home : Products : Links 2.0 : Customization :

Sending both html and plain text emails

Quote Reply
Sending both html and plain text emails
Hi! I'm posting a somewhat of a solution to a problem which I needed to solve here. Hope some one will find it useful. What I needed the database to do was to send out mails in both html and plain text format without sending out two mails to the same user. Basically what I was achieving here is like some of the newletters which you receive, some of them in the same message contains both the html veersionas well as the plain-text version. Depending on how you set up your email program, the mail when receieved by you will either show the html version if not the plain-text version.

I attempted to change the Mailer.pm module given in the links2 package but to no avail I search ariund and found another module which will do exatcly what I needed. The name of the module is call MIME::Lite available from CPAN. However I could not understand how to incorporate this module with the exisiting module, thus I bssically forgo the existing module and use MIME::Lite instead. What you need to do if you have shell access, is to install the module on your server. However, I was informed as well that if you can't have shell access, all you need to do is place the file Lite.pm into a directory say like the links2 directory and then reference it in the links scripts like so:

use lib "/mydisk/myusername/lib";
use MIME::Lite;

then replace the code that would set up the mailing function in the existing script with these:

MIME::Lite->send('smtp', "smtp.domain.com", Timeout=>60);
$mail = MIME::Lite->new(
From =>$from,
To =>$to,
Subject =>$subject,
Type =>'multipart/alternative'
);
$mail->attach(Type =>'TEXT',
Data =>$msgtxt
);
$mail->attach(Type =>'text/html',
Data =>$msg
);
$mail->send;

If you do not use smtp, replace it with
MIME::Lite->send("sendmail", "/usr/lib/sendmail -t -oi -oem");


Then all you have to do is to use
$msg = qq| print all your html codes including all the necessary html headers here|;
$msgtxt = qq| print all your plain text here|;

I think that's it. I tried it out and it works. I have also attach the Lite.pm file. If anyone knows how to incorporate this module into the exisiting Mailer.pm module, please tell me. I would very much appreciate learning new ways.

Thank you.
Julian

Last edited by:

vampy: Jul 16, 2002, 6:50 AM
Quote Reply
Re: [vampy] Sending both html and plain text emails In reply to
I would like to use this mod to send the modsuccess.html template (filled out) to the user upon successful modification.



The questions I have are after uploading lite.pm to my links/admin directory where Mailer.pm is,what do I do with this code? And Where to put it?



use lib "/mydisk/myusername/lib";

use MIME::Lite;



I will be calling the lite.pm from admin_html.pl, from sub html_modify_email



Here is the modified code:



MIME::Lite->send("sendmail", "/usr/lib/sendmail -t -oi -oem");

$mail = MIME::Lite->new(

From =>$from,

To =>$to,

Subject =>$subject,

Type =>'multipart/alternative'

);

$mail->attach(Type =>'TEXT',

Data =>$msgtxt

);

$mail->attach(Type =>'text/html',

Data =>$msg

);

$mail->send;

Here is my original sub



sub html_modify_email {

# --------------------------------------------------------

# All the link information is stored in %link.

my (%link) = @_;



# Set the to, from, subject and message to send.

my $to = $link{'Contact Email'};

my $from = $db_admin_email;

my $subject = "Your link has been modified!";

my $msg = &load_template ('modsuccess.html', \%link);



# Then mail it away!

require "$db_lib_path/Mailer.pm";

my $mailer = new Mailer ( { smtp => $db_smtp_server,

sendmail => $db_mail_path,

from => $from,

subject => $subject,

to => $to,

msg => $msg,

log => $db_mailer_log

} ) or

&cgierr("Unable to init mailer! Reason: $Mailer::error");

$mailer->send or &cgierr ("Unable to send modification message. Reason: $Mailer::error");

}





What would the new sub look like?



Thanks,

Jake