Page 1 of 1
[Solved] Attaching files from the PC rather on the server ??
Posted: Tue May 13, 2008 10:42 pm
by andrewbs
Hi folks
I've been looking for ages for a method of sending emails with attachments, and a friend said to google PHPMailer
Have a solution using that, but currently need to send 2 messages, one with just the file (or as many as need to be sent), and one with the text entered into the "comments/enter info here" box
So, then I found SwiftMailer
I've done a few of the exercises in the Wiki, so I know it's installed just fine, and I can send an email picking up a file on my server, but not from my PC.
Reading through this forum, I've noticed some comments (without much code to help) about uploading the file to the server before it can be attached ?? Also a reference or two about a tutorial/instructions on getting it work with local files, but I can't find it
Am I reading it right ? Seems weird since the PHPMailer thing I'm using doesn't seem to do this (that I can find in the scripts) ??
I'll upload a page shortly showing what I have so far, but if anyone can point me towrds the instructions, then that'd help me quicker (gotta run and do a couple of things so will do the upload in a few hours - or maybe tomorrow)
Re: Attaching files from the PC rather on the server ??
Posted: Tue May 13, 2008 11:00 pm
by Chris Corbyn
So your script lives on a server and you want to attach a file from the user's PC? This isn't possible without doing an upload first... PHP is running server-side and has no access to the user's PC

Re: Attaching files from the PC rather on the server ??
Posted: Tue May 13, 2008 11:36 pm
by andrewbs
that's right Chris
Am putting together the sample code - from the PHPMailer sample, I found a script that has an "Add Attachment" button that dynamically allocates Attachment boxes that are placed into the form, so the user can add as few or as many attachments as required
Am reworking it to try and make it work with SwiftMailer... funnily enough, sometimes I solve my own problems when trying to explain it to others
Nup, well that didn't work, so here is my page, with stuff removed / replaced with generic items to make suitable for posting:
Code: Select all
<?php
error_reporting(E_ALL);
if(isset($_POST['action'])&&$_POST['action']=="send") {
sendMail();
} else {
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Page Title</title>
</head>
<body onload='init()'>
<table width='100%' cellpadding='0' cellspacing='0'>
<tr>
<td bgcolor='#FF0000'>
<center><h2>Email Attachments</h2></center>
</td>
</tr>
<tr>
<td bgcolor='#99cc00'>
</td>
</tr>
</table>
<form enctype="multipart/form-data" name="send" method="post" action="<?php $_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
<input type="hidden" name="emailto" value="me@myisp.com">
<table border='0' cellpadding='3' cellspacing='0' width='100%'>
<tr valign='top'>
<td><center>
<fieldset>
<input type="hidden" name="action" value="send">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
<table border='0' cellpadding='3' cellspacing='0' width='750'>
<tr valign='top'>
<td align='right'>Your Name: </td>
<td align='left'><input name='myname'></td>
</tr>
<tr valign='top'>
<td align='right'>Your E-mail Address: </td>
<td align='left'><input name='email'></td>
</tr>
<tr valign='top'>
<td align='right'>Comments: </td>
<td align='left'><textarea name='body' cols="50" rows="4" wrap="physical"></textarea></td>
</tr>
</table>
</fieldset>
</center>
</td>
</tr>
<tr valign='top'>
<td>
<center>
<fieldset>
<table border='0' cellpadding='3' cellspacing='0' width='750'>
<tbody id="emailTBody">
<tr><td></td></tr>
<tr><td></td></tr>
<tr valign='top'>
<td align='right'> Attachment: </td>
<td align='left'>
<!-- <input type="file" name="attachment"/>-->
<input type="button" name="add_attachment" value="Add attachment" onClick="addAttachment()">
</td>
<tr valign='top'>
<td></td>
<td>
<input type="submit" value="Send Email">
</td>
</tr>
</table>
</fieldset>
</center>
</td>
</tr>
</table>
</form>
<script type="text/javascript" language="javascript">
var trow;
function init()
{
var sx = (screen.availWidth - 550)/2;
var sy = (screen.availHeight - 500)/3;
if (sx < 0) { sx = 0; }
if (sy < 0) { sy = 0; }
window.moveTo(sx,sy);
window.resizeTo(590,500);
trow = 2;
}
function addAttachment()
{
var tbodyElem = document.getElementById("emailTBody");
var trElem, tdElem, txtNode, tmpString;
trElem = tbodyElem.insertRow(trow);
tdElem = trElem.insertCell(0);
tdElem.className = "gradient";
tdElem.setAttribute("style", "text-align: right;");
txtNode = document.createTextNode("Attachment " + (trow - 1) + ":");
tdElem.appendChild(txtNode);
tdElem = trElem.insertCell(1);
tdElem.className = "gradient";
tdElem.setAttribute("style", "text-align: left;");
tdElem.setAttribute("colspan", "3");
var input = document.createElement("input");
input.setAttribute("type", "file");
input.setAttribute("name", "attachment[]");
input.setAttribute("size", "60");
input.setAttribute("value", "");
tdElem.appendChild(input);
trow++;
}
</script>
</body>
</html>
<?php
}
function sendMail() {
$body = stripslashes($_POST['body']);
$to_email = stripslashes($_POST['emailto']);
$myname = stripslashes($_POST['myname']);
$email = stripslashes($_POST['email']);
$subject = "Attachment email";
require_once("lib/Swift.php");
require_once("lib/Swift/Connection/SMTP.php");
require_once("lib/Swift/Connection/NativeMail.php");
$swift =& new Swift(new Swift_Connection_SMTP("my smtp server"));
$from =& new Swift_Address("$email","$myname");
$emailto =& new Swift_Address("$to_email");
$message =& new Swift_Message($subject);
$msgbdy = "<font face='verdana,tahoma,arial'>Hi<br />
<br />
<br />
I entered the following into the Comments Box there<br />
<br>
=============================================<br>
$body.<br>
=============================================<br>
<br>
<br />
And have attached some files for you too<br>
<br>
<br>
Kind regards.<br />
$myname<br />
";
$message->attach(new Swift_Message_Part("$msgbdy","text/html"));
// this would successfully attach a file called "spacer.gif" if in the same directory as this page
// $message->attach(new Swift_Message_Attachment(file_get_contents("spacer.gif"),"spacer.gif"));
$filecount = 0;
foreach ($_FILES["attachment"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
<code needs to go here !!!!!!!!!!>
$message->attach(new Swift_Message_Attachment(new Swift_File(" ??? ")," ??? "));
}
}
//Now check if Swift actually sends it
if ($swift->send($message, $emailto, $from)) echo "Email Sent";
}
?>
Re: Attaching files from the PC rather on the server ??
Posted: Wed May 14, 2008 12:37 am
by andrewbs
found it Chris - elsewhere in the forums. A thread was talking about just uploading files, so I worked it out, and then to make it email the file
Is there a way to then have the script delete the file after attaching it ??
The bit where it says CODE GOES IN HERE (right near the bottom) with some surrounding code, repeated/modified, is to be replaced with (note, my uploads directory is 2 levels up, hence the ../../
Code: Select all
$filecount = 0;
foreach ($_FILES["attachment"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["attachment"]["tmp_name"][$key];
$oname = $_FILES["attachment"]["name"][$key];
$name = "../../uploads/$oname";
move_uploaded_file($tmp_name,$name);
$result = $message->attach(new Swift_Message_Attachment(new Swift_File("$name"),"$oname"));
if ($result == false) { echo "\n Adding $name failed !\n"; }
}
}
//Now check if Swift actually sends it
Thanks for the Forum Chris, some great info here, and of course SwiftMailer too
(I'll put a Solved in the subject line once I get the "Delete file" code - or is as simple as writing
delete_file("$name");)
Re: Attaching files from the PC rather on the server ??
Posted: Wed May 14, 2008 6:36 pm
by andrewbs
found it... the unlink command
A second loop needs to be done, similar in iteration as the attaching files loop
But needs to be placed after the email is sent.
Thanks for SwiftMailer Chris

Re: [Solved] Attaching files from the PC rather on the server ??
Posted: Wed May 14, 2008 9:00 pm
by Chris Corbyn
Talking to yourself on a forum eh?
Glad you got it sorted
