我创建了一个PHP类,它大大简化了使用PHP发送文本消息的过程.我知道这不是一个"帮助我!" 类型问题本身,但我想分享代码,因为我发现它是非常有用的.您可以随意使用代码执行任何操作.你甚至可以到处告诉你做过的人.如果出现任何问题,请不要指责我.
无需再费周折:
<?php
// Carrier email suffixes
define('ATT', 'txt.att.net');
define('SPRINT', 'messaging.sprintpcs.com');
define('TMOBILE', 'tmomail.net');
define('US_CELLULAR', 'email.uscc.net');
define('VERIZON', 'vtext.com');
define('VIRGIN_MOBILE', 'vmobl.com');
// Message parameters
define('MAX_SMS_LENGTH', 140);
define('DEFAULT_CELL_SENDER', 'sender@example.com');
class Cell
{
public static function send($pNumber, $pCarrier, $pMessage)
{
// Keep a notifier of whether the message was sent or not
$Success = true;
// Define the recipient address
$Recipient = $pNumber . '@' . $pCarrier;
// Find out how many message will have to be sent
$MessageCount = ceil(strlen($pMessage) / MAX_SMS_LENGTH);
for ($i = 0; $i < $MessageCount; $i++)
{
// Calculate the subset of the entire message that can be sent at once
$StartIndex = $i * MAX_SMS_LENGTH;
$Message = stripslashes(substr($pMessage, $StartIndex, MAX_SMS_LENGTH));
// Display page numbers on messages that span multiple iterations
if ($MessageCount > 1)
{
$Message .= ' (' . ($i + 1) . '/' . $MessageCount . ')';
}
// Send the message
$Success &= mail($Recipient, null, $Message, 'From: ' . DEFAULT_CELL_SENDER);
}
return $Success;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
它会自动处理多个文本消息的分页.此外,我知道短信通常限制在160个字符,而不是140个字符.我将限制减少了20个字符,以便为电子邮件地址留出空间.
我希望这可以帮助那里的人.干杯!
| 归档时间: |
|
| 查看次数: |
544 次 |
| 最近记录: |