I need to know what is the limit of mail function and current servers?
The mail function itself has no limit, but some hosting providers limit the number of messages you can send in case of shared hosting, i.e. multiple clients on the same server.
How many emails at a time can I send?
The main limit is the runtime. There is usually a limit of 30 seconds, but you can eliminate the limit with the set_time_limit (0) function. However, some hosting providers are not allowed to use this function.
What to do to not overload the server?
Depends on how you want to send. In general email sending does not cause overhead in terms of CPU usage because it only requires process or network communication with servers, and since this is only I / O operations do not consume CPU.
What most CPU consumes is the composition of the messages themselves. What happens is that email standards require you to properly encode MIME messages.
This means that for example 8-bit characters (letters with accents and letters) should be encoded in the body of the message using quoted-printable encoding, something you are not doing in the example above.
As of PHP 5.3, the quoted_printable_encode function is integrated into PHP. It is much faster and simpler than trying to encode text with PHP routines as needed in previous versions.
Otherwise I do not recommend reinventing the wheel and try to do it manually as you did in your example composing e-mail messages. There are many ready-to-do classes that know how to compose messages according to Internet e-mail patterns. You do not need to learn all of the standards to use these classes.
I use the class MIME message which is even optimized for sending bulk email. I have a website that sends around 5 million newsletters per month. Some newsletters go by email to over 400,000 subscribers.
This class has caching capabilities to reuse connections to identical composite messages sent to different recipients.
On the iMasters website you have a more detailed article explaining how Sending bulk email in PHP using smart optimizations with this class.
Advantages of installing SMTP on localhost and local SMTP boundaries?
In this question you assume that you need an SMTP server to send messages. This is not true. It is a common misconception. I'll try to explain without going into too many technical details.
SMTP is a protocol for receiving messages. Who gets is the SMTP server. You only need an SMTP server if you need to receive messages.
In PHP to send mail on a Linux system, the mail function uses a local program called sendmail that injects the message into the local mail server.
The message is queued on the mail server and later the mail server sends the message communicating with the destination SMTP server of the recipient's email domain.
So PHP itself does not use SMTP in Linux. PHP uses SMTP in Windows because normally Windows does not come with the mail server and so PHP needs to communicate with an SMTP server from your provider that is usually on another machine.
Some sites use external e-mail services such as Amazon or MailChimp because the hosting provider does not install a local mail server or offer an SMTP server to delegate message delivery.
However, these services are very expensive when you exceed the limits of free accounts, which seems to be the case.
In your case, I recommend installing a local mail server, such as postfix or qmail, to avoid paying high email accounts.
Note that when I say local mail server, I am referring to an email server on your hosting machine, not on your personal computer that you have at home or at your company.
If you do not have an email server installed, you may need a virtual server (VPS) plan or dedicated server to install if your hosting provider does not provide one.
However you need to be careful to configure your domain's SPF TXT record so messages are not considered spam. You also need to be careful if you use an external SMTP server.
If you have questions about this, you can comment below, or create a new question.