Laravel Türkiye Discord Kanalı Forumda kod paylaşılırken dikkat edilmesi gerekenler!Birlikte proje geliştirmek ister misiniz?

{
"isSuccess": false,
"message": "Teklif maili gönderilemedi Method Illuminate\\Mail\\Mailer::build does not exist.",
"data": null,
"statusCode": 500
}

böyle bir hata alıyorum hocam. laravel 10 kullanıyorum. ayrıca Job dosyasını iptal etmek istiyorum. Yada etmek mantıklı mı. zaten mail dosyam var bir de job dosyası eklemeyeyim diye düşündüm. direkt mail dosyasıyla işlem yapmak istiyorum.
`

      public function sendOfferMail(int $offer_id, string $customer_email): ServiceResponse
        {

         $company_id = auth()->user()->selected_company_id;
          $offer = Offer::where('company_id', $company_id)->find($offer_id);
         if (!$offer) {
            return new ServiceResponse(false, 'Teklif bulunamadı', null, 404);
        }

         $mailSetting = MailSetting::where('company_id', $company_id)->first();
       if (!$mailSetting) {
             return new ServiceResponse(false, 'Mail ayarları bulunamadı', null, 404);
         }

    if (empty($mailSetting->mail_driver) ||
        empty($mailSetting->mail_host) ||
        empty($mailSetting->mail_port) ||
        empty($mailSetting->mail_username) ||
        empty($mailSetting->mail_password) ||
        empty($mailSetting->mail_encryption)) {
        return new ServiceResponse(false, 'Mail ayarları alanları boş bırakılamaz', null, 404);
    }
    try {
 
        Mail::build([
            'transport' => $mailSetting->mail_driver,
            'host' => $mailSetting->mail_host,
            'port' => $mailSetting->mail_port,
            'encryption' => $mailSetting->mail_encryption,
            'username' => $mailSetting->mail_username,
            'password' => Crypt::decryptString($mailSetting->mail_password),
        ])
            ->to($customer_email)
            ->queue(new OfferMail($offer));

        return new ServiceResponse(true, 'Teklif maili gönderildi', null, 200);
    } catch (\Exception $e) {
        \Log::error('Mail Gönderim Hatası: ' . $e->getMessage());
        \Log::error('Mail Ayarları: ' . json_encode([
                'host' => $mailSetting->mail_host,
                'port' => $mailSetting->mail_port,
                'username' => $mailSetting->mail_username,
                'encryption' => $mailSetting->mail_encryption
            ]));
        return new ServiceResponse(false, 'Teklif maili gönderilemedi '. $e->getMessage(), null, 500);
    }
}`

bu şekilde yaptım ama sürümden kaynaklı herhalde hata alıyorum.
laravel sürümüm; "laravel/framework": "^ 10.0",
OfferMail.php dosyam;
`

class OfferMail extends Mailable  {

   use Queueable, SerializesModels;

  public $offer;

/**
 * Create a new message instance.
 */
public function __construct($offer)
{
    $this->offer = $offer;
}

/**
 * Get the message envelope.
 */
public function envelope(): Envelope
{
    return new Envelope(
        subject: 'Yeni Teklif'
    );
}

/**
 * Get the message content definition.
 */
public function content(): Content
{
    return new Content(
        view: 'mail.offerMail',
        with: [
            'offer' => $this->offer
        ]
    );
}

/**
 * Get the attachments for the message.
 *
 * @return array<int, \Illuminate\Mail\Mailables\Attachment>
 */
public function attachments(): array
{
    return [];
}

}
`

offertMail.blade.php
<!doctype html>
<html>
<body>
<h1>Teklif Detayları</h1>
<p>Teklif UUID: {{ $offer->uuid }}</p>
<!-- Diğer teklif bilgilerini buraya ekleyebilirsiniz -->
</body>
</html>

`

             config(['mailers.smtp' => [        
               'transport' => $mailSetting->mail_driver,
                'host' => $mailSetting->mail_host,
                'port' => $mailSetting->mail_port,
                'encryption' => $mailSetting->mail_encryption,
                'username' => $mailSetting->mail_username,
                'password' => Crypt::decryptString($mailSetting->mail_password),
            ]

        ]);

        Mail::mailer('smtp')
            ->to($customer_email)
            ->queue(new OfferMail($offer));`

build yerine bu şeklilde yaptım fakat yine mail gönderimi olmadı.

`
2025-03-05 06:29:58 App\Mail\OfferMail ................................................................................................................................................................................. RUNNING
2025-03-05 06:29:58 App\Mail\OfferMail ............................................................................................................................................................................ 26.20ms FAIL
2025-03-05 06:30:16 App\Mail\OfferMail ................................................................................................................................................................................. RUNNING
2025-03-05 06:30:19 App\Mail\OfferMail ................................................................................................................................................................................. 2s FAIL

`

`


             config([
                'mail.mailers.custom' => [

                'transport' => $mailSetting->mail_driver,
                'host' => $mailSetting->mail_host,
                'port' => $mailSetting->mail_port,
                'encryption' => $mailSetting->mail_encryption,
                'username' => $mailSetting->mail_username,
                'password' => Crypt::decryptString($mailSetting->mail_password),
            ],
            'mail.default' => 'custom', // Varsayılan mailer olarak custom'ı belirle
        ]);

        Mail::mailer('custom')
            ->to($customer_email)
            ->queue(new OfferMail($offer));`

bu şekilde de direkt FAIL e düşüyor.

mgsmus
bu şekilde yaptım da hocam hala istediğim sonuca ulaşamadım

    Aydnn queue değil send kullanacaksınız, ->send(new OfferMail($offer));. Fail oluyorsa hata veriyordur, loglara bakmanız lazım.

      mgsmus hocam send dediğimde veritabanına atmıyor süre olarak biraz bekletiyor. bu şekilde kuyruğa eklemiyor sanırım.
      send yaptığımda da mail gelmiyor.

        config/mail.php dosyam nasıl olmalı burada bir değişiklik yapacak mıyım.
        `


         <?php
         return [
        
        /*
        |--------------------------------------------------------------------------
        | Default Mailer
        |--------------------------------------------------------------------------
        |
        | This option controls the default mailer that is used to send any email
        | messages sent by your application. Alternative mailers may be setup
        | and used as needed; however, this mailer will be used by default.
        |
        */
        
        'default' => env('MAIL_MAILER', 'smtp'),
        
        /*
        |--------------------------------------------------------------------------
        | Mailer Configurations
        |--------------------------------------------------------------------------
        |
        | Here you may configure all of the mailers used by your application plus
        | their respective settings. Several examples have been configured for
        | you and you are free to add your own as your application requires.
        |
        | Laravel supports a variety of mail "transport" drivers to be used while
        | sending an e-mail. You will specify which one you are using for your
        | mailers below. You are free to add additional mailers as required.
        |
        | Supported: "smtp", "sendmail", "mailgun", "ses",
        |            "postmark", "log", "array", "failover"
        |
        */
        
        'mailers' => [
            'smtp' => [
                'transport' => 'smtp',
                'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
                'port' => env('MAIL_PORT', 587),
                'encryption' => env('MAIL_ENCRYPTION', 'tls'),
                'username' => env('MAIL_USERNAME'),
                'password' => env('MAIL_PASSWORD'),
                'timeout' => null,
                'local_domain' => env('MAIL_EHLO_DOMAIN'),
            ],
        
            'ses' => [
                'transport' => 'ses',
            ],
        
            'mailgun' => [
                'transport' => 'mailgun',
                // 'client' => [
                //     'timeout' => 5,
                // ],
            ],
        
            'postmark' => [
                'transport' => 'postmark',
                // 'client' => [
                //     'timeout' => 5,
                // ],
            ],
        
            'sendmail' => [
                'transport' => 'sendmail',
                'path' => env('MAIL_SENDMAIL_PATH', '/usr/sbin/sendmail -bs -i'),
            ],
        
            'log' => [
                'transport' => 'log',
                'channel' => env('MAIL_LOG_CHANNEL'),
            ],
        
            'array' => [
                'transport' => 'array',
            ],
        
            'failover' => [
                'transport' => 'failover',
                'mailers' => [
                    'smtp',
                    'log',
                ],
            ],
        
        ],
        
        /*
        |--------------------------------------------------------------------------
        | Global "From" Address
        |--------------------------------------------------------------------------
        |
        | You may wish for all e-mails sent by your application to be sent from
        | the same address. Here, you may specify a name and address that is
        | used globally for all e-mails that are sent by your application.
        |
        */
        
        'from' => [
            'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
            'name' => env('MAIL_FROM_NAME', 'Example'),
        ],
        
        /*
        |--------------------------------------------------------------------------
        | Markdown Mail Settings
        |--------------------------------------------------------------------------
        |
        | If you are using Markdown based email rendering, you may configure your
        | theme and component paths here, allowing you to customize the design
        | of the emails. Or, you may simply stick with the Laravel defaults!
        |
        */
        
        'markdown' => [
            'theme' => 'default',
        
            'paths' => [
                resource_path('views/vendor/mail'),
            ],
        ],
        
        ];`

        log dosyası da;
        [2025-03-05 07:38:56] local.ERROR: Mailer [mail] is not defined. {"exception":"[object] (InvalidArgumentException(code: 0): Mailer [mail] is not defined. at C:\xampp\htdocs\ays_crm\vendor\laravel\framework\src\Illuminate\Mail\MailManager.php:115)
        [stacktrace]
        #0 C:\xampp\htdocs\ays_crm\vendor\laravel\framework\src\Illuminate\Mail\MailManager.php(99): Illuminate\Mail\MailManager->resolve('mail')
        #1 C:\xampp\htdocs\ays_crm\vendor\laravel\framework\src\Illuminate\Mail\MailManager.php(77): Illuminate\Mail\MailManager->get('mail')
        #2 C:\xampp\htdocs\ays_crm\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php(202): Illuminate\Mail\MailManager->mailer('mail')

        mail bulunamadı diyor ama bir çok yöntem denedim. maili kaldırdım bu sefer de şu hata veriyordu;
        [2025-03-05 06:30:19] local.ERROR: Connection could not be established with host "mailpit:1025": stream_socket_client(): php_network_getaddresses: getaddrinfo for mailpit failed: Bilinen b�yle bir ana bilgisayar yok. {"exception":"[object] (Symfony\Component\Mailer\Exception\TransportException(code: 0): Connection could not be established with host \"mailpit:1025\": stream_socket_client(): php_network_getaddresses: getaddrinfo for mailpit failed: Bilinen b�yle bir ana bilgisayar yok. at C:\xampp\htdocs\ays_crm\vendor\symfony\mailer\Transport\Smtp\Stream\SocketStream.php:154)
        [stacktrace]
        #0 [internal function]: Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream->Symfony\Component\Mailer\Transport\Smtp\Stream\{closure}(2, 'stream_socket_c...', 'C:\\xampp\\htdocs...', 157)
        #1 C:\xampp\htdocs\ays_crm\vendor\symfony\mailer\Transport\Smtp\Stream\SocketStream.php(157): stream_socket_client('mailpit:1025', 0, '', 60.0, 4, Resource id #1110)
        #2 C:\xampp\htdocs\ays_crm\vendor\symfony\mailer\Transport\Smtp\SmtpTransport.php(275): Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream->initialize()

        en son bu şekilde yaptım hocam yine çalışmadı;
        config([
        'mail.mailers.smtp.host' => $mailSetting->mail_host,
        'mail.mailers.smtp.port' => $mailSetting->mail_port,
        'mail.mailers.smtp.username' => $mailSetting->mail_username,
        'mail.mailers.smtp.password' => Crypt::decryptString($mailSetting->mail_password),
        'mail.mailers.smtp.encryption' => $mailSetting->mail_encryption,
        'mail.from.address' => "aydinyagiz002@gmail.com",
        'mail.from.name' => "Teklif Sistemi"
        ]);
        Mail::mailer('smtp')
        ->to($customer_email)
        ->send(new OfferMail($offer));

        Aydnn Önce config olayını ve kuyruk işleyiş mantığını anlamaya çalışalım. Siz bir işi kuyrukta yapmaya çalıştığınızda iş serialize edilip json payload halinde kuyruğa alınır, sonra unserialize edilip tekrar çalıştırılabilir koda dönüşür ve framework tekrar boot olup o kodu çalıştırır. Bu tekrar boot sırasında doğal olarak kayıtlı olan config değerleri kullanılıyor. Siz ayarları dinamik olarak değiştirip ardından kuyruğa bir iş gönderirseniz o kuyruk içinde geçerli olmayacaktır.

        O yüzden ilk yaptığınız gibi OfferMailJob gibi bir Job yazıp içinde config değiştirip ardından Mail::queue() yerine Mail::send() yapacaksınız. OfferMailJob mailin yeni config ile kuyrukta gönderilmesini sağlayacak, Mail::send() de yeni mail ayarların geçerli olmasını.

        bilgi için teşekkür ederim hocam. söylediğiniz yoldan gittim. Job dosyasını dahil ettim. bu sefer de sadece job dosyası çalışıyor. hata vermiyor ama mail de gelmiyor. sanırım oluşturduğum OfferMail.php dosyası çalışmıyor. Hatamın nerede olduğunu göremiyorum.
        kodlarım;
        OfferService.php
        $data = [
        'customer_email' => $customer_email,
        'offer' => $offer
        ];
        dispatch(new OfferMailJob($data, $mailSetting));

        OfferMailJob.php dosyam;
        `


          class OfferMailJob implements ShouldQueue
         
          {
             use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
        
        /**
         * Create a new job instance.
         */
        public $data;
        public $mailSetting;
        public function __construct($data, $mailSetting)
        {
            $this->data = $data;
            $this->mailSetting = $mailSetting;
        }
        
        /**
         * Execute the job.
         */
        public function handle(): void
        {
            config([
                'mail.mailers.smtp.host' => $this->mailSetting->mail_host,
                'mail.mailers.smtp.port' => $this->mailSetting->mail_port,
                'mail.mailers.smtp.username' => $this->mailSetting->mail_username,
                'mail.mailers.smtp.password' => Crypt::decryptString($this->mailSetting->mail_password),
                'mail.mailers.smtp.encryption' => $this->mailSetting->mail_encryption,
                'mail.from.address' => "aydinyagiz002@gmail.com",
                'mail.from.name' => "Teklif Sistemi"
            ]);
        
            Mail::mailer('smtp')
                //->to($this->data['customer_email'])
                ->to('aydinyagiz002@gmail.com')
                ->send(new OfferMail($this->data['offer']));
        }
        }

        `

        OfferMail.php dosyam;

        `


        class OfferMail extends Mailable
           
        {
        use Queueable, SerializesModels;
        
        public $offer;
        
        /**
         * Create a new message instance.
         */
        public function __construct($offer)
        {
            $this->offer = $offer;
        }
        
        /**
         * Get the message envelope.
         */
        public function envelope(): Envelope
        {
            return new Envelope(
                subject: 'Yeni Teklif'
            );
        }
        
        /**
         * Get the message content definition.
         */
        public function content(): Content
        {
            return new Content(
                view: 'mail.offerMail',
                with: [
                    'offer' => $this->offer
                ]
            );
        }
        
        /**
         * Get the attachments for the message.
         *
         * @return array<int, \Illuminate\Mail\Mailables\Attachment>
         */
        public function attachments(): array
        {
            return [];
        }
        }

        offerMail.blade.php
        <!doctype html>
        <html>
        <body>
        <h1>Teklif Detayları</h1>
        <p>Teklif UUID: {{ $offer->uuid }}</p>
        <!-- Diğer teklif bilgilerini buraya ekleyebilirsiniz -->
        </body>
        </html>

        `

        çıktı;
        PS C:\xampp\htdocs\ays_crm> php artisan queue:work

        INFO Processing jobs from the [default] queue.

        2025-03-05 08:53:52 App\Jobs\OfferMailJob .............................................................................................................................................................................. RUNNING
        2025-03-05 08:53:53 App\Jobs\OfferMailJob .............................................................................................................................................................................. 1s DONE
        2025-03-05 08:54:50 App\Jobs\OfferMailJob .............................................................................................................................................................................. RUNNING
        2025-03-05 08:54:51 App\Jobs\OfferMailJob ........................................................................................................................................................................ 323.23ms DONE
        2025-03-05 08:55:30 App\Jobs\OfferMailJob .............................................................................................................................................................................. RUNNING
        2025-03-05 08:55:30 App\Jobs\OfferMailJob ........................................................................................................................................................................ 329.70ms DONE

          Aydnn Kodu değiştirdikten sonra kuyruğu resetlemeyi unutmayın. mailer('smtp') kaldırıp deneyin, belki config değerlerini eziyordur. Mail ayarlarını da kontrol edin. Test için https://mailtrap.io/ ya da https://debugmail.io/ de kullanabilirsiniz. Bunlarda mail alınıyor ise kullandığınız ayarlarda sorun vardır.

            mgsmus hocam kuyruğu resetledim. mailer('smtp') kaldırdım. Mail ayarları da kendi web sitemde kullandığım mail ayarları (host, port vs) bunlar kendi web sitemde çalışıyor.
            `config([
            'mail.mailers.smtp.host' => $this->mailSetting->mail_host,
            'mail.mailers.smtp.port' => $this->mailSetting->mail_port,
            'mail.mailers.smtp.username' => $this->mailSetting->mail_username,
            'mail.mailers.smtp.password' => Crypt::decryptString($this->mailSetting->mail_password),
            'mail.mailers.smtp.encryption' => $this->mailSetting->mail_encryption,
            'mail.from.address' => "aydinyagiz002@gmail.com",
            'mail.from.name' => "Teklif Sistemi"
            ]);

            // Mail::mailer('smtp')
            // ->to($this->data['customer_email'])
            // ->send(new OfferMail($this->data['offer']));
            Mail::to($this->data['customer_email'])
            ->send(new OfferMail($this->data['offer']));`
            bu kodu OfferMailJob.php çalıştırdığımda ise sadece job dosyası açlışıyor. veritabanına ekliyor ve oradan uçuruyor. ama mail gelmiyor.
            PS C:\xampp\htdocs\ays_crm> php artisan queue:restart

            INFO Broadcasting queue restart signal.

            PS C:\xampp\htdocs\ays_crm> php artisan queue:work

            INFO Processing jobs from the [default] queue.

            2025-03-05 10:28:43 App\Jobs\OfferMailJob .............................................................................................................................................................................. RUNNING
            2025-03-05 10:28:44 App\Jobs\OfferMailJob .............................................................................................................................................................................. 1s DONE
            2025-03-05 10:29:05 App\Jobs\OfferMailJob .............................................................................................................................................................................. RUNNING
            2025-03-05 10:29:05 App\Jobs\OfferMailJob ........................................................................................................................................................................ 368.36ms DONE
            2025-03-05 10:29:44 App\Jobs\OfferMailJob .............................................................................................................................................................................. RUNNING
            2025-03-05 10:29:45 App\Jobs\OfferMailJob ........................................................................................................................................................................ 382.60ms DONE

              Aydnn config('mail.default'); yaptığınızda smtp aldığınızdan emin olun. Mail kutusunda spam klasörünü kontrol edin. Benim tavsiyem https://debugmail.io/ ya da https://mailtrap.io/ ücretsiz üye olup oradaki ayarlar ile deneme yapmanız. Eğer oraya mail gidiyorsa mail ayarları ile ilgili olabilir. Geliştirme ortamında https://laravel.com/docs/10.x/telescope kurarak mail ve diğer logları takip edin. Onun dışında benim önerebileceğim bir şey kalmadı.

                mgsmus hocam ilgileriniz için teşekkür ederim. telescope da kuruluydu orada da sorun gözükmüyordu ama sorunu hallettim. atayım böyle bir hata alan varsa buradan yapabilir.
                $data = [
                'customer_email' => $customer_email,
                'offer' => $offer
                ];
                dispatch(new OfferMailJob($data, $mailSetting, $customer_email));

                OfferMailJob.php dosyam;

                `


                class OfferMailJob implements ShouldQueue
                
                 {          
                 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
                
                /**
                 * Create a new job instance.
                 */
                public $data;
                public $mailSetting;
                
                public $customer_email;
                
                public function __construct($data, $mailSetting, $customer_email)
                {
                    $this->data = $data;
                    $this->mailSetting = $mailSetting;
                    $this->customer_email = $customer_email;
                }
                
                /**
                 * Execute the job.
                 */
                public function handle(): void
                {
                    Config::set('mail.mailers.smtp', [
                        'transport'  => 'smtp',
                        'host'       => $this->mailSetting->mail_host,
                        'port'       => intval($this->mailSetting->mail_port),
                        'encryption' => $this->mailSetting->mail_encryption,
                        'username'   => $this->mailSetting->mail_username,
                        'password'   => Crypt::decryptString($this->mailSetting->mail_password),
                        'timeout'    => null,
                    ]);
                
                    Config::set('mail.from', [
                        'address' => $this->mailSetting->mail_username,
                        'name'    => 'Yeni Teklif',
                    ]);
                
                
                
                    try {
                        Mail::to($this->data['customer_email'])
                            ->send(new OfferMail($this->data['offer'], $this->customer_email));
                
                        Log::info('Mail başarıyla gönderildi.');
                    } catch (\Exception $e) {
                        Log::error('Mail gönderme hatası: ' . $e->getMessage());
                    }
                
                }
                 }

                `
                OfferMail.php dosyam;

                `

                 class OfferMail extends Mailable
                 
                {
                      
                 use Queueable, SerializesModels;
                
                public $offer;
                public $customer_email;
                
                /**
                 * Create a new message instance.
                 */
                public function __construct($offer, $customer_email)
                {
                    $this->offer = $offer;
                    $this->customer_email = $customer_email;
                }
                
                /**
                 * Get the message envelope.
                 */
                public function envelope(): Envelope
                {
                    return new Envelope(
                        subject: 'Yeni Teklif'
                    );
                }
                
                /**
                 * Get the message content definition.
                 */
                public function content(): Content
                {
                    return new Content(
                        view: 'mail.offerMail',
                        with: [
                            'offer' => $this->offer,
                            'customer_email' => $this->customer_email
                        ]
                    );
                }
                
                /**
                 * Get the attachments for the message.
                 *
                 * @return array<int, \Illuminate\Mail\Mailables\Attachment>
                 */
                public function attachments(): array
                {
                    return [];
                }
                }

                `

                bu şekilde çalışıyor hocam.