How can I send sms via php's mail function()?

痴心易碎 提交于 2019-12-04 18:35:05

OK firstly you're allowing a random stranger (who submits the POST request) to set the FROM address of the email message by accessing $_POST['from'] and using that as the FROM address. That's not going to work if (as others have suggested) you're setting a FROM address that is not a valid sender address according to your mail server -- and according to whatever upstream mail server you're using. So, for example, in my own network I can send an email "From: me@mydomain.com" but if I send something that is addressed "From: you@fakedomain.com" then it will probably bounce or be thrown out. So you probably need to hard code that From address to whatever your IT guys say, not use the $_POST variable.

The fact that your email is actually going out as "From: www.data@servername" probably means a couple of things:

  • Your PHP is configured so that the mail() function calls the standard sendmail command installed on most Unix/Linux systems.
  • Your Apache server, that is running the PHP processes, is running as the "www.data" user on that system.
  • The "www-data" user on that system is not authorized to set an alternative FROM address, and so sendmail is forcing the FROM address to be www.data@servername, regardless of what you tell it.

Your system admin needs to tell you what address to hard code as the FROM address on that system. In addition, he or she needs to configure the mail system to allow www.data to set its own FROM address. Assuming that you are using actual real sendmail (and not something else configured to work like sendmail, such as postfix), then that would be done by adding a line "www.data" to the file /etc/mail/trusted-users or whatever trust file is set up on that system. The clue to that is to find the sendmail.cf file in use on the system and look for a line beginning with "Ft", e.g.

Ft/etc/mail/trusted-users

More information can be found in the sendmail docs, e.g. on a Red Hat / CentOS system with the sendmail-cf package installed there will be a /usr/share/sendmail-cf/README file on that system with useful information in it about trusted-users.

Machavity

First off, I would highly suggest you read up on the limitations on email-to-SMS. It's an older thread but I have no reason to believe that the limits are any better now. Remember, SMS is not free to some users so carriers have some incentive to limit what you can do.

Second, the relevant data for your email would be in your mail server logs. Too many people think that because mail() returned true, that the email was sent. All mail() can do is tell you if sendmail (or whatever MTA you use) responded with. If you check out the mail log (typically in /var/log/maillog for most Linux distros) and look for the address you sent to, you will see if sendmail was actually successful in sending the email or if the recipient server rejected it. I'm willing to bet, based on my first point, that you got filtered in some way. If you don't have a properly formatted from address, your server will use username@server-hostname which tends to look like apache@23247244efg.myhostingcomapny.com (major YMMV, as some virtal servers don't even have a FQDN)

Third, you should consider a SMS service. Amazon, for instance, has its SNS service, where you pay-per-usage (there are other services with monthly fees, etc). The advantage here is clear, because you're using something designed to push notices via SMS, instead of a weak (and likely deprecated) method of achieving the same thing. And, this way, you don't have to deal with any headaches of making sure your email is formatted so the carriers will accept it.

If you know the provider of the phone number you can use the email to sms service by most companies.

Ex:

Rogers Wireless: [10-digit phone number]@pcs.rogers.com

Fido: [10-digit phone number]@fido.ca

Telus: [10-digit phone number]@msg.telus.com

Bell Mobility: [10-digit phone number]@txt.bell.ca

Kudo Mobile: [10-digit phone number]@msg.koodomobile.com

MTS: [10-digit phone number]@text.mtsmobility.com

President’s Choice: [10-digit phone number]@txt.bell.ca

Sasktel: [10-digit phone number]@sms.sasktel.com

Solo: [10-digit phone number]@txt.bell.ca

Virgin: [10-digit phone number]@vmobile.ca

Cheers

There are 2 solutions (that I know):

A SMS Gateway Provider or Using a GSM modem.

For the first one, it's the simplest one, you have to use an API of your provider. It can also be through SMTP. The process depends on the provider. Often, you have to provide some extra headers.

Using a GSM modem (i've tried it), you have to launch some commands to your modem, it's not so difficult but, the modem is about 350$

First, check to see if your server ip is on any blacklists. If so, try to get removed from them. Second, Use a valid from address that indicates its coming from your server, don't spoof. I have had no problem sending email from my server when I did that.

(please note the end user may be charged for these messages, and the recipient may need to enable this feature)

The problem is most likely due to the fact that sprintpcs DNS records are broken. This may be deliberate to prevent people abusing their service. You may be able to get around this by using an SMTP client connecting directly to the nonauthoritative address.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!