email_post.pl
Submitted by Jamie on Thu, 2007-06-14 17:34.
Hello.
Does anyone know if it is possible to put some text in bold or even a different colour using email_post.pl or should this be done in the related format.pl file?
Thanks
Jamie
UL



MIME types in email
This is some example Perl code based on work that my colleague Chris Grigson did to send text docs as attachments in email that appear to the user to be Word docs. It demonstrates the use of sendmail for sending multi-part MIME type messages. You will need to use the correct MIME headings for sending HTML text instead of MS Word docs.
I'm not familiar with email_post.pl (we use an older email script), but you would need to use an amended version of that to send MIME messages. Then you can amend format.pl to send HTML instead of plain text.
I recommend sending both a text version and an HTML version so that those who disable HTML viewing will have something to read other than HTML code.
Be warned that you MUST test this before you use it.
The forums may wrap the code, so be careful. Good luck! If you find it useful, or have questions, please contact me.
Here is some annotated Perl:
---------------------------------------------------------------
# Need to load filehandle module to open sendmail
use FileHandle;
# Set up contents of mail etc
$SUBJECT = "Email subject";
$FROM_ADDR = "library\@inst";
$TO_ADDR = "student\@inst";
$PLAIN_TEXT_MSG = "Plain text part of message\n";
$WORD_TEXT_MSG = "This appears to the user to be a Word doc\n";
$WORD_DOC_NAME = "file.doc";
$MAIL_PROGRAM = "/usr/lib/sendmail";
# Initialisation
$fh = new FileHandle; # $fh will be file handle of a pipe to sendmail
$random = (rand()*1000000000000000); # Large string of random digits...
$boundary = "Boundary_separator_$random"; # used to create unique
# string between MIME parts
# Open link to sendmail
if(not $fh->open("| '$MAIL_PROGRAM' '$TO_ADDR'")) {
die "Can't open output to $MAIL_PROGRAM for $TO_ADDR!";
}
# We're OK, so output mail headers, plain text message and then
From: $FROM_ADDR
# fake word doc
print $fh <
To: $TO_ADDR
Subject: $SUBJECT
Content-Type: multipart/mixed;
boundary="$boundary"
MIME-Verion: 1.0
--$boundary
Content-Transfer-Encoding: quoted-printable
Content-type: text/plain
$PLAIN_TEXT_MSG
--$boundary
Content-Type: application/msword;
name="$WORD_DOC_NAME"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="$WORD_DOC_NAME"
$WORD_TEXT_MSG
END_OF_HEADER
# Close filehandle and sendmail will send our message + attachment
close $fh;
HTML in email
I think it would be a case of changing both the email_post.pl script and the format.pl of the script.
The email_post.pl script just sends the contents of a file via email. It would not be possible to insert the html tags in via the email_post.pl script.
The format.pl would have to be modified to include the tags required. This would then make the output unsuitable for printing as well. There are approaches that could be taken to make sure only borrowers with emails would be selected although that it is a different question.
The email_post.pl script would also have to be modified to make sure that the email is sent with details to indicate that the contents is html. I am not sure what the effect will be if a email client cannot view in html format.
The following change only relates to using mail and NOT mailx but does allow the reply address to be taken from the parameter file which was not previously possible. The email content type has to be set to include html. The section in bold needs to be added.
else
{
if( !open(MAIL, "| $mailer_path \"$email_address\"") )
{
&Std_store_error("Unable to send mail");
&Std_output_errors();
&Std_footer( $0, "Processing errors");
exit;
}
($header)="Content-Type: text/html\nMIME-Version: 1.0\n";
print MAIL $header;
print MAIL "Subject: $email_subject \n";
print MAIL "From: $email_replyto \n";
print MAIL "To: $email_address \n\n";
print MAIL $letter;
This is a far from perfect solution and may not fit all circumstances but is a starting point and I would be interested to see any comments or suggestions from html and email experts.
Thanks
Brian Crampton
Developer, Talis