【问题标题】:PHP integration with Clicatell SMTP APIPHP 与 Clicatell SMTP API 的集成
【发布时间】:2015-03-30 16:29:15
【问题描述】:

我正在尝试发送一个 php,它将向 Clicatell SMTP api 发送电子邮件。这个想法很简单 - 使用 PHP SOAP 客户端调用 SQL 服务器,获取回复并将此回复插入到发送到 Clicatell 服务器的电子邮件正文中。

<?php

$to = 'sms@messaging.clickatell.com';

$today = strtoupper(date('dMy')); 
$subject = 'Test sms sent on ' . $today;

// API request and response goes here 
//this is how the response from the api looks like

$response ='To:4412345677693\r\nTo:4412345665693\r\nTo:4412375677693\r\nTo:4463879456776\r\n';  

$message = 'api_id: 12345678\r\nuser:user1234\r\npassword:PxAxSxSxWxOxRxD\r\n' . $response . 'Reply: youremail@yourdomain.com\r\ntext:Test Message Using Clickatell api\r\n'; 

$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type:text/html;charset=UTF-8' . '\r\n';
$headers = 'From: no-reply@example.com' . '\r\n' .

mail($to,$subject,$message,$headers);
?>

这应该有望生成像这样的纯文本电子邮件:

To: sms@messaging.clickatell.com

api_id: 12345678
User: user1234
Password: PxAxSxSxWxOxRxD
To:4412345677693
To:4412345665693
To:4412375677693
To:4463879456776
Reply: youremail@yourdomain.com
text: Test Message Using Clickatell api

但是,我将所有内容都集中在一行上。帮助将不胜感激。

【问题讨论】:

  • '-引用的字符串理解/尊重转义序列。所以 \r\n 你有文字反斜杠和文字 r/n 字符。 '-quoted 字符串中唯一可接受的转义是 \` itself. Use "`。

标签: php email smtp clickatell


【解决方案1】:

只需将引号更改为双引号即可正确处理控制字符:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8\r\n";
$headers .= "From: no-reply@example.com\r\n";

这同样适用于 $response 和 $message。 有帮助吗?

【讨论】:

    【解决方案2】:

    我相信这是因为您在标题中设置了Content-type:text/html;charset=UTF-8,因此您必须使用&lt;br&gt; 来获得换行符。

    由于您想要纯文本而不是 HTML,因此您需要使用更合适的内容类型。例如,尝试使用Content-type: text/plain; charset=iso-8859-1


    要考虑的另一件事是(根据 php 手册)某些继电器会“错误地”将文本中的 "\n" 转换为 UNIX 系统上的 "\r\n"。建议使用 PHP EOF 常量而不是 "\r\n"

    http://php.net/manual/en/function.mail.php


    同样使用单引号字符串,您会发现'\r\n' 不会将\ 视为转义字符。您可能需要使用双引号字符串(例如 "\r\n")来获得所需的行为。

    http://php.net/manual/en/language.types.string.php

    【讨论】:

    • 这也是我的想法,但根本没有帮助。仍然在一条线上作为电子邮件进行低谷
    • 您是否检查过使用"\n""\r\n"'\r\n' 与使用PHP EOF 常量?答案更新了一些关于此的附加说明,另请参阅更新答案中引用的 PHP 手册页。
    • 嗨,我已将 ' 更改为 " 并且有效!非常感谢。我将继续使用 " 而不是 '
    猜你喜欢
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 2021-06-10
    相关资源
    最近更新 更多