• Hi,

    I’m working on a WordPress blog and am trying to modify part of Contact Form 7 to automatically send the email using the User’s name and password for their account. There doesn’t seem to be a way to do this automatically, which is unfortunate, so I’m trying to modify the code where it submits the email to just read in the user info. Unfortunately, I can’t find what part of the code sends off the email.

    This has to be done this way – we’re not using it for simply a contact form but for something integrated with the site, and we need to be sure the correct user info is supplied. Is there a way to do this easily with Contact Form 7, or is there another email plugin that can be made to do this, or where in the source code for CF7 should I change things?

    Any help would be greatly appreciated.

Viewing 10 replies - 1 through 10 (of 10 total)
  • i’m facing the same problem, did you sort it out already?

    Thread Starter PigMan49

    (@pigman49)

    No. I’m hoping that somebody around here knows how Contact Form 7 works.

    I managed to hack my way to a solution regarding sending the username of the logged in user.

    To the file plugins/contact-form-7/modules/special-mail-tags.php I added this code:

    add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag_userdata', 10, 2 );
    
    function wpcf7_special_mail_tag_userdata( $output, $name ) {
    
    	// For backwards compat.
    	$name = preg_replace( '/^wpcf7\./', '_', $name );
    
    	global $current_user;
    
    	if ( 'user_login' == $name )
    		$output = $current_user->user_login;
    
    	return $output;
    }

    This allows me to use the special mail tag [user_login] just like these special mail tags:
    http://contactform7.com/2009/12/25/special-mail-tags/

    Is there also a way to take a email address from a customfield from the post/page where the form is placed?

    @jonas – I just posted a way to output custom fields in another thread, might help achieve what you’re trying to do http://wordpress.org/support/topic/plugin-contact-form-7-mail-recipient-always-different?replies=2#post-1641672

    Hi,

    my problem is the same. I need the forms sent to me, with the logged user name in it. I tried modifying special-mail-tags.php as Jacob Smith said, and it worked. The mail was sent with the user name using a [user_login] tag. But, when I tried to log out, I got problems:

    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-login.php on line 337
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-login.php on line 349
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 717
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 718
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 719
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 720
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 721
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 722
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 725
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 726
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 727
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 728
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 731
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 732
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 733
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 734
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 890

    What did I do wrong? Could you please give the full special-mail-tags.php text so that I can use it in my site?

    Thanks a lot.

    No answers?

    @propulso: Just saw your comments now.

    Here is the full contents of my special-mail-tags.php:

    <?php
    /**
    ** Filters for Special Mail Tags
    **/
    
    add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag', 10, 2 );
    
    function wpcf7_special_mail_tag( $output, $name ) {
    
        // For backwards compat.
        $name = preg_replace( '/^wpcf7\./', '_', $name );
    
        if ( '_remote_ip' == $name )
            $output = preg_replace( '/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR'] );
    
        elseif ( '_url' == $name )
            $output = get_option( 'home' ) . wpcf7_get_request_uri();
    
        elseif ( '_date' == $name )
            $output = date_i18n( get_option( 'date_format' ) );
    
        elseif ( '_time' == $name )
            $output = date_i18n( get_option( 'time_format' ) );
    
        return $output;
    }
    
    add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag_for_post_data', 10, 2 );
    
    function wpcf7_special_mail_tag_for_post_data( $output, $name ) {
    
        if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) )
            return $output;
    
        if ( ! preg_match( '/^wpcf7-f(\d+)-p(\d+)-o(\d+)$/', $_POST['_wpcf7_unit_tag'], $matches ) )
            return $output;
    
        $post_id = (int) $matches[2];
    
        if ( ! $post = get_post( $post_id ) )
            return $output;
    
        $user = new WP_User( $post->post_author );
    
        // For backwards compat.
        $name = preg_replace( '/^wpcf7\./', '_', $name );
    
        if ( '_post_id' == $name )
            $output = (string) $post->ID;
    
        elseif ( '_post_name' == $name )
            $output = $post->post_name;
    
        elseif ( '_post_title' == $name )
            $output = $post->post_title;
    
        elseif ( '_post_url' == $name )
            $output = get_permalink( $post->ID );
    
        elseif ( '_post_author' == $name )
            $output = $user->display_name;
    
        elseif ( '_post_author_email' == $name )
            $output = $user->user_email;
    
        return $output;
    }
    
    add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag_userdata', 10, 2 );
    
    function wpcf7_special_mail_tag_userdata( $output, $name ) {
    
    // For backwards compat.
    
    $name = preg_replace( '/^wpcf7\./', '_', $name );
    
    global $current_user;
    
    if ( 'user_login' == $name )
    $output = $current_user->user_login;
    
    return $output;
    }
    
    ?>

    I hope it helps; I haven’t encountered that error.

    It seems that that kind of error can be related to spaces before and/or after PHP code in various files:
    http://www.hongkiat.com/blog/wordpress-error-warning-cannot-modify-header-information/

    Hi Jacob,

    sorry I didn’t comment before just to thank you for your help. It works just FINE. Great job, great help, big thanks.

    πŸ™‚

    It would be great if this became a DEFAULT option otherwise everytime there is an update of the plugin this gets overwritten.

    If a blog user is logged in, then the email/name should be pre-populated by default.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Contact Form 7: Automatically send with username and email’ is closed to new replies.