_post_message
Site Infrastructure / _post_message

[
// syntax: _post_message(user, message, optional viewer, optional title);
// message should be in HTML.

if(($_args[0] == "") || ($_args[0] == 0)) {[
		debug({Can't post message to nobody.})
]} else {[
	$pid = get_profile_id($_args[0]);
	if(has_key($_args, "3"))
		$title = $_args[3]
	else
		$title = {Message ([now()])};

	if($pid == 0)
		debug({User [uid2name($_args[0])] does not have a profile page and cannot receive messages.})
	else {[

		@profile = import entry $pid;

		debug({Recipient profile ID: [$pid]});

		$can_mail = false;

		if(@profile.send_mail == "1") {[
			$can_mail = true;

			$address = getva("email", get_extended(@profile._author));

			if($address == "") {[
				$can_mail = false;
				debug("Recipient has email notifications enabled, but no email address set.");
			]};

			$vn = $_caller._viewer.name;
			if($_args[2] != "") $vn = uid2name($_args[2]);
			if($vn == "") $vn = get_group_name($_caller._args[0]);


			$result = mail($address,
				{from [$vn]: [$title]},
				$_args[1] & {<br><small><a href="http://anthologi.ca/messages">View online in your inbox</a><br>You can unsubscribe from these email notifications on the <a href="http://anthologi.ca/?Settings">Settings page</a>.</small>},
				{From: Anthologi.ca <mail@anthologi.ca>[
if(has_key($_args, "2")) {[
	$spid = get_profile_id($_args[2]);
	@sender_profile = (import $spid);

	$sender_email = getu("email", get_extended($_args[2]));
	if(@sender_profile.send_mail_reply_to) if($sender_email != "") {
Reply-To: [$vn] <[$sender_email]>};
]};
]
Content-type: text/html
}, "-r mail@anthologi.ca");


			if($result == true) {1[
				debug({Sent e-mail.});
			]} else {[
				$can_mail = false;
				debug({Sending e-mail failed.});
			]};
		]};

		// always send on the site too, regardless.
		$can_mail = false;

		if($can_mail == false) {[
			if(search("id", "parent", $pid, "title", $title) == "")
				$p = new_page($title, $pid)
			else
				$p = new_page($title & "<!>" & rand(), $pid);

			if($p > 0) {[
				set_template($p, title2id("Message"));
				set_permissions($p, "-------dr-----------");
				set_priority($p, 1);
				$vid = _caller._viewer.id;
				if($_args[2] != "") $vid = $_args[2];
				set_body($p, "<!>sender=" & $vid & "<!>body=" & $_args[1]);
				debug("Posted profile message to " & @profile._title & ".");
				{1};
			]} else	{[
				debug("Profile message creation failed. Permissions error?");
				debug("Attempted to attach message to " & $pid & ".");
				debug("Applicable permissions: " & has_permissions($_viewer.id, $pid));
				{0};
			]};

		]};

	]};

]};

]