kanudo
kanudo

Reputation: 2219

PHP Pear : SMTP mail script with Attachment and PLAIN-TEXT and HTML based

i am creating script where the script would be generating a mail such that there becomes two option available in form of "text/plain" or "text/html". While generating headers i am able to generate header for any one of these, but when i try for both of them to create alternative environment, there rises an error with header in the generated mail. Actually i am not able to handle the HEADER ARRAY.

I provided both: the script and the output mail.

PHP Script

if(isset($_POST['cperson']))
    {

        $cperson=$_POST['cperson'];
        $email=$_POST['email'];
        $subject=$_POST['subject'];
        $message=$_POST['message'];

        require_once "../php/Mail.php";

        $from = $cperson."<".$email.">";
        $to = "Receiver<[email protected]>";
        $subject = $subject;

        $text="Text example";
        $html = "<html><head></head><body>HTML <b>Example</b></body></html>";

        $mime_boundary=md5(mt_rand()); //------ Main Boundary
        $alt_boundary=md5(mt_rand()); //------ Alternate Boundary

        $headers = array (
        'From' => $from,
        'To' => $to,
        'Subject' => $subject,
        'MIME-Version' => "1.0",
        'Content-Type' => "multipart/mixed; boundary=\"".$mime_boundary."\"\r\n"."--".$mime_boundary."\r\n"."Content-Type: multipart/alternative; boundary=".$alt_boundary."\r\n");

        $body = "--".$alt_boundary."\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $text . "\r\n"."--".$alt_boundary."\r\n";
            "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $html . "\r\n"."--".$alt_boundary."\r\n";

        $tmp_name1 = $_FILES['doc1']['tmp_name'];
        $type1 = $_FILES['doc1']['type'];
        $file_name1 = $_FILES['doc1']['name'];
        $size1 = $_FILES['doc1']['size'];

        if (file_exists($tmp_name1))
        {

            if(is_uploaded_file($tmp_name1))
            {
                $file1 = fopen($tmp_name1,'rb');
                $data1 = fread($file1,filesize($tmp_name1));
                fclose($file1);
                $data1 = chunk_split(base64_encode($data1));
            }

            $body .= "--".$mime_boundary."\n" .
                "Content-Type: ".$type1.";\n" .
                " name=\"".$file_name1."\"\n" .
                //"Content-Disposition: attachment;\n" .
                //" filename=\"{$fileatt_name}\"\n" .
                "Content-Transfer-Encoding: base64\n\n" .
                $data1 . "\n\n";

        }

        $body .= "--".$mime_boundary."\n";

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "username";
    $password = "password";

    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail))
    {
        echo("<p> ". $mail->getMessage() ." </p>");
    }
    else
    {
        header("Location:inquiry.php?msg=success");
    }
 }
 else
 {
     header("Location:inquiry.php?msg=fail");    
 }

Output Mail Following output is generated as a mail

Subject: Test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="525d350e35975cc6e00f4374ba753aa4"


--432463601994e834b268ba151f6b0852
Content-Type: text/plain; 
Content-Transfer-Encoding: 7bit
Text example

--432463601994e834b268ba151f6b0852

--525d350e35975cc6e00f4374ba753aa4
Content-Type: image/png;
 name="logo.png"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAPIAAAAoCAYAAADEzaftAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz
AAANgQAADYEBdB6KQAAAABl0RVh0U29mdHdhcm-----------------------w9QVFSEESNG6Gyj

--525d350e35975cc6e00f4374ba753aa4

Thanks in advance :)

Based on the below mail i carried out using mixed and alternative

Content-Type: multipart/mixed; 
 boundary="----=_Part_997431_715227151.1404894582403"

------=_Part_997431_715227151.1404894582403
Content-Type: multipart/alternative; 
 boundary="----=_Part_997430_1861058758.1404894582403"

------=_Part_997430_1861058758.1404894582403
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Text Message

------=_Part_997430_1861058758.1404894582403
Content-Type: text/html
Content-Transfer-Encoding: 7bit

HTML Message

------=_Part_997430_1861058758.1404894582403--

------=_Part_997431_715227151.1404894582403
Content-Type: image/png;
 name="logo.png"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAPIAAAAoCAYAAADEzaftAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz
AAANgQAADYEBdB6KQAAAABl0RVh0U29mdHdhcm-----------------------w9QVFSEESNG6Gyj

------=_Part_997431_715227151.1404894582403--

Upvotes: 2

Views: 1132

Answers (1)

kanudo
kanudo

Reputation: 2219

Wow i got the solution

Here is the output mail

Content-Type: multipart/mixed;
 boundary="bdd34484bf73eb6782690e90985334b2"

--bdd34484bf73eb6782690e90985334b2
Content-Type: multipart/alternative;
 boundary="1e5d6e084aa19ef37bf6f314c2dfe404"

--1e5d6e084aa19ef37bf6f314c2dfe404
Content-Type: text/plain; 
Content-Transfer-Encoding: 7bit

Text example

--1e5d6e084aa19ef37bf6f314c2dfe404
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<html><head></head><body>HTML <b>Example</b></body></html>

--1e5d6e084aa19ef37bf6f314c2dfe404--
--bdd34484bf73eb6782690e90985334b2
Content-Type: image/png;
 name="logo.png"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAPIAAAAoCAYAAADEzaftAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz
AAANgQAADYEBdB6KQAAAABl0RVh0U29mdHdhcm-----------------------w9QVFSEESNG6Gyj

--bdd34484bf73eb6782690e90985334b2--

The changes and working script is below Working script

if(isset($_POST['cperson']))
    {

        $cperson=$_POST['cperson'];
        $email=$_POST['email'];
        $subject=$_POST['subject'];
        $message=$_POST['message'];

        require_once "../php/Mail.php";

        $from = $cperson."<".$email.">";
        $to = "Receiver<[email protected]>";
        $subject = $subject;

        $text="Text example";
        $html = "<html><head></head><body>HTML <b>Example</b></body></html>";

        $mime_boundary=md5(mt_rand()); //------ Main Boundary
        $alt_boundary=md5(mt_rand()); //------ Alternate Boundary

        /* $headers = array (
        'From' => $from,
        'To' => $to,
        'Subject' => $subject,
        'MIME-Version' => "1.0",
        'Content-Type' => "multipart/mixed; boundary=\"".$mime_boundary."\"\r\n"."--".$mime_boundary."\r\n"."Content-Type: multipart/alternative; boundary=".$alt_boundary."\r\n"); */

        // Edited Header Part
        $headers = array (
        'From' => $from,
        'To' => $to,
        'Subject' => $subject,
        'MIME-Version' => "1.0",
        'Content-Type' => "multipart/mixed;\n boundary=\"".$mime_boundary."\"");

        /* $body = "--".$alt_boundary."\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $text . "\r\n"."--".$alt_boundary."\r\n";
            "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $html . "\r\n"."--".$alt_boundary."\r\n"; */

        // Edited Initial Body Part
        $body = "--".$mime_boundary."\r\n" .
            "Content-Type: multipart/alternative;\n".
            " boundary=\"".$alt_boundary."\"\n\n".
            "--".$alt_boundary."\n" .
            "Content-Type: text/plain; \n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $text . "\n\n"."--".$alt_boundary."\r\n" .
            "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\r\n" .
            $html . "\n\n"."--".$alt_boundary."--\r\n";

        $tmp_name1 = $_FILES['doc1']['tmp_name'];
        $type1 = $_FILES['doc1']['type'];
        $file_name1 = $_FILES['doc1']['name'];
        $size1 = $_FILES['doc1']['size'];

        if (file_exists($tmp_name1))
        {

            if(is_uploaded_file($tmp_name1))
            {
                $file1 = fopen($tmp_name1,'rb');
                $data1 = fread($file1,filesize($tmp_name1));
                fclose($file1);
                $data1 = chunk_split(base64_encode($data1));
            }

            $body .= "--".$mime_boundary."\n" .
                "Content-Type: ".$type1.";\n" .
                " name=\"".$file_name1."\"\n" .
                //"Content-Disposition: attachment;\n" .
                //" filename=\"{$fileatt_name}\"\n" .
                "Content-Transfer-Encoding: base64\n\n" .
                $data1 . "\n\n";

        }

        $body .= "--".$mime_boundary."--\n";

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "username";
    $password = "password";

    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail))
    {
        echo("<p> ". $mail->getMessage() ." </p>");
    }
    else
    {
        header("Location:inquiry.php?msg=success");
    }
 }
 else
 {
     header("Location:inquiry.php?msg=fail");    
 }

Upvotes: 1

Related Questions