Page 1 of 3
[SOLVED] Attachment problems with swiftmailer
Posted: Tue Apr 17, 2007 5:06 pm
by AshP
Hi all,
I will hold my hands up and say now that I am quite new to all this PHP & coding banter, however I am a fast learner, have come a long way in the last few weeks & would be very grateful of any help you can give me!
Basically, I am trying to use swiftmailer to email uploaded forms including multiple attachments.
I have followed the tutorial on the swiftmailer website (for creating a form & emailing its contents) and managed to get the email part working with no problems (eventually...) however, after quite a few hours now i am still struggeling to get attachments to work at all.
I think this may possibly be due to the way in which I am uploading the attachments in the first place, as I am using Rad upload for the attachments, as opposed to just using the standard method for form attachment. This unfortunatly is nessecary as the end product needs to be as simple to use as possible (Rad is very easy to use with drag & drop interface).
The best I have done so far is to receive an error message:
>Fatal error: Uncaught exception 'Swift_FileException' with message 'No such file 'Array'' in /public_html/swift/Swift/File.php:63Stack trace:#0 /public_html/swift/Swift/File.php(51): Swift_File->setPath(Array)#1 /public_html/web/upload.php(127): Swift_File->__construct(Array)#2 {main} thrown in /public_html/swift/Swift/File.php on line 63 >
I am guessing the problem is with the way in which rad uploads the data, however the end product of rad is that I end up with a copy of any uploaded files in the php /tmp/ directory and an additional directory within the website /uploaded/ - as far as i am aware, swiftmailer uses the php tmp/ file so I am not quite sure as to why it does not work??
If anyone could help me get this going i would be very grateful indeed as it is starting to give me a headache!
thanks for any response[/quote]
Posted: Tue Apr 17, 2007 5:15 pm
by John Cartwright
posting your code helps

Posted: Tue Apr 17, 2007 5:33 pm
by AshP
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Yeh, I thought it might!
OK here is the form.html
[syntax="html"]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Upload</title>
<style type="text/css">
<!--
.style1 {font-family: Tahoma}
.style3 {font-family: Tahoma; color: #FFFFFF; }
.style7 {font-size: 16px}
.style8 {font-size: 10px}
-->
</style>
</head>
<script type="text/javascript" src="script.js">
</script>
<body>
<table width="600" border="0" cellpadding="0" cellspacing="0" valign="middle">
<tr>
<td width="600" valign="top" bgcolor="#9900FF"></td>
</tr>
<tr>
<td bgcolor="#9900FF"><form action="upload.php" method="post" enctype="multipart/form-data" name="data" target="mainFrame" class="style7">
<blockquote>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="top"><span class="style3 ">userid:</span></td>
<td align="left" valign="middle"><span style="width:500px; height:40px"><span class="style3 ">
<input name="userid" type="text" id="userid" size="6" maxlength="6">
<span class="style8">(provided by us when you create an account)</span> </span></span></td>
</tr>
<tr>
<td align="left" valign="top"><span class="style3 ">email:</span></td>
<td align="left" valign="middle"><span style="width:500px; height:40px"><span class="style3 ">
<input name="email" type="text" id="email" value="youremail@youraddress.com" size="40">
<span class="style8">(your email address) </span></span></span></td>
</tr>
<tr>
<td align="left" valign="top"><span class="style3 ">service:</span></td>
<td align="left" valign="middle"><span style="width:500px; height:40px"><span class="style3 ">
<select name="service" id="service">
<option value="Goad Only">Interactive Goad</option>
<option value="Full Particulars">Full Particulars</option>
<option value="Requirements">Requirements</option>
</select>
</span></span></td>
</tr>
<tr>
<td align="left" valign="top"><span class="style3 ">timing:</span></td>
<td align="left" valign="middle"><span style="width:500px; height:40px"><span class="style3 ">
<select name="timing" id="timing">
<option value="4">Standard (24 hours)</option>
<option value="2">Urgent (Same Day)</option>
</select>
</span></span></td>
</tr>
<tr>
<td align="left" valign="top"><span class="style3 ">photo 1: </span></td>
<td align="left" valign="middle"><span style="width:500px; height:40px"><span class="style3 ">
<input name="photo_1" type="text" id="photo_1" size="40">
<span class="style8">(location of where photo 1 is taken from) </span></span></span></td>
</tr>
<tr>
<td align="left" valign="top"><span class="style3 ">photo 2: </span></td>
<td align="left" valign="middle"><span style="width:500px; height:40px"><span class="style3 ">
<input name="photo_2" type="text" id="photo_2" size="40">
<span class="style8">(location of where photo 2 is taken from) </span></span></span></td>
</tr>
<tr>
<td align="left" valign="top"><span class="style3 ">photo 3: </span></td>
<td align="left" valign="middle"><span style="width:500px; height:40px"><span class="style3 ">
<input name="photo_3" type="text" id="photo_3" size="40">
<span class="style8">(location of where photo 3 is taken from) </span></span></span></td>
</tr>
<tr>
<td align="left" valign="top"><span class="style3 ">photo 4: </span></td>
<td align="left" valign="middle"><span style="width:500px; height:40px"><span class="style3 ">
<input name="photo_4" type="text" id="photo_4" size="40">
<span class="style8">(location of where photo 4 is taken from) </span></span></span></td>
</tr>
<tr>
<td align="left" valign="top"><span class="style3 ">video:</span></td>
<td align="left" valign="middle"><span style="width:500px; height:40px"><span class="style3 ">
<input name="video" type="text" id="video" size="40">
<span class="style8">(location of where video is taken from) </span></span></span></td>
</tr>
</table>
</blockquote>
<div style="border: 0px solid #336699; paddding:0px">
<div align="center">
<script language="javascript" src="embed.js"></script>
<script language="javascript">
var _info = navigator.userAgent;
var ie = (_info.indexOf("MSIE") > 0);
var win = (_info.indexOf("Win") > 0);
if(win)
{
if(ie)
{
IELoader();
}
else
{
document.writeln('<object type="application/x-java-applet;version=1.4.1"');
document.writeln('width= "600" height= "290" id="rup" >');
document.writeln('<param name="archive" value="dndplus.jar">');
document.writeln('<param name="code" value="com.radinks.dnd.DNDAppletPlus">');
document.writeln('<param name="name" value="Rad Upload Plus">');
}
}
else
{
/* mac and linux */
document.writeln('<applet ');
document.writeln(' archive = "dndplus.jar"');
document.writeln(' code = "com.radinks.dnd.DNDAppletPlus"');
document.writeln(' name = "Rad Upload Plus"');
document.writeln(' hspace = "0"');
document.writeln(' vspace = "0" MAYSCRIPT="yes"');
document.writeln(' width = "600"');
document.writeln(' height = "290"');
document.writeln(' align = "middle" id="rup">');
}
</script>
<!-- BEGIN APPLET CONFIGURATION PARAMETERS -->
<param name="max_upload" value="10000">
<!-- size in kilobytes (takes effect only in Rad Upload Plus) -->
<param name = "url" value = "http://www.id.co.uk/web/upload.php">
<!-- you can pass additional parameters by adding them to the url-->
<!-- to upload to an ftp server instead of a web server, please specify a url
in the following format:
ftp://username:password@ftp.myserver.com
replacing username, password and ftp.myserver.com with corresponding entries for
your site -->
<param name = "message" value="http://www.id.co.uk/dragdrop.html">
<!-- edit the above line to customize your applet's display -->
<!-- END APPLET CONFIGURATION PARAMETERS -->
document.writeln('
<param name="MAYSCRIPT" value="true">
');
document.writeln('
<param name="scriptable" value="true">
');
<script language="javascript">
<!--
if(win)
{
document.writeln('</object>');
}
else
{
document.writeln('</applet>');
}
//-->
</script></form>
</td>
</tr>
<br>
</table>
</body>
</html>
and here is the upload.php[/syntax]
Code: Select all
<html>
<head><title>Uploaded Files</title></head>
<body bgcolor="FFFFCC">
Welcome user id: <? echo $_POST["userid"]; ?>
<table border="1" cellpadding="5" width="100%" align="center" style="margin:3px">
<tr><td colspan="2" bgcolor="#0066cc"><font color="#FFFFCC" size="+1" align="center">Files Uploaded</font></td></tr>
<tr bgcolor="#ffff00"><td><nobr>File Name</nobr></td>
<td align="right"><nobr>File size</nobr></td></tr>
<?
/*
* SET THE SAVE PATH by editing the line below. Make sure that the path
* name ends with the correct file system path separator ('/' in linux and
* '\\' in windows servers (eg "c:\\temp\\uploads\\" )
*/
$save_path="/public_html/tmp/";
$file = $_FILES['userfile'];
$k = count($file['name']);
for($i=0 ; $i < $k ; $i++)
{
if($i %2)
{
echo '<tr bgcolor="#FFFF99"> ';
}
else
{
echo '<tr>';
}
$shortName = split('/',urldecode($file['name'][$i]));
echo '<td align="left">' . urldecode($file['name'][$i]) ."</td>\n";
echo '<td align="right">' . $file['size'][$i] ."</td></tr>\n";
if(isset($save_path) && $save_path!="")
{
$name = $shortName;
move_uploaded_file($file['tmp_name'][$i], $save_path . $name[count($name)-1]);
}
}
echo "<tr style='color: #0066cc'><td>SSL</td><td>". (($_SERVER[HTTPS] != 'on') ? 'Off' : 'On') ."</td></tr>";
if(! isset($save_path) || $save_path =="")
{
echo '<tr style="color: #0066cc"><td colspan=2 align="left">Files have not been saved, please edit upload.php to match your configuration</td></tr>';
}
echo "<tr style='color: #0066cc'><td colspan=2>Top level folder hint : $userfile_parent</td></tr>";
//Start of swiftemail code
//
//
//
//
//Copy into global variables
$name = "Do Not Reply";
$email = "mailer@id.co.uk";
$title = $_POST["userid"];
$title2 = $_POST["timing"];
$body = $_POST["service"];
$pic1 = $_POST["photo_1"];
$pic2 = $_POST["photo_2"];
$pic3 = $_POST["photo_3"];
$pic4 = $_POST["photo_4"];
$vid = $_POST["video"];
//Everything looks ok, we can start Swift
require_once "../swift/Swift.php";
require_once "../swift/Swift/Connection/SMTP.php";
//Enable disk caching if we can
if (is_writable("/tmp"))
{
Swift_CacheFactory::setClassName("Swift_Cache_Disk");
Swift_Cache_Disk::setSavePath("/tmp");
}
//Create a Swift instance
$swift =& new Swift(new Swift_Connection_SMTP("mail.id.co.uk"));
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
//Create the message to send
$message =& new Swift_Message($title2 . "/" . $title);
$message->attach(new Swift_Message_Part($body . $pic1 . $pic2 . $pic3 . $pic4 . $vid));
//If an attachment was sent, attach it
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["userfile"]["tmp_name"]))
{
if ($_FILES["userfile"]["error"])
$file_path = $_FILES["userfile"]["tmp_name"];
$file_name = $_FILES["userfile"]["name"];
$file_type = $_FILES["userfile"]["type"];
}
//attachment code
if ($file_path && $file_name && $file_type)
{
$message->attach(
new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
//Try sending the email
$sent = $swift->send($message, "ashley@id.co.uk", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
?>
</table>
</body>
</html>
There are of course other files used by rad upload, mainly the large javascirpt file...
Any ideas?
Thanks for the quick response!
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Wed Apr 18, 2007 12:59 am
by Chris Corbyn
$file_path is being passed as "Array" which suggests it is an array that has been cast to a string. RAD upload must be capable of sending multiple files which results in an array for each value or something. What does the following show if you put it just above the line I've copied in:
Code: Select all
var_dump($_FILES);
var_dump($file_path);
//attachment code
if ($file_path && $file_name && $file_type)
{
Posted: Wed Apr 18, 2007 3:08 am
by AshP
Hi thanks for the response.
Added your 2 lines of code and the following came up:
array(1) { ["userfile"]=> array(5) { ["name"]=> array(1) { [0]=> string(12) "DSC00035.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phplaBEeq" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(123550) } }}array(1) { [0]=> string(14) "/tmp/phplaBEeq"} >Fatal error: Uncaught exception 'Swift_FileException' with message 'No such file 'Array'' in /public_html/swift/Swift/File.php:63Stack trace:#0 /public_html/swift/Swift/File.php(51): Swift_File->setPath(Array)#1 /public_html/web/upload.php(129): Swift_File->__construct(Array)#2 {main} thrown in /public_html/swift/Swift/File.php on line 63 >
Does not really mean much to me, hopefully it means something to one of you guys!

Posted: Wed Apr 18, 2007 3:31 am
by Chris Corbyn
Yes, RAD upload is sending in a way that would allow multiple files. You'll need to change all the $_FILES["attachment"][" .. "] to $_FILES["attachment"][" .. "][0]. What a pita
Code: Select all
//If an attachment was sent, attach it
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["userfile"]["tmp_name"][0]))
{
if (!$_FILES["userfile"]["error"][0])
{
$file_path = $_FILES["userfile"]["tmp_name"][0];
$file_name = $_FILES["userfile"]["name"][0];
$file_type = $_FILES["userfile"]["type"][0];
}
}
I also fix the strange if() statement you had that was doing nothing at all

Posted: Wed Apr 18, 2007 3:45 am
by AshP
I copied and pasted your code into mine, but Im still getting the same error
I had a look that the upload.php file to see if there were any more lines which could be changed from $_FILES["userfile"][" .. "] to $_FILES["userfile"][" .. "][0] but could not find any!
Am I being stupid? (Please feel free to say yes)
Appreciate your help dude, cheers!
Posted: Wed Apr 18, 2007 3:55 am
by Chris Corbyn
I'm not sure how you can be getting that error now (at least, not the same error message). Post your ammended code please

Posted: Wed Apr 18, 2007 4:06 am
by AshP
It's probably me doing somthing stupid but here is upload.php:
Code: Select all
<html>
<head><title>Uploaded Files</title></head>
<body bgcolor="FFFFCC">
Welcome user id: <? echo $_POST["userid"]; ?>
<table border="1" cellpadding="5" width="100%" align="center" style="margin:3px">
<tr><td colspan="2" bgcolor="#0066cc"><font color="#FFFFCC" size="+1" align="center">Files Uploaded</font></td></tr>
<tr bgcolor="#ffff00"><td><nobr>File Name</nobr></td>
<td align="right"><nobr>File size</nobr></td></tr>
<?
/*
* SET THE SAVE PATH by editing the line below. Make sure that the path
* name ends with the correct file system path separator ('/' in linux and
* '\\' in windows servers (eg "c:\\temp\\uploads\\" )
*/
$save_path="/public_html/tmp/";
$file = $_FILES['userfile'];
$k = count($file['name']);
for($i=0 ; $i < $k ; $i++)
{
if($i %2)
{
echo '<tr bgcolor="#FFFF99"> ';
}
else
{
echo '<tr>';
}
$shortName = split('/',urldecode($file['name'][$i]));
echo '<td align="left">' . urldecode($file['name'][$i]) ."</td>\n";
echo '<td align="right">' . $file['size'][$i] ."</td></tr>\n";
if(isset($save_path) && $save_path!="")
{
$name = $shortName;
move_uploaded_file($file['tmp_name'][$i], $save_path . $name[count($name)-1]);
}
}
echo "<tr style='color: #0066cc'><td>SSL</td><td>". (($_SERVER[HTTPS] != 'on') ? 'Off' : 'On') ."</td></tr>";
if(! isset($save_path) || $save_path =="")
{
echo '<tr style="color: #0066cc"><td colspan=2 align="left">Files have not been saved, please edit upload.php to match your configuration</td></tr>';
}
echo "<tr style='color: #0066cc'><td colspan=2>Top level folder hint : $userfile_parent</td></tr>";
//Start of swiftemail code
//
//
//
//
//Copy into global variables
$name = "Do Not Reply";
$email = "mailer@id.co.uk";
$title = $_POST["userid"];
$title2 = $_POST["timing"];
$body = $_POST["service"];
$pic1 = $_POST["photo_1"];
$pic2 = $_POST["photo_2"];
$pic3 = $_POST["photo_3"];
$pic4 = $_POST["photo_4"];
$vid = $_POST["video"];
//Everything looks ok, we can start Swift
require_once "../swift/Swift.php";
require_once "../swift/Swift/Connection/SMTP.php";
//Enable disk caching if we can
if (is_writable("/tmp"))
{
Swift_CacheFactory::setClassName("Swift_Cache_Disk");
Swift_Cache_Disk::setSavePath("/tmp");
}
//Create a Swift instance
$swift =& new Swift(new Swift_Connection_SMTP("mail.id.co.uk"));
//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
//Create the message to send
$message =& new Swift_Message($title2 . "/" . $title);
$message->attach(new Swift_Message_Part($body . $pic1 . $pic2 . $pic3 . $pic4 . $vid));
//If an attachment was sent, attach it
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["userfile"]["tmp_name"][0]))
{
if (!$_FILES["userfile"]["error"][0])
{
$file_path = $_FILES["userfile"]["tmp_name"][0];
$file_name = $_FILES["userfile"]["name"][0];
$file_type = $_FILES["userfile"]["type"][0];
}
}
var_dump($_FILES);
var_dump($file_path);
//attachment code
if ($file_path && $file_name && $file_type)
{
$message->attach(
new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
//Try sending the email
$sent = $swift->send($message, "ashley@id.co.uk", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
?>
</table>
</body>
</html>
and for good measure here is the error:
array(1) { ["userfile"]=> array(5) { ["name"]=> array(1) { [0]=> string(12) "DSC00035.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpiGCu7B" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(123550) } }}string(14) "/tmp/phpiGCu7B" >Fatal error: Uncaught exception 'Swift_FileException' with message 'No such file '/tmp/phpiGCu7B'' in /public_html/swift/Swift/File.php:63Stack trace:#0 /public_html/swift/Swift/File.php(51): Swift_File->setPath('/tmp/phpiGCu7B')#1 /public_html/web/upload.php(131): Swift_File->__construct('/tmp/phpiGCu7B')#2 {main} thrown in /public_html/swift/Swift/File.php on line 63 >
When are you off to oz?

my bro is out there at the moment lucky bugger!
Posted: Wed Apr 18, 2007 4:25 am
by Chris Corbyn
Does this RAD upload move the files after they're uploaded or something? There's no error in the code, it's looking for the file where PHP thinks it is "/tmp/phplaBEeq" but something has removed it.
What exactly is this RAD upload thing and why are you using it rather than just doing a standard upload? I'm not familiar with it.
EDIT | I'm off to Oz 29th September. I'd go now but I can't really leave my employer in the lurch and I haven't saved enough money to do a full 2 months pre-work traveling yet

Posted: Wed Apr 18, 2007 4:45 am
by AshP
As I said before, unfortunatly rad upload is a bit of a necessity as it provides drag and drop multiple file uploading and also compresses the files being uploaded which are both nessecary for this website. (unless you know of a better way)
As rad uploads the file(s) it obviously uses the tmp file on the server, however the end product is a copy of the files in the /uploaded/ directory on the website.
Is there anyway to configure swift to attach these completed files once Rad has done its thing?
Really appreciate your help bud, if we get it working ill send you some beer money!

wish i was off to oz - just got an email from my bro who's on his way to the great barrier reef!!
Posted: Wed Apr 18, 2007 4:48 am
by Chris Corbyn
AshP wrote:As I said before, unfortunatly rad upload is a bit of a necessity as it provides drag and drop multiple file uploading and also compresses the files being uploaded which are both nessecary for this website. (unless you know of a better way)
As rad uploads the file(s) it obviously uses the tmp file on the server, however the end product is a copy of the files in the /uploaded/ directory on the website.
Is there anyway to configure swift to attach these completed files once Rad has done its thing?
Really appreciate your help bud, if we get it working ill send you some beer money!

wish i was off to oz - just got an email from my bro who's on his way to the great barrier reef!!
If you know where the end copy goes then just change:
Code: Select all
//If an attachment was sent, attach it
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["userfile"]["tmp_name"][0]))
{
if (!$_FILES["userfile"]["error"][0])
{
$file_path = $_FILES["userfile"]["tmp_name"][0];
$file_name = $_FILES["userfile"]["name"][0];
$file_type = $_FILES["userfile"]["type"][0];
}
}
To
Code: Select all
//If an attachment was sent, attach it
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["userfile"]["tmp_name"][0]))
{
if (!$_FILES["userfile"]["error"][0])
{
$file_path = /* wherever the file is */;
$file_name = $_FILES["userfile"]["name"][0];
$file_type = $_FILES["userfile"]["type"][0];
}
}
You may also need to change the $file_name and $file_type if RAD adjusts those values too. They won't stop it from sending the attachment, but they might make it appear corrupt if RAD has, say for example turned a JPEG into a ZIP.
Posted: Wed Apr 18, 2007 5:21 am
by AshP
ok i am making progress...
have changed the
Code: Select all
//If an attachment was sent, attach it
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["userfile"]["tmp_name"][0]))
{
if (!$_FILES["userfile"]["error"][0])
{
$file_path = $_FILES["userfile"]["tmp_name"][0];
$file_name = $_FILES["userfile"]["name"][0];
$file_type = $_FILES["userfile"]["type"][0];
}
}
to
Code: Select all
//If an attachment was sent, attach it
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["userfile"]["tmp_name"][0]))
{
if (!$_FILES["userfile"]["error"][0])
{
$file_path = $save_path;
$file_name = $_FILES["userfile"]["name"][0];
$file_type = $_FILES["userfile"]["type"][0];
}
}
The attachment now works - however - as you predicted, the file is corrupt when it comes through.
Interestingly enough though the filename and type come through as the attachment.
eg i upload pic1.jpeg and it comes through as an attachment pic1.jpeg but is corrupt(and really small).
I thought this would not be the case as the $file_name & $file_type would be wrong?
Posted: Wed Apr 18, 2007 6:08 am
by Chris Corbyn
Is $save_path a filename or a directory?
Posted: Wed Apr 18, 2007 6:11 am
by AshP
$save_path is defined near the top of the php code - its the directory that uploaded files are saved to...
I have been looking at the Rad documentation regarding upload handeling, does this make any sense to you?
http://www.radinks.com/upload/upload.php