PHP电子邮件中的链接与AngularJS断开

时间:2018-03-22 15:21:46

标签: php angularjs

我有一个使用PHP文档发送电子邮件的AngularJS应用程序。 电子邮件正文包含两个指向用JS变量填充的图像的链接。

大部分电子邮件都很好,链接也很有用,但在其中一些邮件中,链接(两者或其中一个)都会破碎,看起来像这样:

https://blabla.com/register/uploads/Frankfurt2018-22-03-2018-16-07-52.!

或者像这样:

https://blabla.com/register/uploads/KoelnerListe2%21

或者像这样:

https://blabla.com/register/upload!

它奇怪的原因有时是两个链接,有时只有一个,大多数时候都是正确的。

链接变量来自Angular应用程序,如下所示:

$scope.sendapplication = function(){

  $scope.photoor = "https://blabla.com/register/uploads/"+$scope.photoor;
  $scope.photosmall = "https://blabla.com/register/uploads/"+$scope.photo;

  $scope.exhibitor = {
    'img':$scope.photosmall,
    'imgoriginal':$scope.photoor,
  };

  var $promise=$http.post('emailtest.php',$scope.exhibitor); 

  $promise.then(function (data) {
    ...
  });

};

在php文件中我这样做:

$contentType = explode(';', $_SERVER['CONTENT_TYPE']); // Check all available Content-Type
$rawBody = file_get_contents("php://input"); // Read body

$data = array(); // Initialize default data array

if(in_array('application/json', $contentType)) { 
  $data = json_decode($rawBody); // Then decode it
  $photo = $data->img;
  $photooriginal = $data->imgoriginal;
} else {
  parse_str($data, $data); // If not JSON, just do same as PHP default method
}

header('Content-Type: application/json; charset=UTF-8');
echo json_encode(array( // Return data
  'data' => $data
));

$sabine = 'blabla@gmail.com';

$headerss = "From: ".$galleryname."<".$email.">\r\nReturn-path: ".$email."";
$headerss .= "Reply-To: ".$galleryname."<".$email.">";
$headerss .= "MIME-Version: 1.0\r\n";
$headerss .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$recipient = $sabine;

$subjects = "Registration for ".$fairumlaut." - ".$galleryname.""; 
$bodys .= "<p><strong>Original photo</strong>: <a href=".$photooriginal.">Link</a></p>";
$bodys .= "<p><strong>Web resized photo</strong>: <a href=".$photo.">Link</a></p>";
$bodys .= "<p></p>";

mail($recipient, $subjects, $bodys, $headerss);

什么可能导致这种奇怪的行为?

1 个答案:

答案 0 :(得分:0)

将链接包装在urlencode函数中。这将解决您的问题。

更新:或者如果我读了你的代码,我会看到链接来自JS。尝试encodeURI()..;)