[Problem]Sending mails with attachment in loop.

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
0ski
Forum Newbie
Posts: 2
Joined: Thu Aug 06, 2009 5:20 am

[Problem]Sending mails with attachment in loop.

Post by 0ski »

Hello,

I have problem with sending mails with different attachments for each email adress. Need to doing it in loop with 20 email batch. All mails i have in db with send attempts. In loop 1st mail sends correcly and i receive it. But after sending 1st mail i get blank page and script stops.
I'm using Kohana framework and Gmail. All messages, attachments, addresses are preparing correcly. Any ideas? :(

Here is my code:

Code: Select all

 
public function send()
        {
            if ( ! Auth::instance()->logged_in('admin'))
        {
            // User is not admin.
            url::redirect('auth/logged_in');
        }
            $swift = email::connect();
            
            $email_templates_model = new Email_Templates_Model;
            $templates = $email_templates_model->getTemplate();
            $emails_model = new Emails_Model;
            $emails_to_send = $emails_model->getEmail();
            $templates = $templates->result_array(FALSE);
            $from = 'test@gmail.com';
            
            foreach($emails_to_send as $id => $email)
            {
                echo $id;
                $subject = $id.$templates[$email->template_id - 1]['subject'];
                $body = $id.$templates[$email->template_id - 1]['body'];
                // Adresaci
                $recipients = new Swift_RecipientList;
                $recipients->addTo($email->email);
                // Build the HTML message
                $message = new Swift_Message($subject, $body, "text/html");
                $message->setEncoding("utf8-general-ci");
             
                // Attachment
                $swiftfile = new Swift_File('attached/'.date('Y-n-j').'/'.$email->file);
                $attachment = new Swift_Message_Attachment($swiftfile);
                $message->attach($attachment);
                $body = new Swift_Message_Part($body, "text/html");
                $body->setEncoding("utf8-general-ci");
                $message->attach($body);
                
                $swift->send($message, $recipients, $from);
            }
            
            $swift->disconnect();
 
0ski
Forum Newbie
Posts: 2
Joined: Thu Aug 06, 2009 5:20 am

Re: [Problem]Sending mails with attachment in loop.

Post by 0ski »

With attached pdf file 2x smaller (80kb) mailer sending all mails. When i trying send bigger pdf file after big one I get blank page.
Post Reply