Forum OpenACS Q&A: help with acs-mail

Collapse
Posted by Rafael Calvo on
Hi
I'm building an application that should send HTML emails (eventually with attachments) given some profiles built by users. It would normally use the notifications package but I could not find a way of formating emails with it, so I'm trying to use acs-mail.
I have taken code from the documentation at /doc/acs-mail/openacs-mail.html in my installation, but I get an error (see below).
I have two questions:
1) Is acs-mail the way to go? what about acs-mail-lite? (I can see that notifications uses it, but I read in other thread that it was planned to be merged into acs-mail)
2) If so why isn't the function found? thanks
set subject "GS alert: My subject"
set headerTo "rafa@usyd.edu.au"
set headerFrom "rafa@usyd.edu.au"
set email "rafa@usyd.edu.au"
set content_type "text/plain"

set body_id [acs_mail_body_new -header_subject $subject -content $text -content_type $content_type]

# queue it
set sql_string "select acs_mail_queue_message.new (
                  null,             -- p_mail_link_id
                  :body_id,         -- p_body_id
                  null,             -- p_context_id
                  now(),            -- p_creation_date
                  :user_id,         -- p_creation_user
                  :ip_addr,         -- p_creation_ip
                  'acs_mail_link'   -- p_object_type
                );"

set mail_link_id [db_string queue_message $sql_string]

# put in in outgoing queue
set sql_string "
insert into acs_mail_queue_outgoing
 ( message_id, envelope_from, envelope_to )
values
 ( :mail_link_id, :from_addr, :to_addr )"

db_dml outgoing_queue $sql_string
         
error:
Database operation "0or1row" failed (exception NSDB, "Query was not a statement returning rows.")

ERROR:  Function acs_mail_body__new("unknown", "unknown", "unknown", "unknown", "unknown", "unknown", "unknown", "unknown", "unknown", "unknown", "unknown", timestamptz, "unknown", "unknown", "unknown") does not exist
	Unable to identify a function that satisfies the given argument types
	You may need to add explicit typecasts

SQL: 

select acs_mail_body__new (
  NULL,				-- body_id 
  NULL,		-- body_reply_to 
  NULL,			-- body_from 
  NULL,			-- body_date 
  NULL,	-- header_message_id 
  NULL,		-- header_reply_to 
  'GS alert: My subject',      -- header_subject 
  NULL,			-- header_from 
  NULL,			-- header_to 
  NULL,		-- content_item_id 
  'acs_mail_body',		-- object_type
  now(),				-- creation_date
  NULL,		-- creation_user 
  NULL,			-- creation_ip 
  null					-- context_id
);



Collapse
2: Re: help with acs-mail (response to 1)
Posted by Rafael Calvo on
I just noticed that nobody is using this code:

 grep -Rl acs_mail_body_new *
acs-mail/sql/oracle/acs-mail-create.sql
acs-mail/sql/postgresql/acs-mail-create.sql
acs-mail/tcl/acs-mail-procs-oracle.xql
acs-mail/tcl/acs-mail-procs-postgresql.xql
acs-mail/tcl/acs-mail-procs.tcl
acs-mail/www/doc/openacs-mail.html

I guess the documentation is outdated and this function has been deprecated. Shouldn't we remove it from the documentation / code?

I'm now lost on how to use acs-mail 😟

Collapse
3: Re: help with acs-mail (response to 1)
Posted by Rafael Calvo on
I am now trying to send html emails using acs-mail-lite. Using bulk-mail as example I tried some code added by Mohan (acording to the code). It uses:
set message_data [build_mime_message $text $message_html]
but it look like build_mime_message is broken, since at least 1 file is missing:
couldn't read file "/var/lib/aolserver/rafa/packages/acs-tcl/tcl/base64.tcl": no such file or directory
    while executing
Is this package deprecated as well. Is this a bug that I need to report? cheers
Collapse
4: Re: help with acs-mail (response to 1)
Posted by Torben Brosten on
Rafael,

There's a base64-procs.tcl, mime-procs.tcl and md5-procs.tcl in that dir.

$ more base64-procs.tcl

# base64.tcl --
#
#......

Looks like the filenames have changed from *.tcl to *-procs.tcl

This thread may also be useful:

https://openacs.org/forums/message-view?message_id=51834

cheers,

Torben

Collapse
5: Re: help with acs-mail (response to 1)
Posted by Brian Fenton on
Hi Rafael, acs_mail_body_new works for me. Here's some code where I create a mail with an attachment and send it to 2 different email addresses. In my case csv_string is a string I build which contains CSV data. I'm using OpenACS 5.0.0 beta 1. Hope this helps, Brian
db_transaction {



#email the file to this user and also to accounts - see /doc/acs-mail/openacs-mail.html for usage
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 each to accounts and 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 )"

#copy for accounts
set to_addr [parameter::get -parameter accounts_email_address]
if {![empty_string_p $to_addr]} {
	db_dml outgoing_queue $sql_string
} else {
	ns_log Error " accounts_email_address parameter is blank"
}

#send another copy to the logged in user
set to_addr $from_addr
db_dml outgoing_queue $sql_string


} on_error {
	ad_return_error "[_ work.unable_to_update]" "[_ work.we_got_following_error] 
$errmsg
" ad_script_abort }