【问题标题】:Send attachment and inline image in phpmailer every 5 minute每 5 分钟在 phpmailer 中发送附件和内联图像
【发布时间】:2021-07-02 17:22:40
【问题描述】:

我需要访问一个站点:https://c.xkcd.com/random/comic/ 并使用 JSON API 了解详细信息。

示例 JSON:

{"month": "2", "num": 64, "link": "", "year": "2006", "news": "", "safe_title": "Solar Plexus", "transcript": "[[Hat guy and man standing there talking]]\nHat guy: Asolarplexussayswhat?\nMan: What?\n[[They continue to stand there]]\n[[They continue to stand there]]\n[[Hat guy punches the man in the chest]]\n{{alt: It hurts to be hit there, you know}}", "alt": "It hurts to be hit there, you know", **"img": "https://imgs.xkcd.com/comics/solar_plexus.jpg"**, "title": "Solar Plexus", "day": "15"}

我需要提取图像 (img) 并使用 phpmailer 将其作为附件和内联内容发送。我需要每 5 分钟执行一次,每次使用一个随机 url。

https://c.xkcd.com/random/comic/ 每次访问时都会显示随机漫画。

我曾想过使用 curl 下载图像,但我不知道如何以编程方式每 5 分钟执行一次。

nb:我不能使用任何框架。

【问题讨论】:

  • 你有没有尝试过?你到底卡在哪里了?

标签: json curl cron phpmailer


【解决方案1】:

我个人不使用 PHP,但最近我做了类似的项目。

您可以使用sleep(seconds) 来完成工作。在您的情况下,只需将 300 的值传递给 sleep();循环内

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host       = 'smtp.gmail.com';
$mail->SMTPAuth   = true;
$mail->Username   = 'your_email@gmail.com';
$mail->Password   = '**password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port       = 587;

$mail->setFrom('sent_mail_from@gmail.com', 'Hitik Saini');

//email content
$mail->isHTML(true);
$mail->Subject = 'This is email Subject';
$mail->Body    .= 'A welcoming description.<br>';
$mail->Body    .= '<img src='.$img_src.'> <br>';

//Attachments
$mail->addStringAttachment(file_get_contents($img_src),'img_name.png');

try {
  $mail->send();
  echo 'Message sent';
  sleep(300);

  } catch (Exception $e) {
  echo $mail->ErrorInfo;

  //Reset the connection to abort sending this message
  //The loop will continue trying to send mails
  $mail->getSMTPInstance()->reset();
  }

  //Clear all addresses and attachments for the next iteration
  $mail->clearAddresses();
  $mail->clearAttachments();
 }

将 img_src 替换为来自xkcd.com的图片链接

【讨论】:

    猜你喜欢
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多