src/Context/Email/EmailContext.php
public function loginWithCredentialsThatWasSentByEmail($to = '')
    {
        /**
         * Function must return an associative array with two keys: "username" and "password". The
         * value of each key should be a string with placeholder that will be replaced with user
         * login and password from an account. In testing, placeholders will be replaced by regular
         * expressions for parse the message that was sent.
         *
         * @example
         * @code
         * function mail_account_strings($name, $pass) {
         *     return array(
         *       'username' => t('Username: !mail', array('!mail' => $name)),
         *       'password' => t('Password: !pass', array('!pass' => $pass)),
         *     );
         * }
         *
         * // Drupal module.
         * function hook_mail($key, &$message, $params) {
         *     switch ($key) {
         *         case 'account':
         *             $message['subject'] = t('Website Account');
         *             $message['body'][] = t('You can login on the site using next credentials:');
         *             $message['body'] += mail_account_strings($params['mail'], $params['pass']);
         *         break;
         *     }
         * }
         *
         * // Behat usage.
         * mail_account_strings('(.+?)', '(.+?)');
         * @endcode
         *
         * @var callable $callback
         */
        $param = 'email_account_strings';
        $callback = $this->getTqParameter($param);

        if (empty($callback)) {
            throw new \Exception(sprintf('Parameter "%s" was not specified in "behat.yml"', $param));
        }

        if (!is_callable($callback)) {
            throw new \InvalidArgumentException(sprintf('The value of "%s" parameter is not callable.', $param));
        }

        $regexps = array_filter(call_user_func($callback, self::PARSE_STRING, self::PARSE_STRING));

        if (count($regexps) < 2) {
            throw new \RuntimeException(sprintf('Unfortunately you have wrong "%s" function.', $callback));
        }

        $userContext = $this->getUserContext();

        foreach ($this->getEmailMessages($to) as $message) {
            if (!empty($message['body'])) {
                $matches = [];

                // Process every line.
                foreach (explode("\n", $message['body']) as $string) {
                    foreach ($regexps as $name => $regexp) {
                        preg_match($regexp, $string, $match);

                        if (!empty($match[1])) {
                            $matches[$name] = $match[1];
                        }
                    }
                }

                if (!empty($matches['username']) && !empty($matches['password'])) {
                    $userContext->fillLoginForm($matches);
                    break;
                }
            }
        }

        if (!$userContext->isLoggedIn()) {
            throw new \RuntimeException(
                'Failed to login because email does not contain user credentials or they are was not parsed correctly.'
            );
        }
    }
public function loginWithCredentialsThatWasSentByEmail($to = '')
    {
        /**
         * Function must return an associative array with two keys: "username" and "password". The
         * value of each key should be a string with placeholder that will be replaced with user
         * login and password from an account. In testing, placeholders will be replaced by regular
         * expressions for parse the message that was sent.
         *
         * @example
         * @code
         * function mail_account_strings($name, $pass) {
         *     return array(
         *       'username' => t('Username: !mail', array('!mail' => $name)),
         *       'password' => t('Password: !pass', array('!pass' => $pass)),
         *     );
         * }
         *
         * // Drupal module.
         * function hook_mail($key, &$message, $params) {
         *     switch ($key) {
         *         case 'account':
         *             $message['subject'] = t('Website Account');
         *             $message['body'][] = t('You can login on the site using next credentials:');
         *             $message['body'] += mail_account_strings($params['mail'], $params['pass']);
         *         break;
         *     }
         * }
         *
         * // Behat usage.
         * mail_account_strings('(.+?)', '(.+?)');
         * @endcode
         *
         * @var callable $callback
         */
        $param = 'email_account_strings';
        $callback = $this->getTqParameter($param);

        if (empty($callback)) {
            throw new \Exception(sprintf('Parameter "%s" was not specified in "behat.yml"', $param));
        }

        if (!is_callable($callback)) {
            throw new \InvalidArgumentException(sprintf('The value of "%s" parameter is not callable.', $param));
        }

        $regexps = array_filter(call_user_func($callback, self::PARSE_STRING, self::PARSE_STRING));

        if (count($regexps) < 2) {
            throw new \RuntimeException(sprintf('Unfortunately you have wrong "%s" function.', $callback));
        }

        $userContext = $this->getUserContext();

        foreach ($this->getEmailMessages($to) as $message) {
            if (!empty($message['body'])) {
                $matches = [];

                // Process every line.
                foreach (explode("\n", $message['body']) as $string) {
                    foreach ($regexps as $name => $regexp) {
                        preg_match($regexp, $string, $match);

                        if (!empty($match[1])) {
                            $matches[$name] = $match[1];
                        }
                    }
                }

                if (!empty($matches['username']) && !empty($matches['password'])) {
                    $userContext->fillLoginForm($matches);
                    break;
                }
            }
        }

        if (!$userContext->isLoggedIn()) {
            throw new \RuntimeException(
                'Failed to login because email does not contain user credentials or they are was not parsed correctly.'
            );
        }
    }
@param string $to

@throws \Exception
When parameter "parse_mail_callback" was not specified.
@throws \InvalidArgumentException
When parameter "parse_mail_callback" is not callable.
@throws NoSuchElement
When "Log in" button cannot be found on the page.
@throws \RuntimeException
When credentials cannot be parsed or does not exist.

@Given /^(?:|I )login with credentials that was sent on (?:"([^"]*)"|email)$/