0

PHP pear Mail with attachment implementation with my current code. oddly there were many sources out there that just didn’t seem to work.

For now my mail form can send mail using php pear mail SMTP. But it does not have the ability to send attached file or mime.

I try adding the Mail_mime classes require('Mail/mime.php'); let my attachment field name = picture

so my file handler $file = 'cat.jpg';

do I need to upload this file to my server ? or if can be direct send to email ? I am not sure

What should I modify to trying to send authenticated SMTP email with an attachment using Pear Mail.

<?php 
    // Pear Mail Library
    require_once "Mail.php";
    require_once "Mail/mime.php";

    //getting form data
    $errorMSG = "";
    if (empty($_POST["name"])) {$errorMSG = "Name is required ";} else { $name = $_POST["name"];}
    if (empty($_POST["email"])) {$errorMSG .= "Email is required ";} else {$email = $_POST["email"];}
    if (empty($_POST["subject"])) {$errorMSG .= "Subject is required ";} else {$subject = $_POST["subject"];}
    if (empty($_POST["message"])) {$errorMSG .= "Message is required ";} else {$message = $_POST["message"];}

    //Message prepair
    $Subject         =$subject;
    $sender_name     =$name;
    $sender_email    =$email;


    $body           .="Customer Name: ".$sender_name."\r\n <br>";
    $body           .="Customer Email: ".$sender_email."\r\n <br>";
    $body           .=$message;

    //Mime type adding
    $text = $body;
    $html = '<html><body>'.$body.'</body></html>';

    $attachment = $Attachment;
    $crlf = "\n";

    $mime = new Mail_mime($crlf));
    $mime -> setTXTBody($text);
    $mime -> setHTMLBody($html);


    $allowTypes = array('pdf', 'jpg', 'png', 'jpeg');
    $file_type = $_FILES['attachment']['type']

    if(in_array($file_type, $allowTypes)){
        foreach($_FILES['attachment']['tmp_name'] as $key => $value){
        $file_name = $_FILES['attachment']['name'][$key]; //nama file (tanpa path)
        $tmp_name  = $_FILES['attachment']['tmp_name'][$key]; //nama local temp file di server
        $file_type = $_FILES['attachment']['type'][$key]; //tipe filenya (langsung detect MIMEnya)
        $fp      = fopen($tmp_name, 'r');
        $content = fread($fp, filesize($tmp_name));
        $content = addslashes($content);
        fclose($fp);
            $data = chunk_split(base64_encode($content));
            $mime->addAttachment($data, $file_type);
        }
    }

    $body = $mime -> get();

   // Identify the sender, recipient, mail subject, and body
   $sender    = $sender_name."<no-reply@example.com>"; 
   $recipient = "info@example.com";
   $Cc        = "cc@example.com";
   $subject   = $Subject ;

   // Identify the mail server, username, password, and port
   $server   = "ssl://smtp.example.com";
   $username = "no-reply@example.com";
   $password = "**********";
   $port     = "465";

   // Set up the mail headers
   $headersssss = array(
      "MIME-Version"                => '1.0',
      "Content-Type"                => 'text/html; charset="UTF-8',
      "Content-Transfer-Encoding"   => 'base64\r\n',
      "From"                        => $sender,
      "To"                          => $recipient,
      "Reply-To"                    => $sender_email,
      "Cc"                          => $Cc,
      "Subject"                     => $subject
   );

   $headers = $mime -> headers($headersssss);

   // Configure the mailer mechanism
   $smtp = Mail::factory("smtp",
      array(
        "host"     => $server,
        "username" => $username,
        "password" => $password,
        "auth"     => true,
        "port"     => 465
      )
   );

   // Send the message
   $mail = $smtp->send($recipient, $headers, $body);
   if (PEAR::isError($mail)) {
      echo ($mail->getMessage());
   }else{
      echo "success"; 
   }

I have found an example here Can any one help me with this ?

Firefog
  • 3,094
  • 7
  • 48
  • 86
  • Do you absolutely have to use pear? If not, this is simple to do with phpmailer. See example at https://github.com/PHPMailer/PHPMailer. phpmailer is also simple to setup on your server - just copy a few files to your server. – mti2935 Jan 14 '19 at 02:09
  • yes because my old app have this feature. Can you help me to convert into PHPMailer version of my`email_send.php` code above – Firefog Jan 14 '19 at 07:00

0 Answers0