将附件(jpg)添加到此PHP邮件脚本

时间:2013-11-07 11:43:24

标签: php email jpeg attachment send

我试图添加一个代码,允许我通过php发送附件(jpg)!

我目前的剧本:

<?php
$input = array("@[XXXX:0]", "@[XXXX:0]", "@[XXX:0]", "@[XXX:0]", "@[XXX:0]", "@[XXX:0]",                 "@[XXX:0]", "@[XXX:0]", "@[XXX:0]", "@[XXX:0]", "@[XXX:0]", "@[XXX:0]");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]];
?>

<?php
 $to = "XXX@XXX.com";
 $subject = "Wuensche euch allen ein schoenes Wochenende.
Sehn uns hier: @[XXXXXXXXXXXX:0]!
Shouts heute gehen an ". $input[$rand_keys[0]]." <3.
Kuss, XXX.";
 $body = "bacon";
 if (mail($to, $subject, $body)) {
   echo("<p>Message to Facebook successfully sent!</p>");
  } else {
   echo("<p>Message to Facebook failed!</p>");
  }
 ?>

类似于:

$file = picture.jpg

(位于同一目录!)

如果有效:

我们可以做点什么吗

$file = $random_picture

$random_picture = 1.jpg 2.jpg 3.jpg 4.jpg etc.

这样脚本会随机编号。并在每次加载时发送它?

谢谢! :)

2 个答案:

答案 0 :(得分:1)

您可以从HTML EMAIL WITH JPEG ATTACHMENTS TUTORIAL获得帮助。

你需要在身体部位改变一点:

$pics = array('1.jpg', '2.jpg', '3.jpg', '4.jpg');
//Pick key of the random image
$random_key = array_rand($pics,1);

$file = file_get_contents("YOUR_ABSOLUTE_URL".$pics[$random_key]);

$body.= "Content-Type: image/jpg; name=\"".$pics[$random_key]."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-disposition: attachment; file=\"".$pics[$random_key]."\"\r\n"
."\r\n"
.chunk_split(base64_encode($file)); 

您现在正在发送电子邮件。

注:

请查看array_rand

编辑:

完整代码如下:

<?php
$to = "XXX@XXX.com";
$subject = "YOUR_SUBJECT";
$body = "bacon";
$pics = array('1.jpg', '2.jpg', '3.jpg', '4.jpg');
//Pick key of the random image
$random_key = array_rand($pics,1);

$file = file_get_contents($pics[$random_key]);

$body.= "Content-Type: image/jpg; name=\"".$pics[$random_key]."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-disposition: attachment; file=\"".$pics[$random_key]."\"\r\n"
."\r\n"
.chunk_split(base64_encode($file));

if (mail($to, $subject, $body)) {
    echo("<p>Message to Facebook successfully sent!</p>");
} else {
    echo("<p>Message to Facebook failed!</p>");
}
?>

答案 1 :(得分:0)

下面的内容我猜(没有测试);

$randomPicArr = array('a.jpg','b.jpg','c.jpg','d.jpg','e.jpg');
$randomNum = rand(0,5);
$random_picture = $randomPicArr[$randomNum];