开发一个PHP脚本,每天向您发送一个关联数组

时间:2014-03-22 14:09:20

标签: php json

如何开发一个php文件,向我发送一个关联数组的电子邮件,其中包含在JSON文件中格式化的最后广告的所有信息?

我有这个Json代码:

{
    "26":{
        "adUrl":"http:\/\/www.parseme.vacau.com\/index.php?page=search&sCategory=26",
        "title":"Engineering Geologist Figueres",
        "salary":"2300.00 Euro \u20ac",
        "category":"ENGINEERING - ARCHITECTURE",
        "publishedDate":"2013-05-07",
        "modifiedDate":"2013-05-27",
        "country":"Spain",
        "region":"Girona",
        "city":"Figueres",
        "cityArea":"Vilatenim",
        "address":"Pla Del Marqu\u00e8s, 13",
        "description":"<p><b>THIS ADVERT IS NOT RELA[... cropped to sample ...] ts and acting as represenattive on site.<\/p>",
        "imageUrl":"http:\/\/www.parseme.vacau.com\/oc-content\/uploads\/65.jpg"
    },
    "30":{
        "adUrl":"http:\/\/www.parseme.vacau.com\/index.php?page=search&sCategory=30",
        "title":"Engineering Barcelona",
        "salary":"2300.00 Euro \u20ac",
        "category":"ENGINEERING - ARCHITECTURE",
        "publishedDate":"2013-05-07",
        "modifiedDate":"2013-05-27",
        "country":"Spain",
        "region":"Barcelona",
        "city":"Barcelona",
        "cityArea":"",
        "address":"",
        "description":"<p><b>THIS ADVERT IS NOT RELA[... cropped to sample ...] ts and acting as represenattive on site.<\/p>",
        "imageUrl":"http:\/\/www.parseme.vacau.com\/oc-content\/uploads\/48.jpg"
    }
}

1 个答案:

答案 0 :(得分:0)

这个PHP脚本应该每次运行时给你发电子邮件(因为你必须更改php邮件程序设置)。您需要做的就是在托管服务器上设置CronJob,并将其设置为每天运行一次此脚本文件。希望这有帮助。

<?php

$json = '{
    "26":{
        "adUrl":"http:\/\/www.parseme.vacau.com\/index.php?page=search&sCategory=26",
        "title":"Engineering Geologist Figueres",
        "salary":"2300.00 Euro \u20ac",
        "category":"ENGINEERING - ARCHITECTURE",
        "publishedDate":"2013-05-07",
        "modifiedDate":"2013-05-27",
        "country":"Spain",
        "region":"Girona",
        "city":"Figueres",
        "cityArea":"Vilatenim",
        "address":"Pla Del Marqu\u00e8s, 13",
        "description":"<p><b>THIS ADVERT IS NOT RELA[... cropped to sample ...] ts and acting as represenattive on site.<\/p>",
        "imageUrl":"http:\/\/www.parseme.vacau.com\/oc-content\/uploads\/65.jpg"
    },
    "30":{
        "adUrl":"http:\/\/www.parseme.vacau.com\/index.php?page=search&sCategory=30",
        "title":"Engineering Barcelona",
        "salary":"2300.00 Euro \u20ac",
        "category":"ENGINEERING - ARCHITECTURE",
        "publishedDate":"2013-05-07",
        "modifiedDate":"2013-05-27",
        "country":"Spain",
        "region":"Barcelona",
        "city":"Barcelona",
        "cityArea":"",
        "address":"",
        "description":"<p><b>THIS ADVERT IS NOT RELA[... cropped to sample ...] ts and acting as represenattive on site.<\/p>",
        "imageUrl":"http:\/\/www.parseme.vacau.com\/oc-content\/uploads\/48.jpg"
    }
}';

$arr = json_decode($json, true);

$last_arr = end($arr);

foreach ($last_arr as $key => $value) {
    $message .= $value . '<br>';
}

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('josh@example.net', 'Josh Adams');  // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = $message;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

echo 'Message has been sent';

?>