首页 文章

带有send文件的PHP邮件脚本不起作用[重复]

提问于
浏览
-1

这个问题在这里已有答案:

为什么我的脚本不起作用?
我尝试使用xampp和在线主机 .
我试过 $headers = "From: $from \r\n";
我得到"Sent successfully!"但我没有收到任何邮件 .
$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?` ```xml <code> // headers used for send attachment with email $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";

// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";

$message .= "--{$boundary}\n";

// attach the attachments to the message
foreach( $files as $key => $value )
{
    if( empty( $value ) )
    {
        unset( $files[$key] );
        unset( $filenames[$key] );
    }
}

for($x = 0; $x < count($files); $x++) {
    $file = fopen($files[$x], "r");
    $content = fread($file,filesize($files[$x]));
    fclose($file);
    $content = chunk_split(base64_encode($content));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" . "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
    $message .= "--{$boundary}\n";
}

// sending mail
$sendmail = mail($to, $subject, $message, $headers);

// verify if mail is sent or not
if ($sendmail) {
    echo "Sent successfully!";
} else {
    echo "Error occurred. Try again!";
}

</code> ```

有人能对我说话吗?
我不明白......我做错了什么?

2 回答

  • 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();
    

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

    我希望这有帮助 .

  • 0

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

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

    这可能会帮到你:http://www.w3schools.com/php/php_file_upload.asp

相关问题