====== WP_MAIL mit einer SMTP-Verbindung verschicken (via PHPMailer) ====== Oft ist sinnvoll wp_mail()'s nicht direkt vom Webserver verschicken zu lassen. Mails vom Webserver landen schnell im Spam, weil sie nicht verifiziert werden können, oder die eigentliche E-Mails auf einem anderem System liegen. Daher ist es nützlich sich eine gesonderte E-Mail-Adresse einzurichten und das Wordpress dann per SMTP verbinden zu lassen. In Wordpress werden E-Mail über die [[https://github.com/PHPMailer/PHPMailer|Library PHPMailer]] verschickt, und die kommt von Haus aus mit einer Möglichkeit eine SMTP-Verbindung herzustellen. Das ist ein einfacher Beispiel Code wie man ohne Plugin eine SMTP Verbindung in Wordpress herstellen kann: SMTPDebug = SMTP::DEBUG_SERVER; } $phpmailer->isSMTP(); $phpmailer->Host = self::$Host; $phpmailer->SMTPAuth = self::$SMTPAuth; $phpmailer->Port = self::$Port; $phpmailer->Username = self::$Username; $phpmailer->Password = self::$Password; $phpmailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $phpmailer->From = self::$From; $phpmailer->FromName = self::$FromName; } } Eine einfachere Version des Codes gibt es [[https://www.media-techport.de/2020/02/emails-aus-wordpress-via-smtp-versenden-ohne-plugin/|hier im Blog]] von media-techport. ===== Hinweis zur SMPTSecure Einstellung ===== An dieser stelle wird eine Konstante übergeben, die auch in standard Systemen mit dem Port zusammen hängt: PHPMailer::ENCRYPTION_SMTPS » Port 465 PHPMailer::ENCRYPTION_STARTTLS » Port 587 Man muss jedoch nicht die Konstanten des PHPMailers nutzen, mann kann auch Strings hinterlegen $phpmailer->SMTPSecure = 'ssl'; // gleiche wie» PHPMailer::ENCRYPTION_SMTPS; $phpmailer->SMTPSecure = 'tls'; // gleiche wie» PHPMailer::ENCRYPTION_STARTTLS; Das zeigt ein Blick in die [[https://github.com/PHPMailer/PHPMailer/blob/master/src/PHPMailer.php|PHPMailer-Klasse auf Github]]. ===== SMTP Klasse nutzen um Auth-Daten von ACF zu bekommen ===== Ich schreibe solche Konfigurationen gerne in statische Klassen. Das hat den Vorteil, dass ich mir die SMTP-Verbindungsdaten auch z.B. von einer ACF-Options-Page ziehen kann. Oder halt einer eigenen Settings-Seite. In diesem erweiterten Beispiel, ziehe ich die SMTP-Konfiguration von einer ACF-Seite. Dadurch kann ich im Backend die SMTP-Daten eingeben, immer ändern und brauche nicht in den Quellcode gehen. SMTPDebug = SMTP::DEBUG_SERVER; } // Konfiguration des PHPMailers $phpmailer->isSMTP(); $phpmailer->Host = self::$Host; $phpmailer->SMTPAuth = true; $phpmailer->Port = self::$Port; $phpmailer->Username = self::$Username; $phpmailer->Password = self::$Password; $phpmailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $phpmailer->From = self::$From; $phpmailer->FromName = self::$FromName; } // --- SMTP Settings von ACF Option Page bekommen private static function set_private_smtp_parameters() { self::$use_smtp = get_field( 'acf_option_use_smpt', 'options' ) ?? false; self::$Host = get_field( 'acf_option_smpt_host', 'options' ) ?? ''; self::$Port = get_field( 'acf_option_smpt_port', 'options' ) ?? ''; self::$Username = get_field( 'acf_option_smpt_user', 'options' ) ?? ''; self::$Password = get_field( 'acf_option_smpt_pass', 'options' ) ?? ''; self::$From = get_field( 'acf_option_smpt_from', 'options' ) ?? ''; self::$FromName = get_field( 'acf_option_smpt_name', 'options' ) ?? ''; } }