Packages

phoenix_kit

1.7.21
1.7.208 1.7.207 1.7.206 1.7.205 1.7.204 1.7.203 1.7.202 1.7.201 1.7.200 1.7.199 1.7.198 1.7.197 1.7.196 1.7.194 1.7.193 1.7.192 1.7.191 1.7.190 1.7.189 1.7.187 1.7.186 1.7.185 1.7.184 1.7.183 1.7.182 1.7.181 1.7.180 1.7.179 1.7.178 1.7.177 1.7.176 1.7.175 1.7.174 1.7.173 1.7.172 1.7.171 1.7.170 1.7.169 1.7.168 1.7.167 1.7.166 1.7.165 1.7.164 1.7.162 1.7.161 1.7.160 1.7.159 1.7.157 1.7.156 1.7.155 1.7.154 1.7.153 1.7.152 1.7.151 1.7.150 1.7.149 1.7.146 1.7.145 1.7.144 1.7.143 1.7.138 1.7.133 1.7.132 1.7.131 1.7.130 1.7.128 1.7.126 1.7.125 1.7.121 1.7.120 1.7.119 1.7.118 1.7.117 1.7.116 1.7.115 1.7.114 1.7.113 1.7.112 1.7.111 1.7.110 1.7.109 1.7.108 1.7.107 1.7.106 1.7.105 1.7.104 1.7.103 1.7.102 1.7.101 1.7.100 1.7.99 1.7.98 1.7.97 1.7.96 1.7.95 1.7.94 1.7.93 1.7.92 1.7.91 1.7.90 1.7.89 1.7.88 1.7.87 1.7.86 1.7.85 1.7.84 1.7.83 1.7.82 1.7.81 1.7.80 1.7.79 1.7.78 1.7.77 1.7.76 1.7.75 1.7.74 1.7.71 1.7.70 1.7.69 1.7.66 1.7.65 1.7.64 1.7.63 1.7.62 1.7.61 1.7.59 1.7.58 1.7.57 1.7.56 1.7.55 1.7.54 1.7.53 1.7.52 1.7.51 1.7.49 1.7.44 1.7.43 1.7.42 1.7.41 1.7.39 1.7.38 1.7.37 1.7.36 1.7.34 1.7.33 1.7.31 1.7.30 1.7.29 1.7.28 1.7.27 1.7.26 1.7.25 1.7.24 1.7.23 1.7.22 1.7.21 1.7.20 1.7.19 1.7.18 1.7.17 1.7.16 1.7.15 1.7.14 1.7.13 1.7.12 1.7.11 1.7.10 1.7.9 1.7.8 1.7.7 1.7.6 1.7.5 1.7.4 1.7.3 1.7.2 1.7.1 1.7.0 1.6.20 1.6.19 1.6.18 1.6.17 1.6.16 1.6.15 1.6.14 1.6.13 1.6.12 1.6.11 1.6.10 1.6.9 1.6.8 1.6.7 1.6.6 1.6.5 1.6.4 1.6.3 1.5.2 1.5.1 1.5.0 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.2 1.3.1 1.3.0 1.2.10 1.2.9 1.2.8 1.2.7 1.2.5 1.2.4 1.2.2 1.2.1 1.2.0 1.1.0 1.0.0

A foundation for building Elixir Phoenix apps — SaaS, social networks, ERP systems, marketplaces, and more

Current section

Files

Jump to
phoenix_kit lib mix tasks phoenix_kit.email.send_test.ex
Raw

lib/mix/tasks/phoenix_kit.email.send_test.ex

defmodule Mix.Tasks.PhoenixKit.Email.SendTest do
@shortdoc "Send test email to specific address"
@moduledoc """
Mix task to send a test email to verify email system functionality.
This task sends a test email to a specific address without requiring
email system system configuration. Useful for basic email delivery testing.
## Usage
# Send to specific email address
mix phoenix_kit.email.send_test --to admin@example.com
# Send to multiple addresses
mix phoenix_kit.email.send_test --to admin@example.com,user@example.com
# Send with custom subject
mix phoenix_kit.email.send_test --to admin@example.com --subject "Custom Test Subject"
# Include system (requires configured repo)
mix phoenix_kit.email.send_test --to admin@example.com --track
## Options
--to EMAIL Email address to send to (required)
--subject SUBJECT Custom email subject (optional)
--track Enable email system (requires repo configuration)
--from EMAIL From email address (optional)
## Examples
# Basic test email
mix phoenix_kit.email.send_test --to admin@example.com
# Multiple recipients with custom subject
mix phoenix_kit.email.send_test --to "admin@example.com,user@example.com" --subject "PhoenixKit Test Email"
# Test with system enabled
mix phoenix_kit.email.send_test --to test@example.com --track
"""
use Mix.Task
import Swoosh.Email
@impl Mix.Task
def run(args) do
Mix.Task.run("app.start")
{options, _remaining} = parse_options(args)
recipient = options[:to]
unless recipient do
Mix.shell().error("--to option is required. Please specify recipient email address.")
exit({:shutdown, 1})
end
Mix.shell().info(IO.ANSI.cyan() <> "\n📧 Sending Test Email" <> IO.ANSI.reset())
Mix.shell().info(String.duplicate("=", 30))
send_test_email(options)
end
defp parse_options(args) do
{options, remaining, _errors} =
OptionParser.parse(args,
strict: [
to: :string,
subject: :string,
track: :boolean,
from: :string
]
)
# Set defaults
options =
options
|> Keyword.put_new(:subject, "PhoenixKit Test Email")
|> Keyword.put_new(:track, false)
|> Keyword.put_new(:from, "noreply@phoenixkit.dev")
{options, remaining}
end
defp send_test_email(options) do
recipients = parse_recipients(options[:to])
subject = options[:subject]
from_email = options[:from]
track_email = options[:track]
Mix.shell().info("Recipients: #{Enum.join(recipients, ", ")}")
Mix.shell().info("Subject: #{subject}")
Mix.shell().info("From: #{from_email}")
Mix.shell().info("Tracking: #{if track_email, do: "enabled", else: "disabled"}")
Mix.shell().info("")
timestamp = DateTime.utc_now() |> DateTime.to_string()
Enum.each(recipients, fn recipient ->
Mix.shell().info("📤 Sending email to #{recipient}...")
email =
new()
|> to(recipient)
|> from({from_email, "PhoenixKit Test"})
|> subject(subject)
|> html_body(html_email_body(recipient, timestamp, track_email))
|> text_body(text_email_body(recipient, timestamp, track_email))
result =
if track_email do
# Use PhoenixKit.Mailer with system
PhoenixKit.Mailer.deliver_email(email,
template_name: "test_email",
campaign_id: "manual_test"
)
else
# Use basic Swoosh delivery without system
send_via_configured_mailer(email)
end
case result do
{:ok, _} ->
Mix.shell().info("✅ Email sent successfully to #{recipient}")
{:error, reason} ->
Mix.shell().error("❌ Failed to send email to #{recipient}: #{inspect(reason)}")
end
# Small delay between emails
Process.sleep(500)
end)
Mix.shell().info("\n🎉 Test email sending completed!")
end
defp parse_recipients(recipient_string) do
recipient_string
|> String.split(",")
|> Enum.map(&String.trim/1)
|> Enum.reject(&(&1 == ""))
end
defp send_via_configured_mailer(email) do
# Try to use PhoenixKit.Mailer directly, falling back to simple delivery
mailer = PhoenixKit.Mailer.get_mailer()
if mailer == PhoenixKit.Mailer do
PhoenixKit.Mailer.deliver(email)
else
mailer.deliver(email)
end
rescue
error ->
Mix.shell().error("Mailer delivery failed: #{inspect(error)}")
Mix.shell().info("Attempting basic Swoosh delivery...")
# Fallback to basic SMTP if available
deliver_with_basic_config(email)
end
defp deliver_with_basic_config(email) do
# Simple SMTP configuration for testing
# This is just for development/testing purposes
adapter_config = [
adapter: Swoosh.Adapters.SMTP,
relay: "smtp.gmail.com",
port: 587,
username: System.get_env("SMTP_USERNAME"),
password: System.get_env("SMTP_PASSWORD"),
tls: :if_available,
retries: 1,
no_mx_lookups: false
]
if adapter_config[:username] && adapter_config[:password] do
Swoosh.Mailer.deliver(email, adapter_config)
else
{:error,
"No mailer configuration found. Please configure PhoenixKit.Mailer or set SMTP_USERNAME and SMTP_PASSWORD environment variables."}
end
end
defp html_email_body(recipient, timestamp, track_enabled) do
"""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PhoenixKit Test Email</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; line-height: 1.6; color: #333; background-color: #f8f9fa; }
.header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; border-radius: 8px 8px 0 0; }
.content { padding: 30px; }
.info-box { background-color: #f0f9ff; border: 1px solid #0ea5e9; border-radius: 6px; padding: 16px; margin: 20px 0; }
.system-box { background-color: #{if track_enabled, do: "#f0fdf4", else: "#fef3c7"}; border: 1px solid #{if track_enabled, do: "#22c55e", else: "#f59e0b"}; border-radius: 6px; padding: 16px; margin: 20px 0; }
.footer { background-color: #f8f9fa; padding: 20px; border-radius: 0 0 8px 8px; font-size: 14px; color: #6b7280; }
.system-info { font-family: monospace; background: #f3f4f6; padding: 10px; border-radius: 4px; margin: 10px 0; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>📧 PhoenixKit Test Email</h1>
<p>Email System Verification</p>
</div>
<div class="content">
<div class="info-box">
<strong>✅ Success!</strong> This email was sent successfully through the PhoenixKit email system.
</div>
<p>Hello,</p>
<p>This is a test email to verify your email system configuration. If you received this email, it indicates:</p>
<ul>
<li>✅ Email delivery is working correctly</li>
<li>✅ SMTP/Email provider configuration is valid</li>
<li>✅ PhoenixKit mailer is functioning properly</li>
#{if track_enabled, do: "<li>✅ Email system system is operational</li>", else: "<li>â„šī¸ Email system was not enabled for this test</li>"}
</ul>
<div class="system-box">
<strong>📊 Email Details:</strong>
<div class="system-info">
Recipient: #{recipient}<br>
Sent at: #{timestamp}<br>
Tracking: #{if track_enabled, do: "Enabled", else: "Disabled"}<br>
Type: Manual Test Email
</div>
</div>
<p>You can safely ignore this email - it's just for testing purposes.</p>
</div>
<div class="footer">
<p>This is an automated test email from PhoenixKit Email System.</p>
<p>Generated at: #{timestamp}</p>
</div>
</div>
</body>
</html>
"""
end
defp text_email_body(recipient, timestamp, track_enabled) do
"""
PHOENIXKIT TEST EMAIL - EMAIL SYSTEM VERIFICATION
================================================
Success! This email was sent successfully through the PhoenixKit email system.
Hello,
This is a test email to verify your email system configuration. If you received this email, it indicates:
✅ Email delivery is working correctly
✅ SMTP/Email provider configuration is valid
✅ PhoenixKit mailer is functioning properly
#{if track_enabled, do: "✅ Email system system is operational", else: "â„šī¸ Email system was not enabled for this test"}
EMAIL DETAILS:
--------------
Recipient: #{recipient}
Sent at: #{timestamp}
Tracking: #{if track_enabled, do: "Enabled", else: "Disabled"}
Type: Manual Test Email
You can safely ignore this email - it's just for testing purposes.
---
This is an automated test email from PhoenixKit Email System.
Generated at: #{timestamp}
"""
end
end