Send Text Message From PHP Phone Number - php

I'm a bit confused here and due to the keywords involved Google has not been much help. I want to send text messages from my website to users with cellphones via SMS. Sounds simple enough using things like 1234567890#carrier.com with php mail scripts right? Well one thing I've encountered before is that sometimes it sends an MMS(multimedia message) instead of a regular text message.
What I am trying to find out(using keywords in Google, send text message phone number php) is how to get a phone number for your website? I've seen sites that actually have a phone number linked to the site that you can receive messages from.
Example Facebook uses 32665 to send and receive messages on.
How do you accomplish this?
Is it any way possible using the 1234567890#carrier.com with php mail to strictly only send a text message and not multimedia?
Example php mail script using a phone number:
<?PHP
mail("5558349324#vtext.com", "", "Test SMS", "From: RRPowered <test#rrpowered.com>rn");
?>
Also thanks to any help ahead of time, because I am really really confused at this point.
UPDATE: I just tried using the regular PHP mail functions to a phone number and it appears to cut the messages off at around 140 characters(I believe that is standard message size).
UPDATE 2: Before anyone mentions API like Text Magic, I am aware of those things, BUT I am wanting to build my own code instead of integrating someone elses API(if possible).

Related

Website Form to SMS

I would like to add a "Text Me" form to my website that will allow users to type their Name, Phone Number, and a Message that i receive on my phone via SMS. I would prefer something easy and free, perhaps a code snippet. Does anybody have any suggestions for a "Text Me" form or a service that provides one?
A quick google-search reveals (at least for German) customers: lox24.eu - they have a VERY simple way for sending SMS:
https://www.lox24.eu/API/httpsms.php?konto=<yourId>&password=<yourPassword>&service=<yourTariff>&text=<SMSText>&from=<yourSender>&to=<receivingNumber>
All you have to do, would be creating a short text from the $_POST variables of your form and then do a HTTP-request (preferrably cURL) from within the PHP-page that receives the form-data.
You might try to google for "websms api" to find a SMS-gateway in your country that provides the services you need.
HTH
^KMP
I recommend If This Then That. You can hit a simple web backend, supplying a JSON payload, and IFTTT will then send an SMS to your cell with the payload text, or whatever text you want.
Maker channel: https://ifttt.com/maker
SMS channel: https://ifttt.com/sms
IFTTT will abstract away the specifics of how the text is sent (doesn't matter what carrier, what country, etc.) and it will (probably) break the text into properly sized chunks. If not, you might have to do that yourself, though - just use substr() to divide the text payload into chunks of 160 characters and hit the backend as many times as necessary for the message to be sent.
You can use raw cURL to hit the backend, but I find that Guzzle is a great PHP library for making web requests of any kind.
A lot of mobile carriers in the US (not sure about abroad) will have a text equivalent email. For example, AT&T will text you if you send an email to yourmobilenumber#txt.att.net. Its free.

Random errors appear in alert emails - how could this happen?

I'm the webmaster and lead developer for a set of websites providing assessment services. I have no background in server administration and am ashamed to say that being a sysadmin is pretty intimidating to me; I'm comfortable with basic command line, but I'm frequently overwhelmed by all of the myriad configurations needed to get a standard LAMP stack working with several subdomains and an email server running.
I've finally reached something that thoroughly stumps me. Let me first list our basic server configuration:
VPS running Ubuntu 12.04
Standard Apache, PHP, Mysql, BIND services
1 main domain + 4 subdomain websites configured in the above services, two of which make extensive use of mysql databases having 300-400 tables each
Citadel email server (a choice I'm increasingly come to see as a mistake)
Swiftmailer (for sending automated emails from PHP)
Some of our pages (relating to account and assessment management) send alert emails to the test taker, coach, and/or us, when a specific action is taken. The text for these emails is fetched from a "boilerplate" table in the mysql db. Then in PHP, a couple "placeholder" tags are swapped out for test taker name, date, etc. The PHP code then uses Swift Mailer to send the prepared text as an email, with our local Citadel email server, to a given set of (usually external) addresses. We are CC'd on most of these emails, to provide us with a second record of all of the automated messages we've sent.
(Feel free to skip over this) Here's the gist of my code that fetches and processes the text, and sends it as an email:
$subject = $bp['email_report_title'];
$subject = str_replace("[NAME]", $name, $subject);
$subject = str_replace("[INSTRUMENT_NAME]", $instrument_name, $subject);
$body = $bp['email_report'];
$body = str_replace("[NAME]", $name, $body);
$body = str_replace("[INSTRUMENT_NAME]", $instrument_name, $body);
$body = str_replace("[INSTRUMENT_BASE]", $instrument_base, $body);
sendEmail($email_to, $email_from, $email_cc, $subject, $body);
In a nutshell, here's my problem: the emails we see (as a result of the above events) sometimes have typos appear in them -- inconsistently. Here are a couple images as example, showing a set of auto emails generated & sent on the same day using the same PHP code drawing from the same (unaltered) string in our database. These are screenshots from our internal (CC'd) account, viewed in Mac Mail; but based on test taker complaints, all email recipients appear to be seeing the same text. In the first image, note the differences in spacing in the titles.
In the second & third images, note the incorrect link (two periods at the end). I have checked the PHP code and the mysql source text, and can find absolutely no reason that the login link would be changed to have two periods in that spot. EDIT: Based on commenter request, I've copied the full-length email text below the images.
[name removed]'s LSUA report has been completed. To view the report, click on the following link:
https://www.devtestservice.org/security/logIn..php
Once you have logged in, click the LSUA icon, and follow the link that says "View your report".
The commenting on our assessments is done by highly trained human beings who occasionally make mistakes. If you notice any inconsistencies or errors in your report, please let us know and we will make prompt repairs. If you have any problems logging in or viewing your report, please contact us using the following link:
https://www.devtestservice.org/contactus.php
Lectica, Inc.
Northampton, MA 01060
Our mission is to develop standardized, formative, and diagnostic developmental assessments of the knowledge and skills required to meet the challenges of the 21st century. Our aims are threefold: (1) to build engaging, educative, and feedback-rich developmental assessments and learning resources for K-18 students and their teachers, (2) to create equally rich assessments that diagnose learning needs and support the development of adults (in the workplace and beyond), and (3) to build (and share) knowledge about learning and its role in the future of society.
This message contains PRIVILEGED AND CONFIDENTIAL INFORMATION intended solely for the use of the addressee(s) named above. Any disclosure, distribution, copying or use of the information by others is strictly prohibited. If you have received this message in error, please advise the sender by immediate reply and delete the original message and any and all attachments. Thank you.
Sorry for the long background explanation. But my question is simple: How could these typos happen? At what point in the workflow could a (verified consistent) mysql data cell, fetched and processed by (straightforward) PHP code, plopped into a standard PHP -> SMTP mailer plugin, lead to the creation of an email that appears to have random typos?
I'd love any ideas you have, and I'm glad to provide more detailed code if needed. Thanks in advance.
EDIT: Thanks to Eggyal for pointing out that only the longer subject lines have the spaces removed. This pattern holds true across other older emails too. Also, I've provided the full text of one email alert above; note the '..php' at the end of the link. All emails have the same signature line, and all alert emails of the same type have the same length body (plus or minus 15 characters for different instrument names and test taker names).
I have experienced very similar behavior in emails when using plain php's mail() function and postfix smtp server. It took me weeks to figure it out why HTML in those emails is randomly corrupted: it was long lines in the email source, exactly as #eggyal writes in the comments.
RFC2822 states that lines should be no more than 78 chars. To comply, SMTP server inserts newlines at selected places, however this can easily corrupt space-sensitive content. Check the Content-Transfer-Encoding header in your emails, I assume it will be either 7bit, 8bit or quoted printable.
Try changing it to base64 - that's the easy way. I'm not familiar with Swift Mailer but I guess this could work:
$transferEncoding = $message->getHeaders()->get('Content-Transfer-Encoding');
$transferEncoding->setValue('base64');
Alternatively, you can try to wrap lines manually before handing over to SMTP server.

Extract email message itself from all its prior messages and meta data (Sendgrid Parse API/PHP)?

I'm using Sendgrid and their Parse API to send/receive email. The Parse API allows one's web app to receive email as a $_POST but the problem is that in the $_POST I want to be able to extract the message itself from its prior messages and meta data that get chained along.
To show you what I mean in the following picture, i'd just like to capture the text, "trying sending from 12373 to 12373 from GMAIL" and not all the junk below it. If that is not possible, does anyone have any suggestions on how to parse the email body ($_POST['text']) such that I can separate out the message itself?
The problem is see is that depending on the email client (gmail, outlook, etc.), It's not clear to me that the date information, in this case: "On Wed, Jan 23, 2013...", will allows follow the message itself. If all email client's put the date beneath the message, then it would seem I could design a fancy regex to look for a line break followed by a date or something. Thoughts?
You have a couple of options:
1) Insert a token that splits the emails
You could do something like --- reply above this line --- and then cut out everything below that token.
2) Use an email reply parsing library
There is a really good one done by github, but it's in ruby. There's a php port though that might be good for what you need:
Fully working code:
<?php
require_once 'application/third_party/EmailReplyParser-master/src/autoload.php';
$email = new \EmailReplyParser\Email();
$reply = $email->read($_POST['text']);
$message=$reply[0]->getContent();
$message=preg_replace('~On(.*?)wrote:(.*?)$~si', '', $message);
//Last line is needed for some email clients, e.g., some university e-mails: foo#bar.edu but not Gmail or Hotmail, to get rid of "On Jan 23...wrote:"
//This failure to remove "On Jan 23...wrote:" is a known issue and is documented in their README
?>
There's simply no guaranteed way to parse quoted message threads from an email message, so you won't find a regex or any other code that will work in all cases. There's no standard to define formatting of replies, and as you've already observed different mail clients use different conventions. Many, in fact, will allow the user to edit the quoted text. Also, users can paste in unrelated messages, with or without headers, resulting in a mix-and-match of formats.
If you can record and keep the history of all messages as they are sent and received, then you can (usually, but not always) use the In-Reply-To header (see RFC-5322) to locate the previous message by matching it's Message-ID header, and do a diff on the body and remove duplicate text runs. It's apparent that some email systems do this to improve their presentations, but I'm not aware of any available open source code.
// cut quoted text, https://regex101.com/r/xO8nI1/5
$message = preg_replace('/(On\s.*<\n){0,1}(.*\n(\n){0,1}((^>+\s?.*$)+\n?)+)/mi', '', $message);
How about replies in languages other than English? We came out with solution to add marker, but instead of translating it for every email (based on user's language) we put some invisible characters into it (zero width space U+200B , to be precise). Basing on "On..." regexp it's error prone, it can easily cut some email content.

Including links in text message using twilio API

How can I include a url in an outgoing text message using twilio API? I tried, but the message was not sent. Is there a specific format? syntax?
Update:
Here's the code: (I am using the php api)
Perhaps the problem is with using a variable in the link? or maybe in a different format?
$sms = $client->account->sms_messages->create(
"xxx-xxx-xxxx",
$send_to_number,
"Hey $var1. words words $var2. via example.com.
see: https://graph.facebook.com/$fb_id/picture");
The example.com link works perfect, so do $var1 and $var2. But when adding the last link which includes a variable (and it is from facebook graph api, but I don't think that matters), then the message is not sent. Is there any way to solve this without url shortener?
SMS messages sent via Twilio are limited to 160 characters because carriers will break messages up into 160 character sized chunks. These chunks doen't necessarily arrive in order, so it is recommended to send some sort of pagination along with the message if you expect it to be over 160 chars.
https://www.twilio.com/help/faq/sms#sms-technical-3
The official Twilio PHP helper library will error out if you attempt to send a message longer than 160 characters.
Something else to watch out for: if you break your message into two lines like you have done here, PHP will include the characters you used for indentation in the message, so the above code will produce an SMS message that looks like this:
Hey $var1. words words $var2. via example.com.
see: https://graph.facebook.com/$fb_id/picture
The message is too long.
There is a limit of characters in a twilio message. This has nothing to do with the variable. It now works, just shortened the text.
Twilio recently updated their api. You can now send messages longer than 140 characters.
Such messages will be automatically split into pieces for each carrier.

possible to make text messaging with php have a constant "telephone number" value?

i have an iphone 3g and can successfully send text messages using the PHP mail() function. My issue is that for each message i receive, the "telephone number" associated with the incoming text message changes each time. If possible, I would like to somehow make this number constant so that I can take advantage of iphone's ability to aggregate all text messages from the same telephone number -Otherwise my iphone would be cluttered with messages. Is there a way to do this?
an example of the numbers I receive would be 1(410) 000-001, 1(410) 000-002, 1(410) 000-003, etc... can i make this constant somehow?
$message = stripslashes("new user just joined!");
mail("8185551111#txt.att.net", "Subject", "$message");
please let me know! thanks...
This is likely more to do with ATT than your PHP code.
This is a limitation of the carrier's email-to-SMS gateway. There's no way to guarantee which number the message will come from.
SHAMELESS PROMO: You could use a service like Twilio (whom I work for) and get a phone number for $1/month which you would be able to always send from. There's a ton of PHP sample code to get you started. There are other SMS APIs out there as well so look around and find the one that you like best.

Categories