flydev12620
flydev12620

Hello,

I am trying for hours to send an email with an attachment. Sending an email without attachment work fine. (I am newbie with model's things)

What I've done :

The markup code :

    <form enctype="multipart/form-data" data-request-update="calcresult: '#result'" onsubmit="$(this).request('onSubmit'); return false;">
    <input type="text" name="name" placeholder="name"/>
    <input type="text" name="email" placeholder="email"/>
    <input type="file" name="attachment"/>
    <textarea name="msg" cols="30" rows="5" placeholder="some text"></textarea>
    <button type="submit">Submit</button>
    </form>

The PHP code :

    function onSubmit()
    {
    // Get fields
    $data = Input::all();
    $rules = ['name' => 'required', 'email' => 'required|email', 'msg' => 'required'];
    $msgs = ['required' => 'We need all fields', 'email' => 'We need a valid email'];
    $attachment = post('attachment'); // ?? 

    $v = Validator::make($data, $rules, $msgs);

    Log::info(var_export($data, true));

    if ($v->fails())
    {
        $this['result'] = $v->messages()->first();
    }else{
        Mail::queue('code_contact', $data, function($message)
        {
            $message->from(post('email'), post('name'));
            $message->to('noreply@example.com')->subject('New mail received');
            $message->attachData( ???, ??? ); // i dont know how to use and retrieve the path of my uploaded      file.

        });

        $this['result'] = 'mail sent.';
    }
    }

In my event log viewer, I get thoses datas for $data var dump:

    Log::info(var_export($data, true));

result:

    array (
    'name' => 'nameTest',
    'email' => 'test@example.com',
    'msg' => 'message',
    'attachment' =>
    Symfony\Component\HttpFoundation\File\UploadedFile::__set_state(array(
    'test' => false,
    'originalName' => 'logo-1.png',
    'mimeType' => 'image/png',
    'size' => 213858,
    'error' => 0,
    )),
    )

I think I am near the final result of sending this mail with attachment, someone can help me ?

Last updated

flydev12620
flydev12620

Got it, the mail is sent with attachment by adding :

$message->attach( Input::file('attachment') );

Now I just need to find the way to move (rename) the file by its extension and send it as is. By default the mail received contain the file named like this patern : {tmpFileName}.dat

Last updated

Surahman
Surahman

Try this to rename the file in attachment,

$message->attach(Input::file('attachment'), ['as' => 'rename.jpg']);

bishankb.kkaam20476
bishankb.kkaam20476

hlw dont we have to save the uploaded file in database before sending it to someone? can you provide model codes, htm codes and rest codes to me .. thank you

bishankb.kkaam20476
bishankb.kkaam20476
<?php namespace Watchlearn\Contact\Components;

use Cms\Classes\ComponentBase;
use Input;
use Mail;
use Validator;
use Redirect;
use ValidationException;

class ContactForm extends ComponentBase
{

    public function componentDetails(){
        return [
            'name' => 'Contact Form',
            'description' => 'Simple contact form'
        ];
    }

    public function onSend(){

        $data = post();

        $rules = [
            'name' => 'required|min:5',
            'email' => 'required|email'
        ];

        $validator = Validator::make($data, $rules);

        if($validator->fails()){
            throw new ValidationException($validator);
        } else {
            $vars = ['name' => Input::get('name'), 'email' => Input::get('email'), 'content' => Input::get('content')];
            $attachment = post('uploaded_file');

            Mail::send('watchlearn.contact::mail.message', $vars, function($message) {

                $message->to('bishankb.kkaam@gmail.com', 'Admin Person');
                $message->subject('New message from contact form');
                $message->attach( Input::file('uploaded_file') );

            });
        }

    }

}

Last updated

bishankb.kkaam20476
bishankb.kkaam20476

bishankb.kkaam20476 said:

<?php namespace Watchlearn\Contact\Components;

use Cms\Classes\ComponentBase; use Input; use Mail; use Validator; use Redirect; use ValidationException;

class ContactForm extends ComponentBase {

public function componentDetails(){ return [ 'name' => 'Contact Form', 'description' => 'Simple contact form' ]; }

public function onSend(){

   $data = post();

   $rules = [
       'name' => 'required|min:5',
       'email' => 'required|email'
   ];

   $validator = Validator::make($data, $rules);

   if($validator->fails()){
       throw new ValidationException($validator);
   } else {
       $vars = ['name' => Input::get('name'), 'email' => Input::get('email'), 'content' => Input::get('content')];
       $attachment = post('uploaded_file');

       Mail::send('watchlearn.contact::mail.message', $vars, function($message) {

           $message->to('bishankb.kkaam@gmail.com', 'Admin Person');
           $message->subject('New message from contact form');
           $message->attach( Input::file('uploaded_file') );

       });
   }

}

}

Is this the code for sending mail with an uploaded file? can somebody help me? i am stuck for 2 3 days

boutahiibtissam33146
boutahiibtissam33146

To rename file by its real name and extension you can do :

$uploadedFile = Input::file('uploaded_file');
$message->attach($uploadedFile, ['as' => $uploadedFile->getClientOriginalName()]);

Last updated

1-7 of 7