Jump to content

Send mail with attachments.


dsp77

Recommended Posts

Hello,

i have this code for sending email it works great but i don't know where to start in sending emails with a pdf file attached ex: test.pdf. Any suggestions?

 

 

<?php    session_start();    require_once('framework/framework.php');    include('framework/class.smtp.inc');//--- databse settings    $dsn = array(        'dbtype'      => 'mysql',        'username'     => '',        'password'     => '',        'host'         => '',        'database'  => ''    );        try {        $db = db::connect($dsn);    }    catch (Exception $e) {         die($e->getMessage());     }    //---    $subject="Formular online pentru participantii Diaspora Stiintifica 2010";// +--------------  BEGIN  ----  Functia de trimitere mail prin smtp  -------------------+ */    function trimite($destinatar='', $subiect='', $mesaj='') {//--- mail settings    $smtp_server_host="mail.server.com";    $smtp_server_port="25";    $sender="email@email.com";    $return_path="email@email.com";    $smtp_username="email@email.com";    $smtp_pswd="123";        /***************************************        ** Setup some parameters which will be        ** passed to the smtp::connect() call.        ***************************************/        $params['host'] = $smtp_server_host;    // The smtp server host/ip        $params['port'] = $smtp_server_port;    // The smtp server port        $params['helo'] = exec('hostname');        // What to use when sending the helo command. Typically, your domain/hostname        $params['auth'] = false;                    // Whether to use basic authentication or not//        $params['user'] = $smtp_username;        // Username for authentication//        $params['pass'] = $smtp_pswd;            // Password for authentication        $params['timeout'] = '60';        /***************************************        ** These parameters get passed to the        ** smtp->send() call.        ***************************************/        $send_params['recipients']    = $destinatar;    // The recipients (can be multiple)        $send_params['headers']        = array( "MIME-Version: 1.0",                                             "X-Mailer: PHP/" . phpversion(),                                            "Return-Path: ".$return_path,                                            "From: ".$sender,                                            "To: ".$destinatar,                                            "Subject: ".$subiect,                                            "Content-type: text/html; charset=UTF-8",                                            "Content-Transfer-Encoding: 8bit");        $send_params['from']        = $sender;    // This is used as in the MAIL FROM: cmd                                                // It should end up as the Return-Path: header        $send_params['body'] = $mesaj;        /***************************************        ** The code that creates the object and        ** sends the email.        ***************************************/        $smtp = new smtp($params);        $smtp->connect();        $trimis=$smtp->send($send_params);        $raspuns=array($destinatar, $trimis, $smtp->errors);        //    $smtp->rset();    //--- Bcc:/*        $send_params['recipients']    = $sender;        $trimisi=$smtp->send($send_params);        $errmsgi=$smtp->errors;        print "<br>bcc: ".$send_params['recipients']."; ".$trimisi."<br>";            for ($n = 0 ; $n <= count($errmsgi) - 1; $n++) {                print "<br>"."  bccerr:  <strong>".$errmsgi[$n]."</strong><br>";            }*/        $smtp->quit();        return $raspuns;    }// +--------------   END   ----  Functia de trimitere mail prin smtp  -------------------+ */// +---------------------------------------------------------------------------+    $afisez = "";    $cond=0;        $query = "SELECT email_1 FROM inregistrari WHERE `completat`=0";    $useri = $db->getAll($query);    $i=0;    $j=0;    $k=0;        foreach ($useri as $row) {        $tpl = new HTML_Template_Sigma('mesaj');        $tpl->loadTemplateFile("mesaj.html");        $tpl->setVariable($row);        $html=$tpl->get(); //*/        $html=nl2br($html);        $html=str_replace(chr(10), "", $html);        $html=str_replace(chr(13), "", $html); // */// +----------------------+        $subiect = $subject;        if (!empty($row['email_1'])) {            $subelements=preg_split("/, /", $row['email_1'], -1, PREG_SPLIT_NO_EMPTY);            for ($m = 0 ; $m <= count($subelements) - 1; $m++) {                $rez = trimite($subelements[$m], $subiect, $html);                if ($rez[1]==true) {                    $afisez .= $rez[0].";1<br>";                    $i++;                } else {                    $afisez .= $rez[0].";0<br>";                    $errmsg=$rez[2];                    for ($n = 0 ; $n <= count($errmsg) - 1; $n++) {                        $afisez .= "    <strong>".$errmsg[$n]."</strong><br>";                    }                    $j++;                }            }        } else {            $afisez .= $row['email_1'].";no email;n<br>";            $k++;        }    }$afisez .= "<br>{$i} mesaje Trimise<br>";$afisez .= "{$j} mesaje Netrimise<br>";$afisez .= "{$k} mesaje fara adresa<br>";print $afisez;?>

 

Link to comment
Share on other sites

in fact i need to implement this code in the other one...

 

<?phpfunction mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {    $file = $path.$filename;    $file_size = filesize($file);    $handle = fopen($file, "r");    $content = fread($handle, $file_size);    fclose($handle);    $content = chunk_split(base64_encode($content));    $uid = md5(uniqid(time()));    $name = basename($file);    $header = "From: ".$from_name." <".$from_mail.">\r\n";    $header .= "Reply-To: ".$replyto."\r\n";    $header .= "MIME-Version: 1.0\r\n";    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";    $header .= "This is a multi-part message in MIME format.\r\n";    $header .= "--".$uid."\r\n";    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";    $header .= $message."\r\n\r\n";    $header .= "--".$uid."\r\n";    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here    $header .= "Content-Transfer-Encoding: base64\r\n";    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";    $header .= $content."\r\n\r\n";    $header .= "--".$uid."--";    if (mail($mailto, $subject, "", $header)) {        echo "mail send OK"; // or use booleans here        echo $mailto;    } else {        echo "mail send ERROR!";    }}?><?php$my_file = "letter.pdf";$my_path = $_SERVER['DOCUMENT_ROOT']."/admin/email/";$my_name = "name";$my_mail = "mail.@mail.com";$my_replyto = "mail.@mail.com";$my_subject = "subject";$my_message = "message";mail_attachment($my_file, $my_path, "mail@mail.com", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.