Darren Bygraves handed over the drugs to his girlfriend who was being held on remand at HMP New Hall, near Wakefield, after being charged with stabbing him.
A court heard Bygraves committed the offence has he had developed an infatuation for the prisoner who was much younger than him.
Bygraves, 49, handed over the drugs to the woman as he gave her a "hug and a kiss" in the prison's visitor area.
Leeds Crown Court heard the inmate had already attracted the suspicion of prison officers shortly before the visiting session by announcing that she was no longer using drugs.
Officers stopped and searched the woman as she left the visitor area.
Tablets wrapped in clingfilm were found in the bottom of her trousers.
Robert Galley said the pills were sedatives and tablets to treat irritable bowel syndrome.
The court heard Bygraves and the woman were both arrested over the incident but he continued to offend while on bail.
Police officers stopped him in his Fiat Punto in Barnsley on October 8, 2018.
Bygraves tried to drive off but the officers managed to stop him.
They found 18 small packages of ketamine in the vehicle along with cards offering a "delivery service" for the class B drug.
The defendant was interviewed and said he was being paid a wage to sell the drug in order to clear debts.
Bygraves was arrested again in April last year as he sold heroin and crack cocaine on the streets.
Police stopped him in his Ford Mondeo and he was found in possession of a Kinder egg which contained the class A drugs.
His mobile phone was analysed and found to contain messages linking him to drug supply.
Bygraves, of Kirkham Place, Monk Bretton, Barnsley, pleaded guilty to two counts of possession of a class A drug, possessing a class B drug and conveying a drug into prison.
The court heard Bygraves also breached a suspended sentence order by offending .
He had been spared a prison sentence for attempting to pervert the course of justice following the incident when he was stabbed by his girlfriend.
Bygraves refused to give a statement to police after his girlfriend was arrested on suspicion of wounding with intent.
The prosecution went ahead despite Bygraves refusing to co-operate as the incident was witnessed by a neighbour.
Bygraves was convicted of the perverting justice offence after he put superglue in the neighbour's door locks as he tried to deter them from giving a statement in the investigation.
Sean Fritchley, mitigating, said Bygraves had worked as a used car salesman and started the relationship with the woman in 2015
He said: "She had been using drugs recreationally and he got dragged into the cycle of drug use.
"From the start he says the relationship was a violent one.
"She was violent towards him. Police were called on numerous occasions.
"The background was that he had become infatuated with a female that was far younger than him.
"The relationship on her part might not well have been genuine from the start."
Mr Fritchley said Bygraves's relationship with the woman is now over.
Bygraves was jailed for four years and one month.