1st Domain

Hvordan sender jeg e-mails med SMTP Auth? PHP eksempel

PHP mail() tillader som standard ikke brugen af en ekstern SMTP server og supporterer ikke SMTP autorisation — påkrævet af mange mailservere i dag, herunder vores mailservice, for at undgå misbrug.

For at tilgå vores SMTP server via et PHP script med brug af SMTP autorisation til afsendelse af e-mail kan følgende vejledning benyttes:

  • udpak og upload til din hjemmeside følgende gratis pakke kaldet PHPMailer, som kan hentes her
  • opret en fil til afsendelse af beskeder, f.eks. mail.php
  • inkluder og tilret følgende instillinger i dit script:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
 
require 'path/to/PHPMailer/src/Exception.php';  // Be sure to set the correct path
require 'path/to/PHPMailer/src/PHPMailer.php';  // Be sure to set the correct path
require 'path/to/PHPMailer/src/SMTP.php';       // Be sure to set the correct path
 
// Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
 
try {
  $mail->SMTPDebug = SMTP::DEBUG_OFF; // To enable debugging, set SMTP::DEBUG_SERVER
  $mail->isSMTP();
  $mail->Host       = 'mail.web-solutions.eu';
  $mail->SMTPAuth   = true;
  $mail->Username   = 'user@domain.tld'; // Enter your email address
  $mail->Password   = 'password';        // Enter your password
  $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
  $mail->Port       = 465;
 
  $mail->setFrom('from@domain.tld', 'From Name');
  $mail->addAddress('to@domain.tld');
 
  $mail->isHTML(true);
  $mail->Subject = 'Here is the subject';
  $mail->Body    = 'This is the HTML message body in bold!';
  $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
 
  $mail->send();
  echo 'Message has been sent';
} catch (Exception $e) {
  echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Se den officielle dokumentation for yderligere oplysninger.

Du skal benytte følgende instillinger:

  • host: mail.web-solutions.eu
  • port: 465
  • SMTPsecure: SSL
  • SMTPAuth: true
  • username: mail username (din e-mail adresse)
  • password: your mail password