Mailgun different ways to send
Found myself making a simple page with just a contact page, needed to send an email from the website using Mailgun.
composer require mailgun/mailgun-php
Then make a contact.php
<?php
// Start a Session
session_start();
require 'vendor/autoload.php';
use Mailgun\Mailgun;
$post = $_POST;
if (!empty($post) && !empty($post['email']) && !empty($post['name']) && !empty($post['subject']) && !empty($post['message']) && !empty($post['phone']) && !empty($post['company'])) {
$mgClient = Mailgun::create('key');
$domain = "mg.domain.vc";
$message = [
'from' => $post['email'],
'to' => '[email protected]',
'subject' => 'Contact From '.$post['company'].' - '.$post['name'].' - '.$post['subject'],
'text' => $post['message'].'<br><br>Phone :'.$post['phone']
];
$mgClient->messages()->send($domain, $message);
$_SESSION['message'] = 'Your message is well received';
}
header("Location: https://www.domain.vc");
?>
[Update 9/9/24]
Sending simple emails in laravel
I sometimes forget how to set this, so here's how :
MAIL_MAILER=mailgun
MAILGUN_DOMAIN=mg.domain.com
MAILGUN_SECRET=yoursecretkey
You just need to change the mail_mailer to Mailgun and find your secret key.
To try it out, make a command
php artisan make:command SendTestEmail
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class SendTestEmail extends Command
{
// The name and signature of the console command
protected $signature = 'email:test';
// The console command description
protected $description = 'Send a test email to verify email configuration';
// Execute the console command
public function handle()
{
$to = '[email protected]'; // Replace with your email address
$subject = 'Test Email';
$message = 'This is a test email to verify the email configuration in Laravel.';
Mail::raw($message, function ($mail) use ($to, $subject) {
$mail->to($to)
->subject($subject);
});
$this->info('Test email sent successfully.');
}
}
php artisan email:test