PHP Script Delaying SMS Messages Being Sent? - php

I am currently running a PHP script that queries the database and then sends text messages (via carrier's email-to-sms gateway).
In other words, I have the phone numbers stored in the DB as:
1234567890#txt.att.net
1236547890#vtext.com // etc...
My method for sending these is using the Swift Mailer PHP Library. I am sending the message using "SMTP", and using the SMTP account from my Host Provider to send it out.
Typically, using this SMTP account, I will send no more than 30-40 text messages in 1 minute.
Here's my question:
I have noticed that when I send these text messages, many times it appears to work fine, however, lately it appears that some users receive the text messages maybe an hour (or several) later.
I understand that once it leaves my server, it is up to the carrier to decide when to send it. However, could it possible that something in my script causes the issue?
For example, I have noticed that in some parts of the DB, a user's number will be displayed as:
1234567890 //notice, no '#vtext.com' or whatever
Could issues like this cause the delay of other emails/texts?
Finally, does anyone know of any script or way to build a script that can query a cell phone number and determine who the carrier is?
Many thanks on this!

This question can only be answered if you provide us the code, what you have done. I can give you a tip. Try to send the message with the previous timestamp so that your carrier server processes them first.

Related

Problem sending emails to several recipients

I started managing a website with a member list of over 50,000 registered members.
I am having this problem:
Each time i try sending the emails, the page runs for several minutes (more than 20 minutes) and eventually ends with up 'Internal Server Error'.
Now, i do not know how many mails that were sent or if any was sent at all.
How do i send the emails:
I select all emails from the database, run a loop and send one after the other. I chose this option because:
i do not want to add several emails in 'To' field, so people's privacy will be maintained.
Adding the emails in Bcc will make it appear as a spam.
Please how can i handle this.
Thank You
All you need to do is break the job into manageable chunks, instead of reading in all 50,000 emails and trying to send them all.
Read in only X emails from the database at a time.
Send that batch of X emails individually (If X = 10, you will send 10 separate emails).
Mark each email as "sent" as soon as each email is sent, otherwise, if there is an error, you may want to increment "send attempts" on that particular email.
After the X emails have been sent (or attempted), update your statistics to indicate that X emails have been sent.
Go back to 1.
I suggest starting with X = 1
I did this by making a script that calls itself with the id of the user it last processed.
E.g. sendmail.php will then call sendmail.php?id=1 which will then call sendmail.php?id=2 and so on.
That will fix your timeout issue.
Within the script you can echo out the results of the mail command. If its false, then you could echo out an error.
I also added a field in my user table for mail send date and time, so i could have a confirmation of where the script got to incase the browser died.
Your sql for getting the next user to send email to would then be something along the lines of
Select useremail from users where sendmaildatetime is not null limit 1
Assuming your default for the sendmaildatetime column is null.
Sorry if im not detailed enough, im writing this from my iphone and its a PITA :)
#hamlin11 response is right, if you want to manage such a big email sending you MUST do it in small chunks and manage this task as an asynchronous task.
To Manage asynchronous tasks you'll have a lot of way, a subprocess check on all your requests that some async jobs are waiting, a separate cron job, a call on a cron.php script from a crontab, even psynnott answer, with an external script relaunching himself at the end.
But you could also use the right tool for the right task, if you have some control on the system under your website. Send one simple email to a mailing list manager that would do the job for you. This would only imply you create the right user list in the mailing list manager. External mailing list manager are for example mailman or sympa. These tools contains robots that you can talk with to feed mailing list recipients.#psynnott answer can be seen as an external PHP script performing very simple mailing list manager tasks. If you want to alter content of the email depending on some user parameters you'll quite certainly have to write your own separate process managing the tasks.
But you could also search for web services handling this job for you. Spam management is a hard job and managing an official big mailer is an hard job as well, but this is not free, of course.

PHP - Bulk mailing and checking for server responses

We send a lot of emails to a lot of our users (ranges in 20k+ per day). One of the major problems we face are invalid or dead emails - sometimes our users delete their accounts, change their email address without updating their profile, or our email database builders simply caught emails that are invalid or no longer active. These unresolved returned status messages keep not only piling up on our webmaster account, but also waste precious server resources and flag us as spam more often because of the repeated attempts.
Now, while our mail server is set up to keep trying to send an email to an address that returns "temporarily unavailable", I would like to be able to receive status messages into PHP immediately after sending. For example, when my Sender class sends an email, I would like to know the status message that was returned - whether the email is no longer active, or the server does not exist, or the email was simply moved to another address.
Naturally, I want to be able to receive a status message of a delayed email as well. So if an email does not get sent because the recipient email address is temporarily unavailable, I would like to get the "temporarily unavailable" message back into Php, but I would also like the real one to get passed back once the sending succeeds (for example, if the email does go through 2 days later).
Is there a library that would help me achieve this? What are the most common approaches to this problem, if any?
Like most questions about PHP and mail this is mainly about the MTA.
Bulk emailing is a science in its own right (OK, so more like a black art) and at these kinds of volumes you need to up your game a lot if you want reasonable rates of deliverability.
But back to the question.
An awful lot of this is about how you configure your mail server. AFAIK, most MTAs will only send back a failure message when the message is removed from the queue (e.g. after the last attempt at delivery). Which gives 2 options for tracking each attempt:
1) parse the log files
2) set the number of attempts to 1 (and optionally handle re-queueing yourself).
Given that a message can fail to be delivered after it has successfully departed from your server, it makes a lot of sense to use delivery status notifications (i.e. bounce emails) to track the progress of messages - so using option 2 avoids having to build different code for handling different scenarios.
Without knowing what OS this is running on, nor which MTA, it's impossible to give more specific recommendations.
symcbean's answer gives many theorical inputs and several means to handle your case.
Besides, maybe you could have a look on how other libs or builtin functions work. For instance, you can have a look at :
PHPList
Extended PHP Mailer
i used PHPList some times ago but it was already a reliable solution. I don't know the PHP Mailer class but i may be worth a try (or a least a look on how they deal with similar issues).
Regards,
Max

Unread email notifier, most practical approach

I'm in the process of writing a small php-cli script that will loop over my personal inbox and then send me an SMS via a gateway.
The question I have is:
As will have the script launch via cron every 10 minutes, if there is an email sitting in my inbox that is not read before the next script launch then I will receive 2 sms.
Does any one (pseudocode will do) have any idea what the best practice would be in php5 to ensure only 1 SMS is sent?
What I am currently learning towards is storing the message ID in a sqlite DB and flagging a field whether an SMS has been sent or not - but wondering if there is an easier way?
You shouldn't need to store anything. If I understand you correctly, you simply want your program to send you a message if a new, unread email has arrived within the last 10 minutes. In that case, could you not do the following?
foreach($inbox[] as $message){
if(($message.isUnread) && ($message.receiveTime > NOW() - 10 minutes)){
$sendMessage = TRUE;
}
}
if($sendMessage){
sendMessage();
}
I don't know how you're able to access the data, so the above is unlikely to be usable as-is. Assuming you can filter your inbox's contents by read status and date arrived, however, shouldn't that work in principle?
What I am currently learning towards is storing the message ID in a sqlite DB and flagging a field whether an SMS has been sent or not
This is one of the most simple and straightforward ways to do this. Regardless of storage method, you'll have to keep the list of seen-but-unread message IDs somewhere. May as well be in a simple, self-contained SQLite file. I suppose you could just keep them in a list in a flat file. That is by definition more simple, but is it really easier?
I don't see you getting away without persisting something somewhere.
That something should be of ever-incrementing nature - message ID maybe, timestamp for sure. As long as you can look at it and determine if new messages have arrived since.
If all you wish to communicate with the SMS is that there are unread messages, I don't see the need to persist more then a single ID or a timestamp.
After a few hours sleep (up since 4am watching Australia vs Germany, World Cup) I've come up with an even better method, a bit more complex then just using a sqlite DB but has some extra benefits.
I'm using imap_mail to send the SMS to the gateway, and storing the message_id that the SMS is notifying to as a custom "X-Relationship:" header in the outgoing email and then copying the message to my Sent Items.
Then the notifier simply scans my Sent Items for the presence of the X-Relationship header within my Sent Items.
This what I have a record (Sent Item) of the email sent to the SMS Gateway - seems to be working well.

Possible to count number of messages sent via SMTP in PHP?

Bit of a long shot, but is it possible to count how many emails a given SMTP account has sent in an hour?
Say john#example.com sends emails via Thunderbird and a PHP script.
Can that PHP script first check somehow how many emails have been sent that hour?
Obviously the PHP script can easilly keep a tally of how many emails it sends, but no way of knowing if John has been sending too many from his email client, or another script...
Many thanks.
If SMTP server does not have such statistics, then it is not possible to get the number.
Why are you asking, is there some authority that counts e-mails and says No when you reach the limit? If so, then ask that authority.
You can try to analyze /var/log/mail.log file. Usually it keeps all the info you need. For example, you can write a script, that gathers all data from mail.log, analyzes it, and puts it into some database table. This script could be run by cron every hour, or so (depending when you need the info)
Short answer is no (there's a lot wrong with Konstantin's reply, but criticizing it probably isn't going to help). What is the problem you are trying to solve and why do you think a PHP script is the right solution?
C.

Is it possible to send mail asycronously using PHP while giving user feedback on delivery?

Is it possible to send mail asycronously using PHP while giving live user feedback on delivery?
I have recenty written a small app for our company's intranet for sending formatted emails to customers. The interface is quite clean and only requires the input of a job number, it then builds and sends the mail. The mail, while being built, obtains a number of attachments from another server and everything is automated. The library used is PHPMailer.
Is there a way, using other technologies, possibly, but still using PHP as the main language, to show progress of the mails being sent? I have coded robust error checking to check if the mail was actually sent, etc. but i am missing a way of giving users the visual clue of actually delivering the mail to the server via a progress bar, etc.
Is this possible using PHP and something like Ajax? How would you determine the progress of the mail in transit?
I'm not familiar with PHPMailer, but you certainly need support of the library to be able to query it about the status of the emails being sent.
Given that PHP doesn't have threading, I would suggest having a database queue for deliveries, and have an external PHP process triggered from the main site (or via cron) that processes the deliveries on the side, marking on the database the current status on each delivery: NOT_PROCESSED, IN_PROGRESS, CONNECTING, CONNECTED, SENDING_DATA, ACCEPTED, FAILURE_X . You can query the database for the status on each delivery via Ajax.
If PHPMailer internally uses the standard PHP mail() function, which uses a relay SMTP server in your machine, you cannot have that much information about status (which you would have if you created the sockets yourself), you can have just three main states NOT_PROCESSED, IN_PROGRESS, FAILURE_X.
(FAILURE_X really represents many states, as it explains the reason for the failure).
A final consideration on using mail() is that the status you'll be able to know is just the status from the local SMTP relay, which will always accept very quickly, and you won't be able to tell if the mail was really delivered to the outgoing server (at least not without having to interface to it or read mailq, which are nasty things to do).
DISCLAIMER
Given that even in the good case where you know for real the status you cannot know if the email has been received on the other end, nor how much time will it take, I'm not sure how useful such a construction would be. It'd certainly be fun to program, but I doubt it'd be really useful, maybe just some eye candy with standard email disclaimers (emails can be lost in transit, if it fails try again, leave sometime before retrying) would be enough.
I think the best option here is by estimating the time. You can test how much time some 10MB mails takes to be sent, to know the receiving speed of you SMTP server. With that information you can estimate transfer time of any email based on its size, and give your client some visual distraction based on that.
If you email process can send back information, then it may to possible to update a progress bar or progress text with the messages.
This is similiar to the way Wordpress upgrades/installs work. As the process is completed, text is displaying telling each step: "Downloading xxxx.xxx.xxx", "Deactivating Plugin", "Installing Plugin", "Attempting Reactivation", Reactivation successful": something similar. You would need a listener on the client side and a sender of messages on the server: as the script executes, it sends back messages to the client.
As said before, this can only realistically go as far as your server. You can signify if the mail left your server successfully, but without some sort of email reciever conformation step, I think that is as far as you can go.
I gues there us a chance to complete it, but can't be sure that this solution is done already by someone.
My thoughts:
As I know you can work with socket in non-blocking mode stream_set_blocking()
then you could try to use that approach to send emails via that non-blocking socket.

Categories