簡體   English   中英

使用帶有或不帶有附件的php發送郵件

[英]Send mail using php with or without attachment

我正在使用具有郵件附件選項的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>';
  }
}
}
?>

當然,沒有它是行不通的,因為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>';
  }
}
?>

不發送附件時,請刪除附件中的特定說明。
這意味着更改此:

$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';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM