@Pierre_Larde, if you need such a behaviour to switch mail server back and forth programmatically according to whatever you need, I’d totally skip the application.mailservers definition. That is used in first place, if you want to not use credential attributes in your cfmail tag. If I understand you correctly, id use a global custom struct for that. E.g.
appMailservers={
newsletter={
server="smtp.myserver1.com",
username="mailusername",
password="myPassword",
port="123",
....
etc...
....
},
sellingDepartment={
server="smtp.myserver2.com",
username="mailusername",
password="myPassword",
port="456",
....
etc,
....
},
ITdepartment={
server="smtp.myserver3.com",
username="mailusername",
password="myPassword",
port="789",
....
etc,
....
}
};
Then you just use that in your cfmail template for newsmailings like this:
<cfmail to="recipient@example.com" from="sender@example.com" subject="Example email"
server="#appMailservers.newsletter.server#" username="#appMailservers.newsletter.username#" password="#appMailservers.newsletter.password#" port="#appMailservers.newsletter.port#" ..... >
Your Email Message!!
</cfmail>
Or for the ITdepartment like that:
<cfmail to="recipient@example.com" from="sender@example.com" subject="Example email"
server="#appMailservers.ITdepartment.server#" username="#appMailservers.ITdepartment.username#" password="#appMailservers.ITdepartment.password#" port="#appMailservers.ITdepartment.port#" ..... >
Your Email Message!!
</cfmail>
I’d go even further and wrap cfmail in a function that passes two structs: one for the mail object itself (like subject, recepient, message, etc) and one for the server like defined above. E.g.:
public struct function sendMail( struct mail required, struct mailserver required ){
cfmail(to="#arguments.mail.recepient#", from="#arguments.mail.sender#", subject="#arguments.mail.subject#",
server="#arguments.mailserver.server#",
username="#arguments.mailserver.username#"
.....
){ writeOutput( arguments.mail.message ); };
}
...
return someStruct;
}