简体   繁体   中英

PHPMailer - Multiple text messages to single recipient

I have a portion of my project that grabs some customer information from a DB and sends a text-message to a salesman, using PHP Mailer. Some of the customer info included:

  • Name
  • Phone
  • Phone 2
  • Address
  • City
  • State
  • Zip
  • Notes

As you can imagine, 160 characters won't cut it. I need to be able to send at least two text messages to the same number within the same function.

I have a single text message working, using PHP Mailer. I will post the relevent code below:

db_functions.php:

function send_text($name, $message){
require 'class.phpmailer.php';

$to = 'xxxxxxxxxx@vtext.com';

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx@gmail.com";
$mail->Password = 'xxxxxxx';
$mail->SetFrom('xxxxxx@gmail.com');
$mail->Subject = $name;
$mail->Body = $message;
$mail->AddAddress($to);
$mail->Send();
$mail->ClearAddresses();
return;
}

assign_lead.php:

include 'mysql_login_pdo.php';
include '../functions/db_functions.php';

if (!isset($_POST['leadID'])) {
    return;
} else {
    $leadID = $_POST['leadID'];
}
if (!isset($_POST['salesID'])) {
    return;
} else {
    $salesID = $_POST['salesID'];
}

//DB FUNCTIONS
db_assignLead($leadID, $salesID);
$message = db_assignLeadNote($leadID);
$name = db_assignLeadName($leadID);
db_assignAddNote($leadID, $message);

//-------------------------This is the problematic area---------------------
//SEND TEXT MESSAGE(s)
    send_text($name, $message);
    $message = '8104124200_230 N Main St_Davison_48423';
    send_text($name, $message);

As you can see, I want a text message to send to a salesman with the customer's name and a note about the customer. Then, I want to send a second text message with the customer's name and address information. I've used a placeholder of '8104124200_230 N Main St_Davison_48423' for now, but it will be replaced by a function that searches for the address info in the DB.

The first text message sends fine, but the second refuses to send. I made it work once by using a 20-second sleep, but from what I've read, it may be unnecessary. Also, the 20-second sleep was completely unreliable.

As always, I appreciate any help.

I ended up doing the following, which worked. I'm hoping it won't cause issues down the road, but if it does, I'll come back and update this post.

Basically, instead of calling the send_text() function twice, I sent the message twice within the same function:

function send_text($name, $message){
include 'class.phpmailer.php';

//$to = $_POST['to'];
//$password = $_POST['password'];
$to = 'xxxxxx@gmail.com';

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx@gmail.com";
$mail->Password = 'xxxxxx';
$mail->SetFrom('xxxxxx@gmail.com');
$mail->Subject = $name;
$mail->Body = $message;
$mail->AddAddress($to);
$mail->Send();

//Send the second message
$mail->Body = 'Testing second message';
$mail->Send();
//End Second message

return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM