Send email from assets file

Hello,

is it possible to access $kirby->email() from a file located in /assets/php/ to send an email ?

greetings perry


    <?php

	$post = file_get_contents('php://input');

	$t = json_decode($post, true);

	if ($t["vorname"] == "" || $t["name"] == "" || $t["email"] == "" || $t["subject"] == "") :
		echo "error-1";
		return false;
	endif;

	// Validate email
	if (filter_var($t["email"], FILTER_VALIDATE_EMAIL)) :

		echo "success";

	else :
		echo "error-2";
	endif;
	
	try {
		$kirby->email([
			'from' => 'welcome@supercompany.com',
			'replyTo' => 'no-reply@supercompany.com',
			'to' => 'st@gmail.com',
			'cc' => 'st@gmail.com',
			'subject' => "betreff",
			'body' => 'It\'s great to have you with us',
		]);
	} catch (Exception $error) {
		echo "error-3";
	}

	?>

See here how to require Kirby in your scripts: How to migrate users | Kirby CMS

I don’t quite understand why I should migrate an account. I don’t want to write an email to a user, I want to send the data from a contact form. when I use the $kirby->email() part in a template it works, but not when I run it in an assets file.

It was just an example how to include kirby into your script so that you can use Kirby’s methods.

is this part enough to make the kirby() available in the asset as well?

require __DIR__ . '/kirby/bootstrap.php';
$kirby = new Kirby;

is there a cookbook where the procedure is described in detail?

Yes, you only have to require that file, no need for a recipe, I think.

does this also work if I also call the assets file with JS?

this part creates a http status: 500
$kirby = new Kirby;

send form data

    let sendData = function() {

        let data = {
            "vorname": document.getElementById("vorname").value,
            "name": document.getElementById("name").value,
            "email": document.getElementById("email").value,
            "tel": document.getElementById("tel").value,
            "subject": document.getElementById("subject").value,
            "message": document.getElementById("message").value,
        }



        fetch("/assets/php/contact-form-sender.php", {
                method: "POST",
                headers: {
                    'Accept': 'application/json, text/plain, */*',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            })

            .then((result) => {
                if (result.status != 200) {
                    console.log(result)
                    //throw new Error("Bad Server Response");
                }
                return result.text();
            })

            .then((response) => {
                console.log(response);
            })

            .catch((error) => {
                console.log(error);
            });

        return false;
    }


    document.getElementById("submit-button").addEventListener("click", function(e) {
        e.preventDefault();
        sendData()
    })

process data

    <?php

	require __DIR__ . '/kirby/bootstrap.php';
	$kirby = new Kirby;

	$post = file_get_contents('php://input');

	$t = json_decode($post, true);

	if ($t["vorname"] == "" || $t["name"] == "" || $t["email"] == "" || $t["subject"] == "") :
		echo "error-1";
		return false;
	endif;

	// Validate email
	if (filter_var($t["email"], FILTER_VALIDATE_EMAIL)) :

		echo "success";




		try {
			$kirby->email([
				'from' => 'welcome@supercompany.com',
				'replyTo' => 'no-reply@supercompany.com',
				'to' => 'st@gmail.com',
				'cc' => 'st@gmail.com',
				'subject' => "",
				'body' => 'It\'s great to have you with us',
			]);
		} catch (Exception $error) {
			echo "error-3";
		}




	else :
		echo "error-2";
	endif;


	
	?>