A man has been jailed for more than two years for drug offences after being stopped by police.