Can I programmatically confirm an email address

Created on 19 October 2024, 5 months ago

Problem/Motivation

I wish to code a mechanism to allow users to confirm their email by replying to the confirmation request email.

Is there a way to programmatically confirm an email address?

πŸ’¬ Support request
Status

Active

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States somebodysysop

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @somebodysysop
  • πŸ‡ͺπŸ‡ΈSpain manuel.adan 🌌

    Sure, it is explained in the module's main page

    launch an email confirmation in just 1 line
    \Drupal::service('email_confirmer')->confirm('somemail@example.com');

  • Status changed to Fixed 3 days ago
  • πŸ‡ͺπŸ‡ΈSpain manuel.adan 🌌
  • πŸ‡ΊπŸ‡ΈUnited States somebodysysop

    I don't believe the question I asked is explained in the module's main page.

    What wanted to build was a mechanism by which a user could confirm his email address by replying to the confirmation email sent by email-confirmer. I was looking for a hook that would allow me to do that.

    I guess I wasn't clear enough in my question. Anyway, I came up with this process:

    1. extract_confirmation_url_from_email($email_body)
    2. extract_uuid_from_url($url)
    3. confirm_email_by_uuid($uuid)

    Here is the code I created to confirm the email address (using the code from the home page) using the uuid extracted from the reply email:

    	/**
    	 * Confirms an email confirmation entity by UUID.
    	 *
    	 * This method loads the email confirmation entity based on the UUID provided.
    	 * If the entity is still pending, it marks the confirmation as confirmed, saves it,
    	 * and returns 1 for a successful confirmation. If the entity is not found or 
    	 * is not in a pending state, it returns 0.
    	 *
    	 * @param string $uuid
    	 *   The UUID of the email confirmation entity.
    	 *
    	 * @return int
    	 *   Returns 1 if the confirmation is successful, or 0 if the confirmation fails.
    	 */
    	function confirm_email_by_uuid($uuid) {
    	  // Load the confirmation entity by UUID.
    	  $confirmations = \Drupal::entityTypeManager()
    		->getStorage('email_confirmer_confirmation')
    		->loadByProperties(['uuid' => $uuid]);
    
    	  // Get the first confirmation entity.
    	  $confirmation = reset($confirmations);
    
    	  if ($confirmation && $confirmation->isPending()) {
    		// Bypass hash and directly mark as confirmed.
    		$confirmation->set('confirmed', EmailConfirmationInterface::CONFIRMED);
    		$confirmation->save();
    
    		// Optionally invoke hooks or additional logic.
    		\Drupal::moduleHandler()->invokeAll('email_confirmer', ['confirm', $confirmation]);
    
    		// Return 1 for successful confirmation.
    		return 1;
    	  } else {
    		// Return 0 for invalid, expired, or not pending confirmation.
    		return 0;
    	  }
    	}
    
    

    It's working.

    I am aware that using this mechanism, all someone has to do is forward the email sent by email_confirmer to anyone they wish. But, in my situation, the onus for the results is on the user.

    Anyway, thanks for the response. Better late than never.

Production build 0.71.5 2024