WordPress.org

Ready to get started?Download WordPress

Forums

wp_mail is sending mail twice (2 posts)

  1. adityabajaj
    Member
    Posted 1 year ago #

    Hello,
    I am using the following code but it is sending email twice. Could you please help me fix the code to send the email only once? I tried checking post back condition, but that is not right here as I understand.
    The code is executed after checking checking when the user's login session and logging out forcefully and sending an email to admin about it.
    Here is my code:

    define( 'CCLO_CHECK_KEY' , 'cclo-key' );
    
    add_action( 'auth_cookie_valid', 'cclo_check_key' , null , 2 );
    function cclo_check_key( $cookie_elements, $user) {
    
    	$login_key = get_user_meta( $user->ID , CCLO_CHECK_KEY , true );
    
    	//check cookie,login_key
    	if( $login_key != $_COOKIE[CCLO_CHECK_KEY] ) {
    		//error??
    		$redirect_to = wp_login_url() ;
    		wp_logout();
    		wp_safe_redirect( $redirect_to );
    		sendmail($user);
    	}
    }
    
    function sendmail($user)
    {
    //send email about user
    add_filter( 'wp_mail_content_type', 'set_html_content_type' );
    wp_mail( 'asbajaj001@gmail.com', 'Concurrent Login: ' . $user->user_login  , '<p>The user <strong>'. $user->user_login . '</strong> logged in concurrently.</p><p><strong>Further Details:</strong></p><p>Username: ' . $user->user_login . '<br /> Display Name: ' . $user->display_name . '<br />ID: ' . $user->ID . '<br />IP Address Recorded: <strong>' . $_SERVER['REMOTE_ADDR'] . '</strong> <br />Proxy IP Address Recorded: <strong>' . $_SERVER['HTTP_X_FORWARDED_FOR'] . '</strong></p>'  );
    remove_filter( 'wp_mail_content_type', 'set_html_content_type' ); // reset content-type to to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
    }
    
    function set_html_content_type()
    {
    	return 'text/html';
    }
    
    add_filter( 'auth_cookie', 'cclo_set_key' , null , 4 );
    function cclo_set_key( $cookie, $user_id, $expiration, $scheme ){
    
    	$login_key = 'i_have_a_dream_'.time();
    
    	update_user_meta( $user_id , CCLO_CHECK_KEY, $login_key );
    
    	setcookie( CCLO_CHECK_KEY , $login_key , $expiration, SITECOOKIEPATH );		
    
    	return $cookie;
    }
  2. ncc1701capt
    Member
    Posted 8 months ago #

    Did anybody respond to this? I'm having the same problem. here is my code snippet - it is definitely not a loop problem:

    $EmailTo = 'someone@example.com';
    $headers[] = "From: Someone Else <someone-else@anotherexample.com> ";
    $headers[] = "MIME-Version: 1.0 ";
    $headers[] = "Content-Type: text/html; charset=UTF-8";
    $Subject = 'Email Subject';

    if(!wp_mail($EmailTo,$Subject,$Message,$headers)) {
    die("Email failed to send.");
    }

    Thanks for any help you can provide!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags