我正在使用具有邮件附件选项的PHP代码,当我有附件时,一切都很好,但是当我没有邮件时,邮件不会发送给收件人。我发送邮件时回复消息也有问题,我没有收到任何消息。这是我使用的代码:)
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$to="mares.p@hotmail.com";
$subject="Online Prijava";
$from = stripslashes($_POST['ime'])."<".stripslashes($_POST['email_adresa']).">";
if(empty($_POST['ime']) || empty($_POST['email_adresa']))
{
$errors .= "\n Greska: nisu uneta sva obavezna polja";
}
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$tmp_name = $_FILES['fotokopija_uplatnice']['tmp_name'];
$type = $_FILES['fotokopija_uplatnice']['type'];
$file_name = $_FILES['fotokopija_uplatnice']['name'];
$size = $_FILES['fotokopija_uplatnice']['size'];
$message = "PODACI U PSU:
\n\n Razred: " .$_POST['razred']. "
\n\n Boja: " .$_POST['boja']. "
\n\n Tip dlake: " .$_POST['tip_dlake']. "
\n\n Velicina: " .$_POST['velicina']. "
\n\n Pol: " .$_POST['pol']. "
\n\n Visina: " .$_POST['visina']. "
\n\n Tezina: " .$_POST['tezina']. "
\n\n Ime psa: " .$_POST['ime_psa']. "
\n\n Broj pedigra: " .$_POST['broj_pedigrea']. "
\n\n Datum rodjenja: " .$_POST['datum_rodjenja']. "
\n\n Otac: " .$_POST['otac']. "
\n\n Broj pedigrea oca: " .$_POST['broj_pedigrea_oca']. "
\n\n Majka: " .$_POST['majka']. "
\n\n Broj pedigra majke: " .$_POST['broj_pedigra_majke']. "
\n\n Odgajivac: " .$_POST['odgajivac']. "
\n\n
\n\nPODACI O VLASNIKU
\n\n Ime: " .$_POST['ime']. "
\n\n Adresa: " .$_POST['adresa']. "
\n\n Grad: " .$_POST['grad']. "
\n\n Drzava: " .$_POST['drzava']. "
\n\n Telefon: " .$_POST['telefon']. "
\n\n Email adresa: " .$_POST['email_adresa'];
if (file_exists($tmp_name)){
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$file_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
if (@mail($to, $subject, $message, $headers))
{
echo '<div><center><h1>Prijava uspesno poslata.</h1></center></div>';
}else
{
echo '<div><center><h1>Greska prilikom slanja prijave. Molimo pokusajte ponovo.</h1></center></div>';
}
}
}
?>
答案 0 :(得分:1)
当然没有它就行不通,因为mail()在if-clausel里面,需要有附件。将mail()函数移到它之外,如果确实存在附件,则只将MIME部分添加到标题和消息中。顺便说一下,如果你有附件,你就会覆盖$ message。
像这样(未经测试):
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$to="mares.p@hotmail.com";
$subject="Online Prijava";
$from = stripslashes($_POST['ime'])."<".stripslashes($_POST['email_adresa']).">";
if(empty($_POST['ime']) || empty($_POST['email_adresa']))
{
$errors .= "\n Greska: nisu uneta sva obavezna polja";
}
$message = "PODACI U PSU:
\n\n Razred: " .$_POST['razred']. "
\n\n Boja: " .$_POST['boja']. "
\n\n Tip dlake: " .$_POST['tip_dlake']. "
\n\n Velicina: " .$_POST['velicina']. "
\n\n Pol: " .$_POST['pol']. "
\n\n Visina: " .$_POST['visina']. "
\n\n Tezina: " .$_POST['tezina']. "
\n\n Ime psa: " .$_POST['ime_psa']. "
\n\n Broj pedigra: " .$_POST['broj_pedigrea']. "
\n\n Datum rodjenja: " .$_POST['datum_rodjenja']. "
\n\n Otac: " .$_POST['otac']. "
\n\n Broj pedigrea oca: " .$_POST['broj_pedigrea_oca']. "
\n\n Majka: " .$_POST['majka']. "
\n\n Broj pedigra majke: " .$_POST['broj_pedigra_majke']. "
\n\n Odgajivac: " .$_POST['odgajivac']. "
\n\n
\n\nPODACI O VLASNIKU
\n\n Ime: " .$_POST['ime']. "
\n\n Adresa: " .$_POST['adresa']. "
\n\n Grad: " .$_POST['grad']. "
\n\n Drzava: " .$_POST['drzava']. "
\n\n Telefon: " .$_POST['telefon']. "
\n\n Email adresa: " .$_POST['email_adresa'];
$headers = "From: $from\r\n";
if(!empty($_FILES)) {
$tmp_name = $_FILES['fotokopija_uplatnice']['tmp_name'];
$type = $_FILES['fotokopija_uplatnice']['type'];
$file_name = $_FILES['fotokopija_uplatnice']['name'];
$size = $_FILES['fotokopija_uplatnice']['size'];
if (file_exists($tmp_name)){
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message .= "\n\n\nThis is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$file_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
}
if (mail($to, $subject, $message, $headers))
{
echo '<div><center><h1>Prijava uspesno poslata.</h1></center></div>';
}else
{
echo '<div><center><h1>Greska prilikom slanja prijave. Molimo pokusajte ponovo.</h1></center></div>';
}
}
?>
答案 1 :(得分:0)
如果不发送附件,请删除附件特定说明。
这意味着改变这一点:
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$file_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
对此:
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html;\r\n";
$message = $message; // Leave unchanged
但另外我想指出一个事实,即您应该使用API发送邮件以避免任何不必要的错误。而且用它来开发会更容易。我推荐PHPMailer:https://github.com/PHPMailer/PHPMailer
这是一个关于如何使用PHPMailer的简单示例。看看它是如何简单:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User'); // 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 = 'This is the HTML message body <b>in bold!</b>';
$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;
} else {
echo 'Message has been sent';
}