Forums / Developer / How to send an email with attachments with eZmail ?

How to send an email with attachments with eZmail ?

Author Message

Aldo Medri

Wednesday 28 April 2010 9:15:04 am

Hello all,
I've tried unsuccessfully to send an e-mail with attachments using eZmail, here is the code:

$arrAttach['filename'] = "name_of_first_file.txt";
$arrAttach['content'] = "file content";
$arrAttachments[] = $arrAttach;

function my_send_email_attach($fromName, $fromEmail, $toName, $toEmail, $htmlMsg, $oggetto, $messaggio, $arrAttachments) {
    $uid = md5(uniqid(time()));
    $header = "From: ".$fromName." <".$fromEmail.">\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

    $ez_headers = array();
    $ez_headers['From'] = "".$fromName." <".$fromEmail.">";

    $ez_extra_headers = "";
    $ez_extra_headers .= "MIME-Version: 1.0\r\n";
    $ez_extra_headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

    // $body = "This is a multi-part message in MIME format.\r\n";
    $body = "--".$uid."\r\n";
    $body .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $body .= $messaggio."\r\n\r\n";
    foreach ($arrAttachments as $arrAttach)
    {
        $filename = $arrAttach['filename'];
        $content = $arrAttach['content'];
        $content = chunk_split(base64_encode($content));
        $body .= "--".$uid."\r\n";
        $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
        $body .= "Content-Transfer-Encoding: base64\r\n";
        $body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
        $body .= $content."\r\n\r\n";
    }
    $body .= "--".$uid."--";

    include_once('lib/ezutils/classes/ezmail.php');
    include_once('lib/ezutils/classes/ezmailtransport.php');

    $mail = new eZMail();
    if (""!=$htmlMsg)
    {
        $mail->setContentType('text/html');
    }
    $mail->setReceiver( $toEmail, $toName );
    $mail->setSender( $fromEmail, $fromName );
    $mail->setSubject( $oggetto );
    $mail->setBody( $body );
    // $mail->headers( $ez_headers );
    $mail->setExtraHeaders( $ez_extra_headers );
    $mailResult = eZMailTransport::send( $mail );
    return $mailResult;
}

why doesn't it work correctly?
thank you in advance...

Massimiliano Collavo

Tuesday 18 May 2010 7:48:58 am

Hello Aldo,

You can use ezcomponents , http://ezcomponents.org/docs/tutorials/Mail

works well.