zimbra-api / soap-api

Zimbra SOAP client in PHP language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sending Message With Attachment

marcleimvs opened this issue · comments

I wanto to send a message with attachments using the API, anyone could help?

hi @marcleimvs
read https://github.com/zimbra-api/soap-api/wiki for tutorial send message.
use the https://github.com/zimbra-api/upload-api library to send message with 1 attachment, example using upload-api library:

$msgTo = new MsgToSend();
$client = new Client(env('ATTACHMENT_URL'));
$request = new Request($files, '', $authToken);
$attachmentsRes = $client->upload($request);
$attachmentsInfo = new AttachmentsInfo();
$attachmentsInfo->setAttachmentId($attachmentsRes[0]->getAttachmentId());
$msgTo->setAttachments($attachmentsInfo);

but I don't know how to send more than 1 attachment.
can anyone tell me how to send more than 1 attachment?

@nguyennv

Thank You very much!! Your comment helped me find the answer to send multiple attachments mail with the code down here! It worked for me, hope this helps you!!!
`
// Connection part

    // get soap params
    $service = '<service>';   // put your service URL here
    $username = '<username>';  // configure your username
    $password = '<password>';  // set your password here
    // return an api
    $api = MailFactory::instance($service);

    try {

        // select the account
        $account = new AccountSelector(AccountBy::NAME(), $username);

        // authenticate
        $auth = $api->auth($account, $password);
    } catch (Exception $e) {
        // error found!!
        $client = $api->getClient();
        echo $client->lastResponse();
        exit;
    }

    /**
     * Build message part
     */
    // email basic data
    $folder = "/inbox";                                 // select inbox folder
    $contentType = 'text/html';                         // for plain text message use "plain/text"
    $subject = 'Email with attachments';                // message subject
    $body = 'This is a simple mail with attachments';   // message body
    $to = 'whoever@somemail.com';                       // maybe a string or array
    $attachments = '<file1>,<file2>,<file3>';           // the file list maybe string or array
    // create message mime part
    $mp = new MimePartInfo(null, 'multipart/alternative');

    // add the body mime part
    $mp->addMimePart(new MimePartInfo(null, $contentType, $body));

    // get attachments array
    $attachs = is_array($attachments) ? $attachments : explode(',', $attachments);

    // Do we have attachments?
    if (count($attachs)) {

        // process it!
        $atts = [];

        foreach ($attachs as $att) {
            $atts[] = new \SplFileInfo($att);
        }

        // create a upload request
        $authToken = $auth->__get('authToken');
        $client = new \Zimbra\Upload\Client($service, $authToken);
        $request = new \Zimbra\Upload\Request(null, $atts);
        $attachmentsRes = $client->upload($request);

        // process uploaded attachments
        foreach ($attachmentsRes as $att) {

            // create an attachmentsInfo
            $attachmentsInfo = new AttachmentsInfo();
            $attachmentsInfo->setAttachmentId($att->getAttachmentId());

            // for each attachment add a mimepart
            $m = new MimePartInfo($attachmentsInfo);

            // add to the message parts
            $mp->addMimePart($m);
        }
    }

    // create new message
    $msg = new MsgToSend();

    // message setup
    $msg->setSubject($subject)
            ->setFolderId($folder)
            ->setMimePart($mp)
    ;

    // Add a recipient
    // this can be a single address (string)
    // or an array of addresses
    if (is_array($to)) {
        // avoid duplicates
        $to = array_unique($to);

        // add mail address
        foreach ($to as $mail) {
            $msg->addEmail(new EmailAddrInfo($mail, AddressType::TO()));
        }
    } else {
        $msg->addEmail(new EmailAddrInfo($to, AddressType::TO()));
    }

    // send the message checking for error
    $result = false;
    try {
        $result = $api->sendMsg($msg);
    } catch (Exception $ex) {
        $client = $api->getClient();
        echo $client->lastResponse();
        exit;
    }
    if ($result) {
        echo 'Msg sent!';
    } else {
        echo 'Msg fail!';
    }
    exit;

`

it's works. thanks you