This study proposes a closed-form solution for the two-dimensional (2D) transient heat conduction in a rectangular cross-section of an infinite bar with space–time-dependent Dirichlet boundary conditions and heat sources. The main purpose of this study is to eliminate the limitations of the previous study and add heat sources to the heat conduction system. The restriction of the previous study is that the values of the boundary conditions and initial conditions at the four corners of the rectangular region should be zero. First, the boundary value problem of 2D heat conduction system is transformed into a dimensionless form. Second, the dimensionless temperature function is transformed so that the temperatures at the four endpoints of the boundary of the rectangular region become zero. Dividing the system into two one-dimensional (1D) subsystems and solving them by combining the proposed shifting function method with the eigenfunction expansion theorem, the complete solution in series form is obtained through the superposition of the subsystem solutions. Three examples are studied to illustrate the efficiency and reliability of the method. For convenience, the space–time-dependent functions used in the examples are considered separable in the space–time domain. The linear, parabolic, and sine functions are chosen as the space-dependent functions, and the sine, cosine, and exponential functions are chosen as the time-dependent functions. The solutions in the literature are used to verify the correctness of the solutions derived using the proposed method, and the results are completely consistent. The parameter influence of the time-dependent function of the boundary conditions and heat sources on the temperature variation is also investigated. The time-dependent function includes exponential type and harmonic type. For the exponential time-dependent function, a smaller decay constant of the time-dependent function leads to a greater temperature drop. For the harmonic time-dependent function, a higher frequency of the time-dependent function leads to a more frequent fluctuation of the temperature change.