Laravel 8 -将通过表单传递的文件附加到电子邮件

xghobddn  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(121)

我正在一个现有的网站上实现一个“与我们合作”的功能,在这个功能中,我允许用户在一个表单中上传他们的简历,然后我必须将这个PDF文件附加到电子邮件中。
然后我配置了表单和控制器,但是当我尝试将文件附加到电子邮件时,什么也没有发生。也就是说,我正确地收到了电子邮件,但没有附加文件。
你有什么建议或忠告吗?我做错了什么吗?
谢谢大家的建议。

  • 我的表单:
<form action="{{url('/lavoraconnoi')}}" method="POST" enctype="multipart/form-data">
                        @csrf
                        @method('post')
                        <!--<input type=hidden name="oid" value="00D09000007lBWC">-->
                        <!--<input type=hidden name="retURL" value="https://mercurioservice.it/grazie">-->

                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="first_name">Nome *</label>
                                <input required class="form-control" id="first_name" maxlength="40" name="first_name"
                                    size="20" type="text" />
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="last_name">Cognome *</label>
                                <input required class="form-control" id="last_name" maxlength="80" name="last_name"
                                    size="20" type="text" />
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label for="email">Email *</label>
                                <input required class="form-control" id="email" maxlength="80" name="email" size="20"
                                    type="email" />
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label for="cell">Telefono *</label>
                                <input required class="form-control" id="cellulare" maxlength="80" name="cellulare"
                                    type="text" />
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label for="city">Città *</label>
                                <input required class="form-control" id="city" maxlength="40" name="city" size="20"
                                    type="text" />
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="form-group">
                                <label for="state">Stato/Provincia *</label>
                                <input required class="form-control" id="state" maxlength="20" name="state" size="20"
                                    type="text" />
                            </div>
                        </div>

                        <div class="col-md-9">
                            <div class="form-group">
                                <label for="sel1">Posizione di interesse:</label>
                                <select class="form-control" required id="00N0900000Fk6kM" name="00N0900000Fk6kM"
                                    title="Servizi di Interesse">
                                    <option value="SPON">--Nessuno--</option>
                                    <option value="JAM">Junior Account Manager</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-9">
                            <div class="form-group">
                                <label for="cv">Carica il tuo CV:</label>
                                <input type="file" class="form-control" name="cv" placeholder="Carica il tuo CV" />
                            </div>
                        </div>

                        {{-- <div class="col-md-12">
                            <div class="form-group">
                                <br />
                                <div class="g-recaptcha" data-sitekey="6Lc8j9gbAAAAAP_szxZh5CNBcaJVACTaYyAyLbrp"></div>
                            </div>
                        </div> --}}

                        <div class="col-md-12">
                            <div class="checkbox">
                                <label><input type="checkbox" name="privacy" value="Si" required>Ho preso visione della
                                    <a href="https://mercurioservice.it/privacy-policy">privacy policy</a></label>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" name="maggiore" value="Si" required>Dichiaro di avere più di
                                    14 anni
                                </label>
                            </div>

                            <div class="form-group">
                                <input type="submit" style="width: 100%;" value="Invia"
                                    class="btn btn-block btn-primary" name="submit">
                            </div>
                        </div>

                    </form>
  • 控制器:
public function sendcandidatura(Request $request){
        
        if($request->input('privacy') == "Si" && $request->input('maggiore') == "Si" ){
            $oggetto_user = "Nuova candidatura";
            
            try{
                
                Mail::to($request->input('email'))->send(new ContactMail($request->input(), "email.mailuser", $oggetto_user ))->attach($request->file('cv'));

                return redirect()->back()->with('success', 'Candidatura inviata con successo');
            }catch(\Exception $e){
                //dd($e);
                return redirect()->back()->with('error', 'Errore nell\'invio della candidatura, riprovare più tardi!');
            }
            
        }else{
            return redirect()->back()->with('error', 'Devi prima accettare i termini di servizio per contattarci!');
        }
    }
  • ContactMail.php
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ContactMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */

    public $data = [];
    public $vista;
    public $oggetto;

    public function __construct($dati, $view, $subject)
    {
        $this->data = $dati;
        $this->vista = $view;
        $this->oggetto = $subject;
        //dd($this->oggetto);
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject($this->oggetto)->view($this->vista);
    }
}
ccrfmcuu

ccrfmcuu1#

在函数sendcandidatura中上传文件cv

$cv = $request->file('cv');

$request->validate([
'cv' => 'required'
]);

if ($cv->getError() == 1) {
    $max_size = $cv->getMaxFileSize() / 1024 / 1024; // Get size in Mb
    $error = 'The document size must be less than ' . $max_size . 'Mb.';
    return redirect()->back()->with('flash_danger', $error);
}

$attachment = [
'document' => $cv
];

Mail::to($request->input('email'))->send(new ContactMail($request->input(), "email.mailuser", $oggetto_user, $attachment));

您的Mailable类将有4个参数,如下所示:

public $data = [];
public $vista;
public $oggetto;
public $attachment;

public function __construct($dati, $view, $subject, $attachment)
{
    $this->data = $dati;
    $this->vista = $view;
    $this->oggetto = $subject;
    $this->attachment = $attachment;
    //dd($this->oggetto);
}

你的build()函数看起来像这样:

return $this->view($this->vista)
    ->subject($this->oggetto)
    ->attach($this->data['document']->getRealPath(),
    [
        'as' => $this->data['document']->getClientOriginalName(),
        'mime' => $this->data['document']->getClientMimeType(),
]);

相关问题