Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending Message With Attachment #48

Open
marcleimvs opened this issue Dec 20, 2020 · 3 comments
Open

Sending Message With Attachment #48

marcleimvs opened this issue Dec 20, 2020 · 3 comments

Comments

@marcleimvs
Copy link

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

@rizalhrm
Copy link

rizalhrm commented Oct 10, 2022

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

@marcleimvs
Copy link
Author

marcleimvs commented Oct 11, 2022

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;

`

@rizalhrm
Copy link

it's works. thanks you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants