完成(db查询和邮件)或无

I am writing some code to deactivate a users account. When the form is submitted I update the database and email the user confirming the deactivation was successful.

However, if the email fails to send I do not want to update the database and if the query to update the database fails I do not want to send the email, it's all or nothing.

How should I do this? Try catches? Is it even possible? What do you recommend?

//DEACTIVATE ACCOUNT & LOG OUT ALL SESSIONS
$db->update("users", "id", "=", $user->data()->id, array(
    "active" => 0,
    "logout_all" => 1
));

//EMAIL USER
$mail->send(
    "user_deactivated", 
    array(
        "to" => $user->data()->email,
        "subject" => "Account Deactivated",
        "message" => null,
        "from" => "noreply@domain.com"
    ), 
    array(
        "name" => $user->data()->full_name
    )
);

//LOG USER OUT
$user->logout();

Thanks

You can try to use database transactions. Scheme of algoritm can looks:

START TRANSACTION
UPDATE
IF NO ERRORS IN TRANSACTION THEN
    IF SEND EMAIL THEN
        COMMIT TRANSACTION
    ELSE
        ROLLBACK TRANSACTION
ELSE
    ROLLBACK TRANSACTION