Forum OpenACS Development: Re: How do I send HTML email?

Collapse
Posted by Brian Fenton on
Nima, here's some code I used recently to send an attachment. Let me know how you get on. Brian
set generated_name "test"
set csv_string "contents of csv file"
set csv_file_name "outputfilename.csv"

set file_handle [open $csv_file_name w]
puts $file_handle $csv_string
close $file_handle

set ip_addr [ad_conn peeraddr]
set modifying_user [ad_conn user_id]

set from_addr [db_string some_sql "select email from parties where party_id = :modifying_user"]

# create the multipart message ('multipart/mixed')
set multipart_id [acs_mail_multipart_new -multipart_kind "mixed"]

# create an acs_mail_body (with content_item_id = multipart_id )
set body_id [acs_mail_body_new -header_subject $generated_name -content_item_id $multipart_id ]

# create a new text/plain item
set content_item_id [db_exec_plsql create_text_item {
    begin
        :1 := content_item.new (
               name      => :generated_name, 
               title => :generated_name,
               mime_type => 'text/plain',
               text => :csv_string);
    end;}]    

set sequence_num [acs_mail_multipart_add_content -multipart_id $multipart_id -content_item_id $content_item_id]

db_dml update_multiparts "
      update acs_mail_multipart_parts 
      set mime_disposition='attachment; filename=\"$generated_name\"' 
      where sequence_number=:sequence_num 
      and multipart_id=:multipart_id"

# queue it
set mail_link_id [db_exec_plsql queue_the_mail {
    begin

        :1 := acs_mail_queue_message.new (
                  null,             -- p_mail_link_id
                  :body_id,         -- p_body_id
                  null,             -- p_context_id
                  sysdate,          -- p_creation_date
                  :modifying_user,  -- p_creation_user
                  :ip_addr,         -- p_creation_ip
                  'acs_mail_link'   -- p_object_type
                  );
    end;
    }
    ]      

# send a copy of the mail to the logged in user
set sql_string "
    insert into acs_mail_queue_outgoing
     ( message_id, envelope_from, envelope_to )
    values
     ( :mail_link_id, :from_addr, :to_addr )"

set to_addr $from_addr
if {![empty_string_p $to_addr]} {
  db_dml outgoing_queue $sql_string
} else {
  ns_log Error "To email address  is blank"
}