send email but image not show (laravel)

Viewed 31

I send email using a Schedule but the image does not show

image not show

Controller

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Models\Message;
use App\Models\SecondMessage;
use App\Models\ThirdMessage;

class MessageController extends Controller
{
    public function message(){
        $msg = Message::where('user_id',auth::user()->id)->where('msg_type',1)->first();
        return view('admin.create_message',compact('msg'));
    }
    
    public function messageStore(Request $request){
        $message = Message::where('user_id',Auth::user()->id)->where('msg_type',1)->first();
        if(isset($message->id)){

            if($request->file){
                $file = time().'.'.$request->file->getClientOriginalExtension();
                $location = public_path('uploads/' .$file);
                $request->file->move($location, $file);
            }else{
                $location = $message->file;
            }

            $message->subject  = $request->subject;
            $message->msg_body = $request->msg_body;
            $message->file = $file??'';
            $message->save();
        }else{

            if($request->file){
                $file = time().'.'.$request->file->getClientOriginalExtension();
                $location = public_path('/' .$file);
                $request->file->move($location, $file);
            }else{
                $location = null;
            }

            $message           = new Message;
            $message->subject  = $request->subject;
            $message->msg_body = $request->msg_body;
            $message->user_id  = Auth::user()->id;
            $message->msg_type = 1;
            $message->file     = $location;
            $message->save();
        }
        return back()->with('success','First SMTP Message Added Successfully');
    }
    public function secondMessage(){
        $second_msg = SecondMessage::where('user_id',Auth::user()->id)->get();
   
        return view('admin.smtp.second_message',compact('second_msg'));
    }
    public function secondMessageCreate(){
        return view('admin.smtp.second_message_create');
    }

    public function secondMessageStore(Request $request){

        if($request->file){
            $file = time().'.'.$request->file->getClientOriginalExtension();
            $location = public_path('uploads/' .$file);
            $request->file->move($location, $file);
        }else{
            $file = null;
        }

     
        $message           = new SecondMessage;
        $message->subject  = $request->subject;
        $message->msg_body = $request->msg_body;
        $message->file_status = $request->file_status;
        $message->file = $file;
        $message->user_id  = auth::user()->id;
        
        if($message->save()){
            $check = SecondMessage::where('user_id',auth::user()->id)->count();
            $message->serial   = $check;
        }
        
        $message->save();
        return back()->with('success','Second SMTP Message Added Successfully');
    }
    public function secondMessageEdit($id){
        $second_msg = SecondMessage::where('id',$id)->where('user_id',Auth::user()->id)->first();
        return view('admin.smtp.second_message_edit',compact('second_msg'));
    }
    public function secondMessageUpdate(Request $request,$id){
        $message           = SecondMessage::find($id);
        if($request->file){
            $file = time().'.'.$request->file->getClientOriginalExtension();
            $location = public_path('uploads/' .$file);
            $request->file->move($location, $file);
        }else{
            $file = $message->file;
        }
        $message->subject  = $request->subject;
        $message->msg_body = $request->msg_body;
        $message->file_status = $request->file_status;
        $message->file = $file;
        $message->save();
        return back()->with('success','Second SMTP Message Updated Successfully');
    }
    public function thirdMessage(){
        $third_msg = ThirdMessage::where('user_id',Auth::user()->id)->get();
        return view('admin.smtp.third_message',compact('third_msg'));
    }
    public function thirdMessageCreate(){
        return view('admin.smtp.third_message_create');
    }
    public function thirdMessageStore(Request $request){

            if($request->file){
                $file = time().'.'.$request->file->getClientOriginalExtension();
                $location = public_path('uploads/' .$file);
                $request->file->move($location, $file);
            }else{
                $file = null;
            }


            $message           = new ThirdMessage;
            $message->subject  = $request->subject;
            $message->msg_body = $request->msg_body;
            $message->user_id  = auth::user()->id;
            $message->file = $file;
            $message->file_status = $request->file_status;

            if($message->save()){
                $check = ThirdMessage::where('user_id',auth::user()->id)->count();
                $message->serial = $check;
            }
            $message->save();

        return back()->with('success','Third SMTP Message Added Successfully');
    }
    public function thirdMessageEdit($id){
        $third_msg = ThirdMessage::where('id',$id)->where('user_id',auth::user()->id)->first();
        return view('admin.smtp.third_message_edit',compact('third_msg'));
    }
    public function thirdMessageUpdate(Request $request,$id){
        $third_msg = ThirdMessage::where('id',$id)->where('user_id',auth::user()->id)->first();
        if($request->file){
            $file = time().'.'.$request->file->getClientOriginalExtension();
            $location = public_path('uploads/' .$file);
            $request->file->move($location, $file);
        }else{
            $file = $third_msg->file;
        }
        $third_msg->subject  = $request->subject;
        $third_msg->msg_body = $request->msg_body;
        $third_msg->file = $file;
        $third_msg->file_status = $request->file_status;
        $third_msg->save();

    return back()->with('success','Third SMTP Message Updated Successfully');

    }
}

emailbody.blade

<p>{{$body}}</p>
@if(isset($msg->file_status))
    @if ($msg->file_status == 'show')
  <img src="{{asset('uploads/'.$msg->file.'/'.user()->id->$msg->file)}}">
   
    @endif
@endif

MailSchedule

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\SecondSmtp;
use App\Models\Smtp;
use App\Models\Email;
use App\Models\Message;
use App\Models\Hourlymail;
use App\Models\SecondMessage;
use App\Models\ThirdMessage;
use Carbon\Carbon;
use App\Models\Pop;
use App\Models\User;
use PHPMailer\PHPMailer\Exception;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;

class MailSchedule extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mail:schedule';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->smtpFisrtMail();
        $this->smtpSecondMail();
        $this->everyHour();
        $this->getPop();
    
    }

    private $value = 0;
    
    public function getPop(){

     $pop = Pop::get()->toArray();

        foreach($pop as $key=>$imap){
         
            $username       = $imap['emails'];
            $password       = $imap['email_pass'];
            $imapserver     = $imap['host_name'];
            $pop_port       = $imap['pop_port'];

            if(strpos($username ,'gmail.com')) {
              $hostname =  "{imap.gmail.com:$pop_port/imap/ssl/novalidate-cert}INBOX";

            }elseif(strpos($username ,'yahoo.com')){
              $hostname = "{imap.mail.yahoo.com:$pop_port/imap/ssl}";
            }elseif(strpos($username ,'outlook.com')){
                $hostname="{imap-mail.outlook.com:$pop_port/imap/ssl}INBOX";
            }else{
                $hostname = "{".$imapserver.":$pop_port/imap/ssl/novalidate-cert}";
            }

            $inbox = imap_open($hostname,$username,$password);

            $emails = imap_search($inbox,'ALL');
 
                //   dd($emails);
                
                if($emails) {

                    rsort($emails);
                    foreach($emails as $email_number) {
                        
                        // dd($imap['id']);
                        $getheader   = imap_header($inbox, $email_number);
                     
                        $overview    = imap_fetch_overview($inbox,$email_number,0);

                        $subject        = $overview[0]->subject;
                        $str_msg_id     = $overview[0]->message_id;
                        
                        $reply_to       = str_contains($subject,'Re:');
                
                        $fromname       = $overview[0]->from;
                        $namecut        = strpos($fromname, "<");
                        $newfromname    = substr($fromname,0,$namecut);
                        $date           = $overview[0]->date;
                        $fromemail      = $getheader->from[0]->mailbox . "@" . $getheader->from[0]->host;


                        $prefix         = $getheader->from[0]->mailbox;
                        $suffix         = $getheader->from[0]->host;
                        $blacklist      = 5;
                        $imap_count     = Email::where('email',$fromemail)->where('reply_to',1)->count();
                        $serial         = $imap_count+1;
                        $time           = time();

                        $leaduniqcheck = Email::where('email',$fromemail)->where('status',0)->count();
                        
                        $check_msg_id = Email::where('email',$fromemail)->first();
                          
                        if(isset($check_msg_id->id)){
                            $message_id = $check_msg_id->message_id;
                        }else{
                             $message_id = str_replace(array('<', '>'), '', $str_msg_id);
                        }

                        // if($leaduniqcheck == 0 && $serial < 7 ){
                        
                            Email::create([
                                'email'   => $fromemail,
                                'toemail' => $username,
                                'name'    => $newfromname,
                                'subject' => $subject,
                                'reply_to' => $reply_to==true ? 1 : 0 ,
                                'message' => '',
                                'message_id' => $message_id,
                                'msgdate' => $date,
                                'time'    => $time,
                                'serial'  => $serial,
                                'sent_status'=> 0,
                                'status'  =>  0,
                                'pop_id'  => $imap['id'],
                                'user_id' => $imap['user_id']
                            ]);
                        
                        imap_delete($inbox, $email_number);
                    }
                    imap_expunge($inbox);
                    imap_close($inbox);
                }
            }
    
        }
    
    
     public function smtpFisrtMail(){
        require base_path("vendor/autoload.php");

        
        $pop_emails  = Email::where('reply_to',0)
        ->where('status',0)
        ->where('sent_status',0)
        ->get();
    

        foreach($pop_emails as $key=>$pop){
            $msg        = Message::where('user_id',$pop->user_id)->where('msg_type',1)->first();
            $smtps      = Smtp::where('status',1)->where('limit','>',0)->get()->toArray();
            $cc         = (int)count($smtps);


            if(empty($cc)){
                return back()->with('danger','Your Smtp Limit is finished');
            }
            if($cc > $this->value){
                $stp = $smtps[$this->value];
                $this->value = $this->value+1;
            }else{
                $this->value = 0;
                $stp = $smtps[$this->value];
            }
            $smtp = Smtp::find($stp['id']);


            try{
                //update funciton
                $config = array(
                    'driver'     => 'smtp',
                    'host'       => $smtp->host_name,
                    'port'       => $smtp->imap_port,
                    'username'   => $smtp->emails,
                    'password'   => $smtp->email_pass,
                    'encryption' => 'tls',
                    'from'       => array('address' => $smtp->emails, 'name' => 'Rana'),
                    // 'reply_to' => [
                    //     'address' => $smtp->reply_to,
                    //     'name' => "Rana",
                    // ],
                   
                    'sendmail'   => '/usr/sbin/sendmail -bs',
                    'pretend'    => false,
                );
                Config::set('mail', $config);

                $email       = $pop->email;
                $from        = $smtp->emails;
                $sender_name = $smtp->from_name;
                $subject     = $pop->subject;
                $body        = $msg->msg_body??'';
                $message_id  = $pop->message_id;
                
                if(isset($body)){
                   $mail = Mail::send('emailbody',
                        [
                            'email'       => $email,
                            'from'        => $from,
                            'sender_name' => $sender_name,
                            'subject'     => $subject,
                            'body'        => $body,
                            'msg'         => $msg,
                            'message_id'  =>$message_id,
                            'smtp'        => $smtp,
                        ], function ($m) use ($email, $from, $sender_name, $subject, $smtp,$message_id) {
                            
                            $m->setId($message_id);
                         
                            $m->from($from, $sender_name);
                   
                            $m->to($email)->subject($subject);
                            $m->replyTo($smtp->reply_to, $sender_name);
                        });
                    }
     
                Smtp::where('id',$smtp['id'])->where('limit','>',0)->decrement('limit','1');
                
                Email::where('id',$pop->id)->update(['sent_status'=>1,'status'=>0]);

            }catch (Exception $e) {
                    //
            }

        }

    }
    
     public function smtpSecondMail(){
         
         $second_smtps = SecondSmtp::all();
         
         
        require base_path("vendor/autoload.php");
        $pop_emails  = Email::where('reply_to',1)->where('sent_status',0)->get();


        foreach($pop_emails as $key=>$pop){

            $smtp      = SecondSmtp::where('status',1)->where('limit','>',0)->first();

            $msg       = SecondMessage::where('user_id',$pop->user_id)->where('serial',$pop->serial)->first();
 
            try{
                //update funciton
                $config = array(
                    'driver'     => 'smtp',
                    'host'       => $smtp->host_name,
                    'port'       => $smtp->imap_port,
                    'username'   => $smtp->emails,
                    'password'   => $smtp->email_pass,
                    'encryption' => 'tls',
                    'from'       => array('address' => $smtp->emails, 'name' => 'Rana'),
                    // 'reply_to' => [
                    //     'address' => $smtp->reply_to,
                    //     'name' => "Rana",
                    // ],
                    'sendmail'   => '/usr/sbin/sendmail -bs',
                    'pretend'    => false,
                );
            
                Config::set('mail', $config);

                $email = $pop->email;
                $from = $pop->toemail;
         
                 $sender_name = $smtp->from_name;
                $subject = $pop->subject;
                $body = $msg->msg_body;
                $message_id  = $pop->message_id;
               
               
 
                    $m_send = Mail::send('emailbody',
                    [
                        'email' => $email,
                        'from' => $from,
                        'sender_name' => $sender_name,
                        'subject' => $subject,
                        'body' => $body,
                        'msg' => $msg??'',
                        'smtp' => $smtp,
                        'message_id'  =>$message_id,
                    ], function ($m) use ($email, $from, $sender_name, $subject, $smtp,$message_id) {
              
                        $m->from($from, $sender_name);
                        $m->setId($message_id);
                        $m->to($email)->subject($subject);
                        $m->replyTo($from, $sender_name);
                    });
                SecondSmtp::where('id',$smtp->id)->where('limit','>',0)->decrement('limit','1');
                Email::where('id',$pop->id)->update(['sent_status'=>1]);
            }catch (Exception $e) {}
        }
      
    }
    
      public function everyHour(){
        $datetime = \Carbon\Carbon::now()->subHours(1);

        $pop_emails  = Email::where('reply_to',1)->groupBy('email')
        ->get();

        foreach($pop_emails as $key=>$pop){
            $email = Email::where('email', $pop->email)->first();
        

                if(isset($email) && ($email->updated_at <= $datetime)){
                    $email->updated_at = Carbon::now();
                    $email->save();
                    $hourlymail = Hourlymail::where('email', $pop->email)->first();
                    
                if(!isset($hourlymail)){
                    $nhourlymail = new Hourlymail();
                    $nhourlymail->email = $pop->email;
                    $nhourlymail->send = '0';
                    $nhourlymail->save();
                }else{
                    $nhourlymail = Hourlymail::find($hourlymail->id);
                    $nhourlymail->send = $nhourlymail->send + 1;
                    $nhourlymail->save();
                }

                $third_msgs = ThirdMessage::where('user_id',$pop->user_id)->get()->toArray();

                if(count($third_msgs) > $nhourlymail->send){
                    $msg = ThirdMessage::find($third_msgs[$nhourlymail->send]['id']);

                    $smtp = SecondSmtp::where('status',1)->where('limit','>',0)->first();
                
                    //update funciton
                    $config = array(
                        'driver'     => 'smtp',
                        'host'       => $smtp->host_name,
                        'port'       => $smtp->imap_port,
                        'username'   => $smtp->emails,
                        'password'   => $smtp->email_pass,
                        'encryption' => 'tls',
                        'from'       => array('address' => $smtp->emails, 'name' => 'Rana'),
                        // 'reply_to' => [
                        //     'address' => $smtp->reply_to,
                        //     'name' => "Rana",
                        // ],
                        'sendmail'   => '/usr/sbin/sendmail -bs',
                        'pretend'    => false,
                    );
                    Config::set('mail', $config);

                    $email = $pop->email;
                    $from = $pop->toemail;
                    $sender_name = $smtp->from_name;
                    $subject = $pop->subject;
                    $body = $msg->msg_body;
                    $message_id  = $pop->message_id;
                    
                    if(isset($body)){
                        Mail::send('emailbody',
                        [
                            'email' => $email,
                            'from' => $from,
                            'sender_name' => $sender_name,
                            'subject' => $subject,
                            'body' => $body,
                            'msg' => $msg,
                            'smtp' => $smtp,
                            'message_id'  =>$message_id,
                        ], function ($m) use ($email, $from, $sender_name, $subject, $smtp,$message_id) {
                            $m->from($from, $sender_name);
                            $m->setId($message_id);
                            $m->to($email)->subject($subject);
                            $m->replyTo($from, $sender_name);
                            
                        });
                    }
                        
                    // }
  
            }

        }
      }
      }
      
       public function pop(){
        $pops = Pop::where('user_id',Auth::user()->id)->get();
        return view('admin.pop',compact('pops'));
    }
    public function popCreate(){
           return view('admin.create_pop');
    }
     public function popStore(Request $request){
        $count_pop = Pop::where('emails',$request->emails)->count();
        
        if($count_pop>0){
            return back()->with('error','Pop Already Exists');
        }
        
        $users = User::get()->pluck('id')->toArray();
     
        $user_id = implode(',',$users);
        
      
        $pop = new Pop;
        $pop->from_name  = $request->from_name;
        $pop->emails     = $request->emails;
        $pop->email_pass = $request->email_pass;
        $pop->host_name  = $request->host_name;
        $pop->pop_port   = $request->pop_port;
        $pop->pop_limit  = $request->pop_limit;
        $pop->user_id    = Auth::user()->id;
        
        if(Auth::user()->id==1){
            $pop->pop_user   = $user_id;
        }
        
        $pop->status     = 0;
        $pop->save();
        return redirect('pop')->with('success','Pop Added Successfully');
    }
    public function popDelete($id){
        $pop = Pop::where('user_id',Auth::user()->id)->find($id);
        $pop->delete();
        return back()->with('danger','Pop Deleted Successfully');
    }
}

If I send manually using send button the email goes with the image. If the email is sent via Schedule(cronjob) the image does not show. What is the problem?

My script host now live server cPanel.

1 Answers

Is the image you are trying to view outside your code? Gmail? Mailtrap? Might be because you are using {{asset()}}. If you inspect the code, the display value of {{asset('image-path')}} is /image-path. Try to change your src to URL::asset('image-path') Or other codes that will fetch the full path of the image.

Related