Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在多个区块中发送电子邮件?_Php_Curl_Cron_Phpmailer - Fatal编程技术网

Php 如何在多个区块中发送电子邮件?

Php 如何在多个区块中发送电子邮件?,php,curl,cron,phpmailer,Php,Curl,Cron,Phpmailer,我正在使用php邮件功能发送电子邮件,它工作正常 电子邮件正文是动态生成的,问题是电子邮件收件人只能接收160个字符或更少的电子邮件。如果电子邮件的正文长度超过160个字符,那么我想将正文拆分为单独的块,每个块少于160个字符 我正在使用CRON Jobs和curl自动发送电子邮件 当生成多个正文时,如何将每个单独的电子邮件发送给同一收件人?下面的$bodyAll表示只发送一封电子邮件,因为动态生成的内容可以容纳160个字符。如果正文内容超过160,则将不发送$bodyAll,并将$bodyFi

我正在使用php邮件功能发送电子邮件,它工作正常

电子邮件正文是动态生成的,问题是电子邮件收件人只能接收160个字符或更少的电子邮件。如果电子邮件的正文长度超过160个字符,那么我想将正文拆分为单独的块,每个块少于160个字符

我正在使用CRON Jobs和curl自动发送电子邮件

当生成多个正文时,如何将每个单独的电子邮件发送给同一收件人?下面的
$bodyAll
表示只发送一封电子邮件,因为动态生成的内容可以容纳160个字符。如果正文内容超过160,则将不发送
$bodyAll
,并将
$bodyFirstPart
发送给收件人,然后
$bodySecondPart
等,直到发送所有单独的正文文本

$body = $bodyAll;
$body = $bodyFirstPart;
$body = $bodySecondPart;
$body = $bodyThirdPart;
$mail->addAddress("recepient1@example.com");
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body</i>";
if(!$mail->send())
$body=$bodyAll;
$body=$bodyFirstPart;
$body=$bodySecondPart;
$body=$bodyThirdPart;
$mail->addAddress(“recepient1@example.com");
$mail->Subject=“主题文本”;
$mail->Body=“邮件正文”;
如果(!$mail->send())
您可以使用来检查while循环中的长度,并在每次循环迭代时发送每个块:

<?php

$bodyAll = "
  some really long text that exceeds the 160 character maximum for emails,
  I mean it really just tends to drag on forever and ever and ever and ever and
  ever and ever and ever and ever and ever and ever and ever......
";

$mail->addAddress("recepient1@example.com");
$mail->Subject = "Subject Text";

while( !empty($bodyAll) ){

  // check if body too long
  if (strlen($bodyAll) > 160){
    // get the first chunk of 160 chars
    $body = substr($bodyAll,0,160);
    // trim off those from the rest
    $bodyAll = substr($bodyAll,160);
  } else {
    // loop terminating condition
    $body = $bodyAll;
    $bodyAll = "";
  }

  // send each chunk
  $mail->Body = "$body";
  if(!$mail->send())
    // catch send error
}

优秀、简单、优雅,我没有意识到我可以在一段时间内发送邮件。非常感谢您帮助我摆脱困境,并通过这个简单的解决方案。同时,感谢您的耐心和温和地要求澄清。很抱歉,我没有意识到提交我正在使用的任何代码的重要性,即使这些代码不起作用并且看起来像是基本的、通用的代码。我只是做了一点编辑-你总是发送给同一个收件人,所以你只需要在循环之前调用
addAddress
一次,每封邮件的主题都是一样的,所以你也不需要每次都设置它。与所有这些算法不同,您可以将foreach(str_split($bodyAll,160)作为$body){…
。感谢Synchro,很好的更新,简化了处理过程。您没有使用“普通的php邮件函数”;您使用的是完全不同的东西。