0

我正在尝试发送一个 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

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

4

2 回答 2

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。它有帮助吗?

于 2015-03-30T18:48:21.600 回答
0

我相信发生这种情况是因为您正在设置Content-type:text/html;charset=UTF-8标题,因此您必须使用<br>来获取换行符。

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


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

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


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

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

于 2015-03-30T16:49:45.610 回答