Trying to understand why code is included in an SMS message

I've been testing a sketch found here by an active contributor, used to test and wait for a response from a GSM modem, for commands sent. Using this send SMS example (with a SIM7670G), the code sends not just the const char * SMSMessage = "New Message";
but it sends a message like this:

I have tested many different sketches using the actual number and message text, but I want to use variables for the phone number and message like this example.

I can't see in the code why this would be happening, any advice on this appreciated.

//---- courtesy of John Wasser ---
//---- Sends "AT+CMGF=1 AT+CMGS+"+17xxxxxxxx3" New Message"  all as a text message  ??? ----
#include <SoftwareSerial.h>  
SoftwareSerial ATCommandStream(3, 4);
unsigned long ATComandBaudRate = 9600;
const char * DestinationNumber = "+17xxxxxxxx3";
const char * SMSMessage = "New Message";

void setup()
{
  // start th serial communication with the host computer
  Serial.begin(9600);
  while (!Serial);
  delay(200);
  Serial.println("Sketch started.");
  ATCommandStream.begin(ATComandBaudRate);
  Serial.print("ATCommandStream started at baud rate ");
  Serial.println(ATComandBaudRate);
  
  // Set the SMS output to text mode
  SendShortCommand("AT+CMGF=1");
  SendSMSMessage(DestinationNumber, SMSMessage);
  SendShortCommand("AT"); // Just checking that it responds with OK
}

void loop() {}

bool WaitForResponse()
{
  unsigned long startTime = millis();
  while (millis() - startTime < 5000)
  {
    String reply = ATCommandStream.readStringUntil('\n');
    if (reply.length() > 0)
    {
      Serial.print("Received: \"");
      Serial.print(reply);
      Serial.println("\"");
        if (reply.startsWith("OK"))
          return true;
        if (reply.startsWith("ERROR"))
          return false;
    }
  }
  Serial.println("Did not receive OK.");
  return false;
}

bool SendShortCommand(String command)
{
  Serial.print("Sending command: \"");
  Serial.print(command);
  Serial.println("\"");

  ATCommandStream.print(command);
  ATCommandStream.print("\r\n");

  return WaitForResponse();
}

void SendSMSMessage(const char *number, const char *message)
{
  Serial.println("Sending SMS text");
  Serial.print("Sending: ");
  Serial.print("AT+CMGS=\"");
  Serial.print(number);
  Serial.println("\"(CR)");
  Serial.print(message); // The SMS text you want to send
  Serial.println("(EM)");

  ATCommandStream.print("AT+CMGS=\"");  // Send SMS
  ATCommandStream.print(number);
  ATCommandStream.print("\"\r"); // NOTE: Command ends with CR, not CRLF
  ATCommandStream.print(message); // The SMS text you want to send
  ATCommandStream.write(26); // ASCII EndMessage (EM) CTRL+Z character

  WaitForResponse();
}

what do you see in the Serial monitor? (copy and paste the text within code tags)

Serial monitor display:

Sketch started.
ATCommandStream started at baud rate 9600
Sending command: "AT+CMGF=1"
Received: "AT+CMGF=1"
Did not receive OK.
Sending SMS text
Sending: AT+CMGS="+17xxxxxxxx3"(CR)
New Message(EM)
Received: "AT+CMGS="+17xxxxxxxx3"
New Message
"
Received: "+CMGS: 5
"
Received: "
"
Received: "OK
"
Sending command: "AT"
Received: "AT
"
Received: "OK
"

Same message was sent as leading post.
Not sure why the "AT+CMGF=1" did not receive an OK.

If you use a simple code to manually send commands, does the AT+CMGF=1 returns anything ?

Yes, the AT+CMGF=1 returns OK in all other code I'm testing, like:

    while (!sent && attempts < maxAttempts) {
    gsm.println("AT+CMGF=1"); 
    delay(200);
    gsm.print("AT+CMGS=\"");
    gsm.print(number);
    gsm.println("\"");
    delay(100);
    gsm.print(message);
    delay(100);
    gsm.write(26);
    delay(4000); 

May be you just need more time for the module to boot up and be ready to accept commands

What happens if you change the delay(200); into delay(5000); at the beginning of the setup

Edit

Ok ok, At the the beginning, testing now:

void setup()
{
  // start th serial communication with the host computer
  Serial.begin(9600);
  while (!Serial);
  delay(200);
  Serial.println("Sketch started.");
  ATCommandStream.begin(ATComandBaudRate);
  Serial.print("ATCommandStream started at baud rate ");
  Serial.println(ATComandBaudRate);
  delay(5000);
  // Set the SMS output to text mode
  SendShortCommand("AT+CMGF=1");
  SendSMSMessage(DestinationNumber, SMSMessage);
  SendShortCommand("AT"); // Just checking that it responds with OK
}

Yes but you still send over the serial line right away. Maybe the module on the other side is not ready to listen

Is echo disabled on your gsm module? SoftwareSerial can’t read and write at the same time.

Serial mon output with 5000 delay:

Sketch started.
ATCommandStream started at baud rate 9600
Sending command: "AT+CMGF=1"
Received: "AT+CMGF=1
"
Received: "OK
"
Sending SMS text
Sending: AT+CMGS="+17xxxxxxxx3"(CR)
New Message(EM)
Received: "AT+CMGS="+17xxxxxxx63"
New Message
"
Received: "> "
Did not receive OK.
Sending command: "AT"
Received: "AT"
Did not receive OK.

Restarting module to retest again...

It worked after restarting Nano and SIM7670G module.
Here is Serial monitor output:

f���`f���~�������`�~`fSketch started.
ATCommandStream started at baud rate 9600
Sending command: "AT+CMGF=1"
Received: "AT+CMGF=1"
Did not receive OK.
Sending SMS text
Sending: AT+CMGS="17xxxxxxxx3"(CR)
New Message(EM)
Received: "AT+CMGS="17xxxxxxxx3"
New Message
"
Received: "+CMGS: 8
"
Received: "
"
Received: "OK
"
Sending command: "AT"
Received: "
"
Received: "+CPIN: READY
"
Received: "
"
Received: "SMS DONE
"
Received: "AT
"
Received: "OK
"

But still sending this, reason for this post:

Can you remove most of the Serial prints to limit the interruptions and also add a string to wait for instead of the hardwired « OK » so that you really await what’s needed like the > for when sending the sms