Forum OpenACS Q&A: Re: help with acs-mail

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 {
	:1 := (
		 name      => :generated_name, 
		 title => :generated_name,
		 mime_type => 'text/plain',
		 text => :csv_string);

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 {
	:1 := (
		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

# 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 )
	 ( :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] 
" ad_script_abort }