Zend_Mail attachments

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Zend_Mail attachments

Alexander Hanhikoski
I need to attach a file (image.. jpeg, gif, png, or etc.) received from
form upload.

Here's the code I use, but it isn't working.

        $mail = new Zend_Mail();

        $mail->setBodyHtml($mailbody);

        $mail->setFrom("[hidden email]", "Testsite");
        $mail->addTo("[hidden email]", "Receiver");

        $pictmpname = $_FILES['pic']['tmp_name'];
        $pictype = $_FILES['pic']['type'];
        $picfilename = $_FILES['pic']['name'];
        $picsize = $_FILES['pic']['size'];

        $handle = fopen($pictmpname, "rb");
        $picbody = fread($handle, $picsize);
        fclose($handle);

        $at = $mail->addAttachment($picbody);
        $at->type = $pictype;
        $at->filename = $picfilename;

        $mail->setSubject("Test");
        $mail->send();


"You are doing it wrong!"
- I know, please help me out. :)

Best regards,
Alex
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Simon Mundy
I found this out by trial and error - you need to create a reference  
to the attachment, so:-

>        $at = $mail->addAttachment($picbody);
>        $at->type = $pictype;
>        $at->filename = $picfilename;

becomes

>        $at =& $mail->addAttachment($picbody);
>        $at->type = $pictype;
>        $at->filename = $picfilename;

Hope this helps



--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  
4124
http://www.peptolab.com


Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Steven Van Poeck
This really makes me wondering. I thought all assignments to variables
were made
by reference in PHP 5?

Can anyone direct me to a good documentation regarding references in PHP 5
please? When you need to use them and when not ? I'm getting lost :(

Thanks !
Steven


Quoting Simon Mundy <[hidden email]>:

> I found this out by trial and error - you need to create a reference  
> to the attachment, so:-
>
>>        $at = $mail->addAttachment($picbody);
>>        $at->type = $pictype;
>>        $at->filename = $picfilename;
>
> becomes
>
>>        $at =& $mail->addAttachment($picbody);
>>        $at->type = $pictype;
>>        $at->filename = $picfilename;
>
> Hope this helps
>
>
>
> --
>
> Simon Mundy | Director | PEPTOLAB
>
> """ " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
> 202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
> Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  4124
> http://www.peptolab.com
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Andries Seutens
Simon,

This looks pretty unusual to me also.

$at = clone $mail->addAttachment($picbody);  // would create a copy
$at = $mail->addAttachment($picbody); // would create a reference

Am I missing something?

@Steven: check out this link:

http://www.zend.com/php5/articles/engine2-php-oo.php

Regards,

Andries Seutens
Belgium
http://andries.systray.be


Steven Van Poeck schreef:

> This really makes me wondering. I thought all assignments to variables
> were made
> by reference in PHP 5?
>
> Can anyone direct me to a good documentation regarding references in
> PHP 5
> please? When you need to use them and when not ? I'm getting lost :(
>
> Thanks !
> Steven
>
>
> Quoting Simon Mundy <[hidden email]>:
>
>> I found this out by trial and error - you need to create a reference  
>> to the attachment, so:-
>>
>>>        $at = $mail->addAttachment($picbody);
>>>        $at->type = $pictype;
>>>        $at->filename = $picfilename;
>>
>> becomes
>>
>>>        $at =& $mail->addAttachment($picbody);
>>>        $at->type = $pictype;
>>>        $at->filename = $picfilename;
>>
>> Hope this helps
>>
>>
>>
>> --
>>
>> Simon Mundy | Director | PEPTOLAB
>>
>> """ " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
>> 202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
>> Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  
>> 4124
>> http://www.peptolab.com
>>
>>
>>
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Alexander Hanhikoski
In reply to this post by Alexander Hanhikoski
I found this out by trial and error - you need to create a reference  to
the attachment, so:-

>        $at = $mail->addAttachment($picbody);
>        $at->type = $pictype;
>        $at->filename = $picfilename;


becomes

>        $at =& $mail->addAttachment($picbody);
>        $at->type = $pictype;
>        $at->filename = $picfilename;

Changed this. Not sure if it makes difference.



> Also, why not use file_get_contents instead of fopen(), fread(),
> fclose(),


Should not make any functional difference, but it does make the code
cleaner. :)


So the new code, which still is not working:

       $mail = new Zend_Mail();

       $mail->setBodyHtml($mailbody);

       $mail->setFrom("[hidden email]", "Testsite");
       $mail->addTo("[hidden email]", "Receiver");

       $pictmpname = $_FILES['pic']['tmp_name'];
       $pictype = $_FILES['pic']['type'];
       $picfilename = $_FILES['pic']['name'];

       $picbody = file_get_contents($pictmpname);

       $at = $mail->addAttachment($picbody);
       $at->type = $pictype;
       $at->filename = $picfilename;

       $mail->setSubject("Test");
       $mail->send();

What is interesting, that this does work with gif-images, but not with
jpg, jpeg, png, ico, bmp or any other file type. The execution just
freezes into "waiting for responce from webserver" with other filetypes
besides gif. Huh?
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Alexander Hanhikoski
In reply to this post by Alexander Hanhikoski
I found this out by trial and error - you need to create a reference  to
the attachment, so:-

>        $at = $mail->addAttachment($picbody);
>        $at->type = $pictype;
>        $at->filename = $picfilename;


becomes

>        $at =& $mail->addAttachment($picbody);
>        $at->type = $pictype;
>        $at->filename = $picfilename;

Changed this. Not sure if it makes difference.



> Also, why not use file_get_contents instead of fopen(), fread(),
> fclose(),


Should not make any functional difference, but it does make the code
cleaner. :)


So the new code, which still is not working:

       $mail = new Zend_Mail();

       $mail->setBodyHtml($mailbody);

       $mail->setFrom("[hidden email]", "Testsite");
       $mail->addTo("[hidden email]", "Receiver");

       $pictmpname = $_FILES['pic']['tmp_name'];
       $pictype = $_FILES['pic']['type'];
       $picfilename = $_FILES['pic']['name'];

       $picbody = file_get_contents($pictmpname);

       $at =& $mail->addAttachment($picbody);
       $at->type = $pictype;
       $at->filename = $picfilename;

       $mail->setSubject("Test");
       $mail->send();

What is interesting, that this does work with gif-images, but not with
jpg, jpeg, png, ico, bmp or any other file type. The execution just
freezes into "waiting for responce from webserver" with other filetypes
besides gif. Huh?
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Simon Mundy
In reply to this post by Andries Seutens
Hi Andries,

Nope, nothing unusual about this. PHP5 will return a copy of a  
variable when returned from a function by default.

You can change this behavious by setting the reference in the  
function call:-

function &getAttachment($a)
{
        $a = new MailAttachment();
        return $a;
}

...now that will return a reference to the variable by default.

Or you can simply use the syntax provided in the previous example.

It makes a huge difference, as creating references to variables is a  
little bit more expensive in terms of processing that simply creating  
a fresh copy of a variable.

The reason you need to create a reference to the $mail object when  
creating an attachment is that it is stored within the Zend_Mail  
class properties, and there's no way to directly manipulate the  
attachment once it's been added unless you return a reference to it.


...and back to Alexander...


to see if it's made a difference, try:-

$at = $mail->addAttachment($picbody);
$at->type = $pictype;
$at->filename = $picfilename;
var_dump($mail);die();

and then

$at =& $mail->addAttachment($picbody);
$at->type = $pictype;
$at->filename = $picfilename;
var_dump($mail);die();

You should see that the attachment in the second has a new filename,  
but the first example will be an untitled 'mime_attachment'.

What happens if you try...

$at = $mail->addAttachment($picbody, Zend_Mime::TYPE_OCTETSTREAM,  
Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64);

I had a few problems when I used default values for addAttachment,  
and the values above fixed that nicely for me.

Cheers

On 24/05/2006, at 8:15 PM, Andries Seutens wrote:

> This looks pretty unusual to me also.
>
> $at = clone $mail->addAttachment($picbody);  // would create a copy
> $at = $mail->addAttachment($picbody); // would create a reference
>
> Am I missing something?


--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  
4124
http://www.peptolab.com


Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Andries Seutens
Simon Mundy schreef:
> Nope, nothing unusual about this. PHP5 will return a copy of a
> variable when returned from a function by default.
Thanks,

This actually makes sense now :-d

Regards,

Andries Seutens
Belgium
http://andries.systray.be
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Derick Rethans-4
In reply to this post by Simon Mundy
On Wed, 24 May 2006, Simon Mundy wrote:

> Nope, nothing unusual about this. PHP5 will return a copy of a variable when
> returned from a function by default.
>
> You can change this behavious by setting the reference in the function call:-
>
> function &getAttachment($a)
> {
> $a = new MailAttachment();
> return $a;
> }
>
> ...now that will return a reference to the variable by default.

This is non-sense. You don't need to return objects by reference as
that's the default. The example above is needed for PHP 4 and NOT PHP 5.

regards,
Derick
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Steven Van Poeck
In reply to this post by Simon Mundy
So Simon,

Sorry for bothering you with this, but I'd like to get this right for myself:

If a function returns a copy of a variable, when does that variable get copied
then ? e.g:

function Test ( $var )
{
    // is $var already a copy of $obj1 or is it a reference to it ?
    return $var;
}

$obj1 = new Foo();
$obj2 = Test($obj1); // $obj2 is a copy of $obj1

Isn't the variable copied when it is passed to the function rather than
when it
is returned from it ? Because otherwise, passing it as a reference wouldn't
help a bit would it ? It would still be returned as a copy.

I'm really puzzled and so far, I haven't found a definitive answer to this
matter...

Steven Van Poeck
http://poekie.free.fr

Quoting Simon Mundy <[hidden email]>:

> Hi Andries,
>
> Nope, nothing unusual about this. PHP5 will return a copy of a  
> variable when returned from a function by default.
>
> You can change this behavious by setting the reference in the  
> function call:-
>
> function &getAttachment($a)
> {
> $a = new MailAttachment();
> return $a;
> }
>
> ...now that will return a reference to the variable by default.
>
> Or you can simply use the syntax provided in the previous example.
>
> It makes a huge difference, as creating references to variables is a  
> little bit more expensive in terms of processing that simply creating
>  a fresh copy of a variable.
>
> The reason you need to create a reference to the $mail object when  
> creating an attachment is that it is stored within the Zend_Mail  
> class properties, and there's no way to directly manipulate the  
> attachment once it's been added unless you return a reference to it.
>
>
> ...and back to Alexander...
>
>
> to see if it's made a difference, try:-
>
> $at = $mail->addAttachment($picbody);
> $at->type = $pictype;
> $at->filename = $picfilename;
> var_dump($mail);die();
>
> and then
>
> $at =& $mail->addAttachment($picbody);
> $at->type = $pictype;
> $at->filename = $picfilename;
> var_dump($mail);die();
>
> You should see that the attachment in the second has a new filename,  
> but the first example will be an untitled 'mime_attachment'.
>
> What happens if you try...
>
> $at = $mail->addAttachment($picbody, Zend_Mime::TYPE_OCTETSTREAM,  
> Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64);
>
> I had a few problems when I used default values for addAttachment,  
> and the values above fixed that nicely for me.
>
> Cheers
>
> On 24/05/2006, at 8:15 PM, Andries Seutens wrote:
>
>> This looks pretty unusual to me also.
>>
>> $at = clone $mail->addAttachment($picbody);  // would create a copy
>> $at = $mail->addAttachment($picbody); // would create a reference
>>
>> Am I missing something?
>
>
> --
>
> Simon Mundy | Director | PEPTOLAB
>
> """ " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
> 202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
> Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  4124
> http://www.peptolab.com
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Andries Seutens
In reply to this post by Derick Rethans-4
Okay, now I'm lost again!

What I said before *is* right then?:

$at = clone $mail->addAttachment($picbody);  // would create a copy
$at = $mail->addAttachment($picbody); // would create a reference

Regards,

Andries Seutens
Belgium
http://andries.systray.be

Derick Rethans schreef:

> On Wed, 24 May 2006, Simon Mundy wrote:
>
>  
>> Nope, nothing unusual about this. PHP5 will return a copy of a variable when
>> returned from a function by default.
>>
>> You can change this behavious by setting the reference in the function call:-
>>
>> function &getAttachment($a)
>> {
>> $a = new MailAttachment();
>> return $a;
>> }
>>
>> ...now that will return a reference to the variable by default.
>>    
>
> This is non-sense. You don't need to return objects by reference as
> that's the default. The example above is needed for PHP 4 and NOT PHP 5.
>
> regards,
> Derick
>
>
>  

Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Derick Rethans-4
On Wed, 24 May 2006, Andries Seutens wrote:

> Okay, now I'm lost again!
>
> What I said before *is* right then?:
>
> $at = clone $mail->addAttachment($picbody);  // would create a copy
> $at = $mail->addAttachment($picbody); // would create a reference

Yes, this is correct.

regards,
Derick
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Andries Seutens
You made my day!

I was starting to doubt myself and asked myself 1 000 000 question. hehe

Thanks for clearing this out Derick

Andries Seutens
Belgium
http://andries.systray.be

Derick Rethans schreef:

> On Wed, 24 May 2006, Andries Seutens wrote:
>
>  
>> Okay, now I'm lost again!
>>
>> What I said before *is* right then?:
>>
>> $at = clone $mail->addAttachment($picbody);  // would create a copy
>> $at = $mail->addAttachment($picbody); // would create a reference
>>    
>
> Yes, this is correct.
>
> regards,
> Derick
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Andries Seutens
In reply to this post by Alexander Hanhikoski
Alexander,

You forgat to modify the encoding (set it to 8bit)

Change this:

$at = $mail->addAttachment($picbody);
$at->type = $pictype;
$at->filename = $picfilename;

To this:

$at = $mail->addAttachment($picBody, $pictype,
Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_8BIT);

Regards,

Andries Seutens
Belgium
http://andries.systray.be/

Alexander Hanhikoski schreef:

> I need to attach a file (image.. jpeg, gif, png, or etc.) received
> from form upload.
>
> Here's the code I use, but it isn't working.
>
>        $mail = new Zend_Mail();
>
>        $mail->setBodyHtml($mailbody);
>
>        $mail->setFrom("[hidden email]", "Testsite");
>        $mail->addTo("[hidden email]", "Receiver");
>
>        $pictmpname = $_FILES['pic']['tmp_name'];
>        $pictype = $_FILES['pic']['type'];
>        $picfilename = $_FILES['pic']['name'];
>        $picsize = $_FILES['pic']['size'];
>
>        $handle = fopen($pictmpname, "rb");
>        $picbody = fread($handle, $picsize);
>        fclose($handle);
>
>        $at = $mail->addAttachment($picbody);
>        $at->type = $pictype;
>        $at->filename = $picfilename;
>
>        $mail->setSubject("Test");
>        $mail->send();
>
>
> "You are doing it wrong!"
> - I know, please help me out. :)
>
> Best regards,
> Alex
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

Simon Mundy
In reply to this post by Derick Rethans-4
Hi Derick

You had me thinking as well, and I had to re-read the PHP manual to  
make sure I wasn't mistaken.

http://www.php.net/manual/en/functions.returning-values.php

So technically I am still correct for return references from function  
calls...

HOWEVER...

(from php.net)

"Since PHP 5, new return reference automatically so using =& in this  
context is deprecated and produces E_STRICT level message."

So yes, the new MailAttachment object will return a reference in that  
instance.

Thanks for clarifying...

Cheers

> On Wed, 24 May 2006, Simon Mundy wrote:
>
>> Nope, nothing unusual about this. PHP5 will return a copy of a  
>> variable when
>> returned from a function by default.
>>
>> You can change this behavious by setting the reference in the  
>> function call:-
>>
>> function &getAttachment($a)
>> {
>> $a = new MailAttachment();
>> return $a;
>> }
>>
>> ...now that will return a reference to the variable by default.
>
> This is non-sense. You don't need to return objects by reference as
> that's the default. The example above is needed for PHP 4 and NOT  
> PHP 5.
>
> regards,
> Derick


--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  
4124
http://www.peptolab.com


Reply | Threaded
Open this post in threaded view
|

Re: Zend_Mail attachments

GavinZend
In reply to this post by Andries Seutens
Thanks Andries,

The second example in the documentation is wrong:
http://framework.zend.com/manual/en/zend.mail.attachments.html
http://framework.zend.com/developer/ticket/110

Would anyone like to volunteer to correct it?
http://framework.zend.com/developer/browser/trunk/documentation/manual/en/module_specs/Zend_Mail.xml

Thanks,
Gavin

Andries Seutens wrote:

> Alexander,
>
> You forgat to modify the encoding (set it to 8bit)
>
> Change this:
>
> $at = $mail->addAttachment($picbody);
> $at->type = $pictype;
> $at->filename = $picfilename;
>
> To this:
>
> $at = $mail->addAttachment($picBody, $pictype,
> Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_8BIT);
>
> Regards,
>
> Andries Seutens
> Belgium
> http://andries.systray.be/
>
> Alexander Hanhikoski schreef:
>> I need to attach a file (image.. jpeg, gif, png, or etc.) received
>> from form upload.
>>
>> Here's the code I use, but it isn't working.
>>
>>        $mail = new Zend_Mail();
>>
>>        $mail->setBodyHtml($mailbody);
>>
>>        $mail->setFrom("[hidden email]", "Testsite");
>>        $mail->addTo("[hidden email]", "Receiver");
>>
>>        $pictmpname = $_FILES['pic']['tmp_name'];
>>        $pictype = $_FILES['pic']['type'];
>>        $picfilename = $_FILES['pic']['name'];
>>        $picsize = $_FILES['pic']['size'];
>>
>>        $handle = fopen($pictmpname, "rb");
>>        $picbody = fread($handle, $picsize);
>>        fclose($handle);
>>
>>        $at = $mail->addAttachment($picbody);
>>        $at->type = $pictype;
>>        $at->filename = $picfilename;
>>
>>        $mail->setSubject("Test");
>>        $mail->send();
>>
>>
>> "You are doing it wrong!"
>> - I know, please help me out. :)
>>
>> Best regards,
>> Alex
>>
>>
>
>