Reputation: 91
I'm using php code that have option for attachment in mail, everything works great when i have attachment, but when i don't have it mail doesn't go to recipient. also i have a problem with echo message when mail is sent, i don't receive any message. here is code I use :)
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$to="[email protected]";
$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>';
}
}
}
?>
Upvotes: 0
Views: 142
Reputation: 4680
Of course it doesn't work without it, because mail() is inside of the if-clausel where it is required to have an attachment. Move the mail() function outside of it and only add the MIME part to the header and message, if there is really an attachment. By the way, you're overwriting $message if you have an attachment.
Like this (isn't tested):
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$to="[email protected]";
$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>';
}
}
?>
Upvotes: 1
Reputation: 5326
Remove the attachment specific instructions when you don't send one.
That means change this:
$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";
To this:
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html;\r\n";
$message = $message; // Leave unchanged
But additionally I'd like to point out the fact that you should use an API to send your mails to avoid any kind of unwanted bugs. Moreover it will be easier to develop with it. I recommend PHPMailer: https://github.com/PHPMailer/PHPMailer
Here is a quick example on how to use PHPMailer. See how it is simple:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->From = '[email protected]';
$mail->FromName = 'Mailer';
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$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';
}
Upvotes: 0