Jump to content

Email attachment


Sava
 Share

Recommended Posts

I am having a bit of a problem with my form trying to send an attachment from the upload form and  I would really appreciate help. 🙂

Frontend code:
 

<div class="uk-margin">
  <label class="uk-form-label" for="form-stacked-text">Name</label>
    <input id="name" name="name"  class="uk-input uk-border-rounded" type="text">
</div>

<div class="uk-margin">
  <label class="uk-form-label" for="form-stacked-text">Your images</label>
   <input type="file" multiple>
</div>

Backend code:

if ($input->post('name')) {
    $name = $input->post->name;
    $to = 'info@example.com';
    $subject = 'Contact';
    $message = "Name: " . $name;
    $headers = "From:" . $email;
    $mail = wireMail();
    $mail->to($to);
    $mail->subject($subject);
    $mail->body($message);
    $mail->send();
    $session->redirect($page->url);
}

I tried the WireMail::attachment() method but no success

$m = wireMail();
$m->to('user@domain.com')->from('hello@world.com');
$m->subject('Test attachment');
$m->body('This is just a test of a file attachment');
$m->attachment('/path/to/file.jpg');
$m->send();

 

Link to comment
Share on other sites

Hi,

without seeing the full form code it's hard to answer so don't take it the wrong way 🙂
the first thing to check is that your form has the correct enctype="multipart/form-data" attribute
then, have you checked the form really uploads the file into the right folder? i personally use native php move_uploaded_file, example for a simple file
 

$cv = nom_fichier($_FILES['cv']['name']); // nom_fichier is a personal sanitizing function, yes, i'm a frenchie :) but you can use pw great one
if ($cv != "") {
    move_uploaded_file($_FILES['cv']['tmp_name'], $filefolder . $cv);
    chmod($filefolder . $cv, 0777);
}

and then zip it server side (security...) and attach the zip file with wiremail attachment before deleting it after sending the mail

but you can see a full pw example here https://gist.github.com/somatonic/5233338

one thing i can say for sure, $m->attachment works fine, i often use it for application form with two compuslatory file fields (that end in the same zip server side :))

hope it helps

have a nice day

  • Like 2
Link to comment
Share on other sites

Thanks @virtualgadjo for the help. I modified some of the code from the example and this works perfectly


        // Create a new instance of the wireMail class
        $m = wireMail();
        
        // Set the email parameters
        $m->to('test@test.com')->from('test@test.com');
        $m->subject('Test attachments');
        $m->body($message);
        
        // Loop through the uploaded files and add them as attachments to the email
        foreach($files as $filename) {
            $m->attachment($upload_path . $filename);
        }
        
        // Send the email with all the attachments
        $m->send();
        
        foreach($files as $filename) { unlink($upload_path . $filename);}

 have a nice day

Link to comment
Share on other sites

Hi

good to know it works for you, as pw always do actually 😄

coming to your image issue, in order to make this kind of input work with preview, deletion and so on, you'll have to use ajax, the choice is quite simple, jquery or not jquery 🙂
with jquery you'll find a lot o working plugins, the famous oldie one blueimp but many others too
without jquery, you'll mainly find code examples you can extend to implement the functionalities you need, deletion for example

if your form is a user profile settings form, you may also have a look at pw login register pro module that comes with an very interesting file/image input

in any case, i'm sure you know that when you allow uploading files from the front side, security is the main thing to keep hardly focused on...

have a nice day

Link to comment
Share on other sites

  • 5 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...