如何通过php的邮件功能()发送短信? [英] How can I send sms via php's mail function()?

查看:189
本文介绍了如何通过php的邮件功能()发送短信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近设置了一个警报系统,当某些API被调用时,它通过PHP的mail()函数提醒我(或其他人)。



在电子邮件的工作完全罚款该函数返回true,并发送到邮箱中。



由于某些原因发送到SMS网关(即XXXXXXXXXX@messaging.sprint.com) )该函数发送它(它确实返回true),但是它从未被收到在另一端。



如果我采取相同的确切的电子邮件地址(这是一个短信网关),并通过电子邮件客户端(如zimbra或其他)发送它,它可以通过罚款,并由人员收到。



我是网页设计师不是真正的电子邮件/ IT人。我假设它与标题或这一行的东西有关,但是我不懂这个技术主题。



我跟IT人说话,他说它是错误的,看起来像标题中没有适当的发件人地址。而不是来自有效的电子邮件地址,电子邮件错误并且看起来像是来自www.data @ [server-name],而不是我通过标题发送的是一个有效的电子邮件地址。



这是我的代码片段:

 <?php 

$ carriers = array(@ messaging.sprintpcs.com);

//发送电子邮件
if(isset($ _ POST ['to'])){
$ to = $ _POST ['to'];


//从电子邮件获取
if(isset($ _ POST ['from'])){
$ from = $ _POST ['from'] ;
$ fromHeader =From:$ from。\r\\\
Sender:。$ from。\r\\\
;
}

//获取主题
如果(isset($ _ POST ['subject'])){
$ subject = $ _POST ['subject'];
}

//获取消息
if(isset($ _ POST ['message'])){
$ message = $ _POST ['message'];
}

// get cc
if(isset($ _ POST ['cc'])){
if($ _POST ['cc']!= ){
$ ccHeader =CC:$ _ POST ['cc']。\r\\\
;
}
else {
$ ccHeader =;
}
}
else {
$ ccHeader =;


// get bcc
if(isset($ _ POST ['bcc'])){
if($ _POST ['bcc']!= ){
$ bccHeader =Bcc:。$ _ POST ['bcc']。\r\\\
;
}
else {
$ bccHeader =;
}
}
else {
$ bccHeader =;


//获取回复
if(isset($ _ POST ['replyTo'])){
if($ _POST ['replyTo']! =){
$ replyToHeader =Reply-To:$ _ POST ['replyTo']。\r\\\
;
}
else {
$ replyToHeader =;
}
}
else {
$ replyToHeader =;
}

$ additionalHeaders =Content-Type:text / plain; charset = \utf-8\Content-Transfer-Encoding:8bit \r\\\
;

$ headers = $ fromHeader。$ ccHeader。$ bccHeader。$ additionalHeaders;

foreach($ carrier as $ carrier){

$ number = get_numeric_only($ to)。$ carrier;

if(mail($ to,$ subject,$ message,$ headers)){
$ response = array(response=>SUCCESS);
}
else {
$ response = array(response=>ERROR);
}

}

echo json_encode($ response);

?>

编辑:



我更改了邮件()函数通过添加第四个参数,所以看起来像这样显然这将有助于:

  mail($ to,$ subject ,$ message,$ headers,' -  f [myaddress] @ [mydomain] .com')

然后短信电子邮件将返回到标题中的from电子邮件地址,具体如下:

 原始消息收到于2015年1月28日(星期三)19:10:32 -0500 
从localhost [127.0.0.1]

-----以下地址有永久性致命错误---- -
< xxxxxxxxxx@messaging.sprintpcs.com>
(原因:550主机未知)

-----会话记录如下-----
550 5.1.2

是否这个慢跑任何人的头? .....

解决方案

首先,我强烈建议您阅读电子邮件至短信的限制。这是一个较旧的线程,但我没有理由相信现在的限制有所改善。记住,短信对某些用户来说不是免费的,所以运营商有一些动机限制你可以做的。



其次,你的电子邮件的相关数据将在你的邮件服务器日志。太多人认为,因为 mail()返回true,电子邮件已发送。所有 mail()可以告诉你sendmail(或你使用的任何MTA)是否响应。如果您检查邮件日志(通常在大多数Linux发行版中通常位于 / var / log / maillog 中),并查找发送到的地址,您将看到sendmail是否实际成功发送电子邮件或收件人服务器拒绝。根据我的第一点,我愿意打赌,以某种方式进行过滤。如果您的地址格式不正确,您的服务器将使用 username @ server-hostname ,其趋向于 apache@23247244efg.myhostingcomapny.com (主要的YMMV,因为一些病毒服务器甚至没有FQDN)



第三,你应该考虑一个短信服务。例如,亚马逊有其 SNS服务,您每次使用付费(还有其他服务月费等)。这里的优点是清楚的,因为您正在使用旨在通过短信推送通知的内容,而不是实现同样的事情的弱(并且可能被废弃)的方法。而且,这样,您不必处理任何头痛的事情,确保您的电子邮件格式化,以便运营商接受。


I recently set up an alert system whereby when certain APIs are called, it alerts me (or others) via PHP's mail() function.

On email's it works totally fine. The function returns true and it is received in the mailbox where it is sent to.

For some reason when sending to an SMS gateway (i.e. XXXXXXXXXX@messaging.sprint.com) the function sends it (it does return true) however it never gets received on the other end.

If I take that same exact email address (that goes to an sms gateway) and send it via an email client (such as zimbra or whatever), it goes through fine and it is received by the person.

I am the web designer and not really the email / IT person. I am assuming it has something to do with headers or something along this line, however I am not versed on this technical subject.

I spoke to my IT guy and he said that it is erroring and looks like there is no proper "From" address in the headers. Instead of coming from a valid email address, the email errors and looks like it is coming from "www.data@[server-name]" instead of what I am sending via the header which is a valid email address.

here is a snippet of my code:

<?php

$carriers = array("@messaging.sprintpcs.com");

//get to email
if (isset($_POST['to'])) {
$to = $_POST['to'];
}

// get from email
if (isset($_POST['from'])) {
$from = $_POST['from'];
$fromHeader = "From: ".$from."\r\n Sender: ".$from."\r\n";
}

// get subject
if (isset($_POST['subject'])) {
$subject = $_POST['subject'];
}

// get message
if (isset($_POST['message'])) {
$message = $_POST['message'];
}

// get cc
if (isset($_POST['cc'])) {
    if ($_POST['cc']!="") {
    $ccHeader = "CC: ".$_POST['cc']."\r\n";
    }
    else {
    $ccHeader="";
    }
}
else {
$ccHeader="";
}

// get bcc
if (isset($_POST['bcc'])) {
    if ($_POST['bcc']!="") {
    $bccHeader = "Bcc: ".$_POST['bcc']."\r\n";
    }
    else {
    $bccHeader="";
    }
}
else {
$bccHeader="";
}

// get reply to
if (isset($_POST['replyTo'])) {
    if ($_POST['replyTo']!="") {
    $replyToHeader = "Reply-To: ".$_POST['replyTo']."\r\n";
    }
    else {
    $replyToHeader="";
    }
}
else {
$replyToHeader="";
}

$additionalHeaders = "Content-Type: text/plain; charset=\"utf-8\" Content-Transfer-Encoding: 8bit \r\n";

$headers = $fromHeader.$ccHeader.$bccHeader.$additionalHeaders;

foreach ($carriers as $carrier) {

    $number = get_numeric_only($to).$carrier;

    if (mail($to,$subject,$message,$headers)) {
        $response = array("response" => "SUCCESS");
    }
    else {
        $response = array("response" => "ERROR");
    }

}

echo json_encode($response);

?>

edit:

I changed the mail() function by adding a 4th parameter so it looks like this as apparently this would help:

mail($to,$subject,$message,$headers,'-f[myaddress]@[mydomain].com')

Then the sms-email bounced BACK to the "from" email address as stated in the headers with the following:

The original message was received at Wed, 28 Jan 2015 19:10:32 -0500
from localhost [127.0.0.1]

   ----- The following addresses had permanent fatal errors -----
<xxxxxxxxxx@messaging.sprintpcs.com>
    (reason: 550 Host unknown)

   ----- Transcript of session follows -----
550 5.1.2 <xxxxxxxxxx@messaging.sprintpcs.com>... Host unknown (Name     server: messaging.sprintpcs.com: host not found)

Does this jog anybody's head? .....

解决方案

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.

这篇关于如何通过php的邮件功能()发送短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆