There do not appear to be many - if any - rail options, other than up and back to Cordoba... Any help with connecting the Costa de Sol cities, like Malaga and Algeciris and Cadiz and Huelva and - over to Portugal, to Lagos and Faro?
You can do it by bus.
http://www.alsa.es/en/buy-and-schedules/international/?searchType=international
Yes, while we think of Spain and Portugal as practically one country and assume it's easy to get between them, the connections between them are poor. Bus is the best option, because renting a car in one country and dropping it in another will incur huge fees. You can also fly (I don't think this is good for your itinerary, but Malaga and Faro have airports; you can check http://www.skyscanner.com/).
Distances through Spain and Portugal are deceiving. I was looking to go from Faro to Barcelona, and it was 650 miles. Our niece has a waterfront house outside Faro. But we have to go thru London to get there on a budget air carrier.
Wikipedia has up to date information on virtually every airport in the world. It points you in the direction on what flights are available into or out of any airport.
"But we have to go thru London to get there on a budget air carrier. " That's exactly what I was talking about - poor connections between Spain and Portugal, even though they're "right next to each other."
This company http://www.eva-bus.com/novo/index.php?lang=uk will take you from points in west Andalucia to points on the Algarve. There are a number of bus companies in Andalucia, a couple not even with a web presence, but usually only one company plies a particular route. If you have now not the information for what you wish to accomplish come on back with an 'A to B' request(s) or your itinerary.
If you are flying in and out of Malaga you can easily rent a car at the airport and drive to Portugal. Great way to explore n your own and not have to sit on a bus for hours. You do not need to drop it in another country and as I remember it the rates were quite reasonable for a week rental.