PHP, Swift-mailer problem
Asked Answered
G

4

5

When I click submit button on my contact page, trying to submit a form that uses swift-mailer, I get this:

Warning: fopen(uploads/) [function.fopen]: failed to open stream: No such file or directory in /home/polycys2/public_html/html/swift-mailer/lib/classes/Swift/ByteStream/FileByteStream.php on line 131

Fatal error: Uncaught exception 'Swift_IoException' with message 'Unable to open file for reading [uploads/]' in /home/polycys2/public_html/html/swift-mailer/lib/classes/Swift/ByteStream/FileByteStream.php:133

Stack trace: 
#0 /home/polycys2/public_html/html/swift-mailer/lib/classes/Swift/ByteStream/FileByteStream.php(77): Swift_ByteStream_FileByteStream->_getReadHandle()
#1 /home/polycys2/public_html/html/swift-mailer/lib/classes/Swift/Mime/SimpleMimeEntity.php(660): Swift_ByteStream_FileByteStream->read(8192) 
#2 /home/polycys2/public_html/html/swift-mailer/lib/classes/Swift/Mime/SimpleMimeEntity.php(337): Swift_Mime_SimpleMimeEntity->_readStream(Object(Swift_ByteStream_FileByteStream)) 
#3 /home/polycys2/public_html/html/swift-mailer/lib/classes/Swift/Mime/SimpleMimeEntity.php(448): Swift_Mime_SimpleMimeEntity->getBody()
#4 /home/polycys2/public_html/html/swift-mailer/lib/classes/Swift/Mime/SimpleMimeEntity.php(463): Swift_Mime_SimpleMimeEntity->toString()
#5 /home/polycys2/public_html/html/swift-mailer/lib/classes/Swift/ in /home/polycys2/public_html/html/swift-mailer/lib/classes/Swift/ByteStream/FileByteStream.php on line 133

The full path exists on the server, although the error message says 'no such file or directory. What could be the problem? Thank you all in advance! And merry Christmas!

Giusto answered 25/12, 2010 at 18:23 Comment(4)
uploads/ will be relative to the current working directory. What is your working directory in that script? Try getcwd().Sinistrad
So uploads/ is a file?Despot
@Pekka: Either way, opening a directory (e.g.: fopen('/usr', 'r'); does not yield an error. However, it probably won't do much even if it exists.Sinistrad
@Sinistrad Swift seems to explicitly request a file (see the exception's error message)Despot
I
9

You need to supply the full path to uploads not a realtive path because that is going to be interpreted as relative to class file that tries to open your attachment.

Try using realpath before passing the path to Swift.

$path = realpath('uploads/');

Inanimate answered 25/12, 2010 at 18:30 Comment(1)
I had a similar issue with Laravel, using SwiftMailer and Twig and the message.embed() function. I didn't realize that the function was using the overall scripts context and working directory, as opposed to the Twig context and base dir. I had to make the paths relative to overall context, i.e., laravel's public/ dir, instead of the Twig template context and base dir, which I was using without thinking.Calif
G
1

Thank you all for your help! The problem was my absentmindedness - I forgot to create 'upload' directory there. Now it works fine. Thank you!

Giusto answered 25/12, 2010 at 18:40 Comment(0)
K
1

This works for me:

Controller.php

$mail = Yii::$app->mailer->compose('@app/mail/embed-mail',
[
     'term' => Url::to('@webroot/images/term.png'),
     'logo' => Url::to('@webroot/images/LOGO.jpg')
]

And embed-mail.php

<img src="<?= $message->embed($terminal); ?>"/>
<img src="<?= $message->embed($logo); ?>"/>

I hope this works for you

Knives answered 11/4, 2019 at 14:35 Comment(0)
D
0

From the error messages, it seems as if you were specifying a directory where a file path is needed. Maybe because of a missing variable that is supposed to contain the file name.

Despot answered 25/12, 2010 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.