带有send文件的PHP邮件脚本不起作用

时间:2015-01-17 10:10:05

标签: php file email attachment

为什么我的剧本不起作用?
我尝试使用xampp和在线主机 我尝试了$headers = "From: $from \r\n";
我得到了#34;成功发送了!"但是我没有收到任何邮件      $to = "********"; $from = $_POST['email']; $nume = $_POST['nume']; $prenume = $_POST['prenume']; $phone = $_POST['telefon']; $oras = $_POST['oras']; $adresa = $_POST['adresa']; $facultate = $_POST['facultate']; $titlu1 = $_POST['titlu1']; $desc1 = $_POST['desc1']; $titlu2 = $_POST['titlu2']; $desc2 = $_POST['desc2']; $titlu3 = $_POST['titlu3']; $desc3 = $_POST['desc3']; $subject = "Luminile Iernii - Inscriere: $nume $prenume"; $message = " Nume si Prenume: $nume $prenume \n Email: $from \n Nr. Telefon: $phone \n Oras: $oras \n Adresa: $adresa \n Institutia de invatamant: $facultate \n Titlu Fotografie 1: $titlu1 \n Descriere Fotografie 1: $desc1 \n Titlu Fotografie 2: $titlu2 \n Descriere Fotografie 2: $desc2 \n Titlu Fotografie 3: $titlu3 \n Descriere Fotografie 3: $desc3 \n ";

    // Temporary paths of selected files
    $file1 = $_FILES['file1']['tmp_name'];
    $file2 = $_FILES['file2']['tmp_name'];
    $file3 = $_FILES['file3']['tmp_name'];

    // File names of selected files
    $filename1 = "Fotografie 1";
    $filename2 = "Fotografie 2";
    $filename3 = "Fotografie 3";

    // array of filenames to be as attachments
    $files = array($file1, $file2, $file3);
    $filenames = array($filename1, $filename2, $filename3);

    // include the from email in the headers
    $headers = "From: $from";

    // boundary
    $time = md5(time());
    $boundary = "==Multipart_Boundary_x{$time}x";

</code>

Is boundary necessary with attachment?

    // Temporary paths of selected files
    $file1 = $_FILES['file1']['tmp_name'];
    $file2 = $_FILES['file2']['tmp_name'];
    $file3 = $_FILES['file3']['tmp_name'];

    // File names of selected files
    $filename1 = "Fotografie 1";
    $filename2 = "Fotografie 2";
    $filename3 = "Fotografie 3";

    // array of filenames to be as attachments
    $files = array($file1, $file2, $file3);
    $filenames = array($filename1, $filename2, $filename3);

    // include the from email in the headers
    $headers = "From: $from";

    // boundary
    $time = md5(time());
    $boundary = "==Multipart_Boundary_x{$time}x";

</code>

有人可以向我发誓吗? 我不明白......我做错了什么?

2 个答案:

答案 0 :(得分:2)

尝试使用http://github.com/PHPMailer/PHPMailer

这是一个PHP插件,可以处理发送邮件的所有压力任务,而无需编写所有代码。

http://github.com/PHPMailer/PHPMailer下载PHPMailer脚本并将其上传到您的服务器。

包含该文件:<?php require_once('/path/to/class.phpmailer.php'); ?>

您的最终结果将如下所示:

require_once('/path/to/class.phpmailer.php');

$email = new PHPMailer();
$email->From      = 'you@example.com';
$email->FromName  = 'Your Name';
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

$email->Send();

如果您尝试发送没有此类文件的附件,您将编写不需要的代码堆栈。这个插件使发送电子邮件变得更加容易,最终发送带附件的电子邮件也变得更加容易。

我希望这会有所帮助。

答案 1 :(得分:0)

你的from是否设置了发送文件的enctype?

<form action="upload.php" method="post" enctype="multipart/form-data">

这可能有助于您:http://www.w3schools.com/php/php_file_upload.asp