MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to see number of emails sent using smtp in laravel

Laravel does not count or log SMTP emails by default, so a grep will only return a real number once your application writes one log line for each message sent. This guide shows how to log every sent email with Laravel's MessageSent event, count those lines with grep, track failed sends separately and check the figure against your SMTP provider. It also explains why the commands above can give misleading numbers.

To see the number of emails sent using SMTP in Laravel, you can look for log entries in your Laravel application’s logs that indicate email messages have been sent.

For example, you could use the following command to search for log entries containing the string “Sent Email” in the file “laravel.log”:

$ cd /opt/lampp/htdocs
$ grep -c "Sent Email" laravel.log
$ grep -r -i --include="*.log" -o "smtp" . | wc -l
$ grep -r --include="*.log" -o "email-smtp.ap-south-1.amazonaws.com" . | wc -l

Explanation:

  • grep searches for the specified string in the file
  • -c flag only prints the count of matching lines, rather than the lines themselves

This command will give you a count of the number of log entries containing the string “Sent Email”, which would indicate that an email was sent.


What the commands above actually count

Each command in the example above searches for a string. None of them counts emails unless your application writes that string once for each email. With MAIL_MAILER=smtp, Laravel writes nothing to the log when a message is sent successfully. In practice, the SMTP host name usually appears in the log only when something goes wrong.

CommandWhat it really countsProblem
grep -c "Sent Email" laravel.logLines containing the text Sent EmailLaravel never writes this text itself, so the result is 0 unless your code logs it
grep -r -i -o "smtp" . | wc -lEvery time smtp appears in any .log file below the current directoryAlso matches config dumps, error messages and stack traces, and searches every project under htdocs
grep -r -o "email-smtp.ap-south-1.amazonaws.com" . | wc -lEvery time the SES host name appearsThe host name is mostly logged in connection and authentication errors, so this tends to count failures, not sends

Note: -o combined with wc -l counts matches, not log entries. One failed send can produce a stack trace that mentions the host several times, so one failure can add several to the count.

Where Laravel writes its logs

Application logs are written to storage/logs inside the project, not to the web root. With the default single channel the file is storage/logs/laravel.log. With the daily channel there is one file per day, such as laravel-2026-11-29.log. The LOG_CHANNEL (and LOG_STACK) values in .env decide which applies.

cd /opt/lampp/htdocs/your-project
ls -lh storage/logs/
grep -E '^(MAIL_|LOG_)' .envCode language: JavaScript (javascript)

Check that MAIL_MAILER is smtp in the environment you are measuring. Laravel versions before 7 use MAIL_DRIVER instead. If it is set to log, messages are written to the log file and never sent. If it is set to array, they are only held in memory.

Log every sent email with the MessageSent event

Laravel fires Illuminate\Mail\Events\MessageSent after the mailer hands a message to the transport. For SMTP, that means after the server accepts it. Queued mail fires the event in the queue worker when the message actually goes out, not when it is queued. A listener for this event gives you exactly one log line for each accepted message.

First, add a dedicated log channel to the channels array in config/logging.php. This keeps mail lines separate from application noise:

'mail' => [
    'driver' => 'daily',
    'path' => storage_path('logs/mail.log'),
    'level' => 'info',
    'days' => 90,
],Code language: PHP (php)

Then create the listener with php artisan make:listener LogSentMessage --event=MessageSent and replace its contents:

<?php

namespace App\Listeners;

use Illuminate\Mail\Events\MessageSent;
use Illuminate\Support\Facades\Log;

class LogSentMessage
{
    public function handle(MessageSent $event): void
    {
        $message = $event->message; // Symfony\Component\Mime\Email (Laravel 9+)

        Log::channel('mail')->info('Sent Email', [
            'to'         => collect($message->getTo())->map->getAddress()->implode(','),
            'subject'    => $message->getSubject(),
            'message_id' => $event->sent->getMessageId(),
        ]);
    }
}Code language: HTML, XML (xml)

In Laravel 11 and later, listeners in app/Listeners are discovered automatically from the type-hinted event. In Laravel 10 and earlier, register the listener in app/Providers/EventServiceProvider.php:

protected $listen = [
    \Illuminate\Mail\Events\MessageSent::class => [
        \App\Listeners\LogSentMessage::class,
    ],
];Code language: PHP (php)

The listener uses Symfony Mailer methods, which Laravel uses from version 9. Laravel 8 and earlier use SwiftMailer, so the listener needs SwiftMailer equivalents there. Laravel 8 does fire MessageSent, but $event->sent does not exist, so the message_id line will fail.

Clear cached configuration and events, confirm the listener is registered and send a test message:

php artisan optimize:clear
php artisan event:list | grep -A1 MessageSent
php artisan tinker --execute="Mail::raw('Test', fn (\$m) => \$m->to('you@example.com')->subject('SMTP test'));"Code language: PHP (php)

Each email adds one line like this to storage/logs/mail-YYYY-MM-DD.log:

[2026-11-29 10:15:02] production.INFO: Sent Email {"to":"you@example.com","subject":"SMTP test","message_id":"0109018f...@email.amazonses.com"}Code language: JavaScript (javascript)

Count sent emails with grep

Each log line now stands for one email. Leave out -o, because -c already counts matching lines. When you give grep -c several files, it prints one count per file. Use -h with wc -l to get a single total.

cd /opt/lampp/htdocs/your-project

# Today
grep -c "Sent Email" storage/logs/mail-$(date +%F).log

# Total across all retained mail logs
grep -h "Sent Email" storage/logs/mail-*.log | wc -l

# Count per day
grep -h "Sent Email" storage/logs/mail-*.log | cut -c2-11 | sort | uniq -c

# Count per hour for one day
grep -h "Sent Email" storage/logs/mail-2026-11-29.log | cut -c2-14 | sort | uniq -c

# Emails sent to one recipient
grep -h "Sent Email" storage/logs/mail-*.log | grep -c '"to":"user@example.com'Code language: PHP (php)

The per-day command prints output like the following. The count comes first, then the date:

    412 2026-11-27
    389 2026-11-28
    157 2026-11-29

Note: If logrotate compresses old files, use zgrep -h "Sent Email" storage/logs/mail-*.log* | wc -l so the .gz files are counted as well.

Track failed sends separately

Failed SMTP sends throw a Symfony\Component\Mailer\Exception\TransportException. If nothing catches it, Laravel logs it to the default channel, and for queued mail the job ends up in failed_jobs. Search for the exception class, not the host name. Errors come with stack traces, so count only the lines that start a log entry:

# Logged transport errors
grep -h "TransportException" storage/logs/laravel*.log | grep -c '^\['

# Typical messages behind those errors
grep -hoE "Connection could not be established[^\"]*|Expected response code [0-9]+[^\"]{0,80}" storage/logs/laravel*.log | sort | uniq -c | sort -rn | head

# Queued mail that failed
php artisan queue:failedCode language: PHP (php)

Cross-check with your SMTP provider

A MessageSent line means the SMTP server accepted the message. It does not mean the message was delivered. Bounces, complaints and rejections happen after acceptance, so check the provider's own figures as well. For Amazon SES, which the email-smtp.ap-south-1.amazonaws.com host in the example above belongs to, the CLI returns delivery attempts, bounces, complaints and rejects for roughly the last two weeks, in 15-minute data points:

aws ses get-send-statistics --region ap-south-1 \
  --query 'sum(SendDataPoints[].DeliveryAttempts)'Code language: JavaScript (javascript)

If this number is much higher than your log count, other applications or servers are probably using the same SES identity or SMTP credentials. If it is lower, check whether MAIL_MAILER points somewhere else in one of your environments.

Corrected version of the original commands

# Run from the project root, not the XAMPP web root
cd /opt/lampp/htdocs/your-project

# Requires the MessageSent listener shown above
grep -h "Sent Email" storage/logs/mail-*.log | wc -l

# Failures, counted once per log entry
grep -h "TransportException" storage/logs/laravel*.log | grep -c '^\['Code language: PHP (php)
  • The project path now ends at the application folder, so only that application's storage/logs is searched.
  • The string Sent Email is produced by the listener, so the count means something.
  • The smtp and host-name searches are dropped as send counters, because they match error messages and stack traces.
  • -o | wc -l is replaced by line counts, so each email or failure is counted once.

Best practices

  • Log only metadata (recipient, subject, message ID). Never log message bodies, attachments or SMTP credentials.
  • Recipient addresses are personal data. Set days on the mail channel to a retention period your privacy policy allows.
  • If you need reports or dashboards, write a row to a database table from the same listener instead of parsing logs. Counting with SQL is faster and survives log rotation.
  • Keep MAIL_MAILER=log in local and staging environments so test mail does not inflate production SES statistics.
  • Run queue workers under a process supervisor so queued mail is actually sent and logged.

Frequently asked questions

Does Laravel log sent emails by default?

No. With the smtp mailer, a successful send writes nothing to laravel.log. The log mailer writes the full message to the log instead of sending it. To record real sends, listen for the MessageSent event.

Where is the Laravel log file located?

It is in the project's storage/logs directory. With the single channel the file is laravel.log, and with the daily channel the files are named by date, such as laravel-2026-11-29.log.

Does MessageSent mean the email was delivered?

No. It means the SMTP server accepted the message. Delivery, bounces and complaints are only visible in the provider's statistics or through its event notifications.

How do I count queued emails in Laravel?

The MessageSent listener handles them. The event fires in the queue worker when the message is actually sent, so queued and synchronous mail are both logged once. Failed queued mail appears in php artisan queue:failed.

Why does grep -c give per-file counts instead of a total?

When grep -c searches several files, it prints one count per file. Use grep -h "pattern" files | wc -l to get a single total.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

AWS IAM Policy to Protect SES SMTP

You can lock down Amazon SES SMTP credentials with an IAM policy on the IAM user that owns them. A Deny on aws:SourceIp limits which IP addresses…

Read More

PHP Code for Godaddy VPS Server SMTP Email Relay

This page shows how to send email from a GoDaddy VPS through GoDaddy's SMTP relay on port 25 with plain PHP sockets and no extra libraries. Below…

Read More

Unix grep Quick Reference: Patterns, Options, and Real Command Examples

grep is the core Unix command for searching text by pattern across files or standard input. This revised quick reference covers syntax, high-value options, and practical examples you can use immediately.

Read More

Email Authentication & Communication Infrastructure in Modern DevOps: The 2026 Definitive Guide

In the high-stakes world of modern DevOps, we spend our days (and sometimes our nights) obsessing over the “golden signals” of site reliability: latency, traffic, errors, and…

Read More

The 5 Most Popular Email APIs Among Developers In 2026

In the modern world, where everything is going digital, email is among the most important means of communication both in personal and business life. As a developer,…

Read More

Senior Exchange Administrator: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path

The Senior Exchange Administrator is accountable for the reliability, security, performance, and lifecycle management of the organizationโ€™s Microsoft messaging platform, typically spanning Exchange Online (Microsoft 365), Exchange Server (on-premises), or a hybrid configuration. This role ensures email and calendaring services are highly available, compliant, and aligned to business needs while proactively reducing operational risk and improving user experience.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x