SMTP instellen met PHP (PHPMailer / Laravel)

Wesender werkt met elke SMTP-bibliotheek in PHP. Hieronder vind je stap-voor-stap instructies voor PHPMailer (standalone) en Laravel Mail.

Vereisten

  • Een Wesender-account met een geverifieerd domein
  • PHP 7.4 of hoger
  • Composer geïnstalleerd
  • Je API-key uit het dashboard (Instellingen → API-sleutels)

SMTP-verbindingsgegevens

Instelling Waarde
Host smtp.wesender.nl
Poort 587
Beveiliging STARTTLS
Gebruikersnaam je API-key
Wachtwoord je API-key

PHPMailer (standalone)

Stap 1: Installeren

composer require phpmailer/phpmailer

Stap 2: E-mail versturen

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Serverinstellingen
    $mail->isSMTP();
    $mail->Host       = 'smtp.wesender.nl';
    $mail->SMTPAuth   = true;
    $mail->Username   = $_ENV['WESENDER_API_KEY'];
    $mail->Password   = $_ENV['WESENDER_API_KEY'];
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;
    $mail->CharSet    = 'UTF-8';

    // Afzender en ontvanger
    $mail->setFrom('noreply@mijndomein.nl', 'Mijn App');
    $mail->addAddress('klant@voorbeeld.nl', 'Jan de Vries');

    // Inhoud
    $mail->isHTML(true);
    $mail->Subject = 'Welkom bij ons platform';
    $mail->Body    = '<h1>Welkom!</h1><p>Bedankt voor je aanmelding.</p>';
    $mail->AltBody = 'Welkom! Bedankt voor je aanmelding.';

    $mail->send();
    echo 'E-mail verstuurd!';
} catch (Exception $e) {
    echo "Fout: {$mail->ErrorInfo}";
}

Laravel Mail

Stap 1: .env aanpassen

MAIL_MAILER=smtp
MAIL_HOST=smtp.wesender.nl
MAIL_PORT=587
MAIL_USERNAME=ws_live_xxxxxxxxxxxxxxxx
MAIL_PASSWORD=ws_live_xxxxxxxxxxxxxxxx
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=noreply@mijndomein.nl
MAIL_FROM_NAME="Mijn App"

Stap 2: Mailable aanmaken

php artisan make:mail WelkomMail
// app/Mail/WelkomMail.php
class WelkomMail extends Mailable
{
    public function __construct(public string $naam) {}

    public function envelope(): Envelope
    {
        return new Envelope(subject: "Welkom, {$this->naam}!");
    }

    public function content(): Content
    {
        return new Content(view: 'emails.welkom');
    }
}

Stap 3: E-mail versturen

use App\Mail\WelkomMail;
use Illuminate\Support\Facades\Mail;

// Direct
Mail::to('klant@voorbeeld.nl')->send(new WelkomMail('Jan'));

// In de wachtrij (aanbevolen voor productie)
Mail::to('klant@voorbeeld.nl')->queue(new WelkomMail('Jan'));

Foutoplossing

  • SMTP-verbinding mislukt — zorg dat SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS staat, niet ssl
  • Authenticatiefout — gebruikersnaam én wachtwoord zijn beide je Wesender API-key
  • E-mail komt niet aan — controleer of het verzenddomein geverifieerd is (SPF, DKIM, DMARC)
  • Certificate error — voeg toe: $mail->SMTPOptions = ['ssl' => ['verify_peer' => false]] (alleen voor lokaal testen!)