Hallo
Nachdem das grosse php-Forum php.de geschlossen ist, bin ich auf der Suche nach einem neuen Forum, wo man mir kompetent bei PHP-Fragen helfen kann.
Es geht bei mir um die Thematik, dass mein Mailprovider in einem Monat auf envelope-from umstellt. Bei meinen Formularen habe ich die Mailadresse der mich kontaktierenden Person als "from" gesetzt, damit ich dann mit meinem Mailprogramm einfach auf Antworten klicken kann. Dies wird künftig nicht mehr gehen. Eine Idee von mir wäre, die Absender-Mailadresse in ein Reply-To zu packen, die Frage ist nur, wie ;-) Ich habe die Seite nicht selbst gecoded sondern jemand, dessen Tagesgeschäft das ist. Er hat für den Mailversand (SMTP) "MailHelper" verwendet. Das Tool hat nach meiner laienhaften Erkenntnis keine Reply-To Option.
Code:
declare(strict_types=1);
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
class MailHelper
{
private PHPMailer $mail;
private string $ErrorInfo;
public function getErrorInfo(): string
{
return $this->ErrorInfo;
}
public function hasErrorInfo(): bool
{
return !empty($this->ErrorInfo);
}
public function __construct()
{
$this->mail = new PHPMailer(true);
$this->mail->CharSet = PHPMailer::CHARSET_UTF8;
$this->mail->isHTML(false);
}
/**
* @throws Exception
*/
public function toUser(string $subject,
string $body,
string $to_address,
string $to_name,
?string $from_address = null,
?string $from_name = null): PHPMailer
{
if (!$from_address) {
$from_address = MAIL_FROM_ADDRESS;
}
if (!$from_name) {
$from_name = MAIL_FROM_NAME;
}
// Validate to address
if (!filter_var($to_address, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid to address "'.$to_address.'" for user');
}
// Validate from address
if (!filter_var($from_address, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid from address "'.$from_address.'" for user');
}
return $this->handleMail(
subject: $subject,
body: $body,
to_address: $to_address,
to_name: $to_name,
from_address: $from_address,
from_name: $from_name
);
}
/**
* @throws Exception
*/
public function handleMail(string $subject,
string $body,
string $to_address,
string $to_name,
string $from_address,
string $from_name): PHPMailer
{
try {
/** For SMTP, change the following */
if (USE_SMTP) {
$this->mail->isSMTP();
if (DEBUG) {
$this->mail->SMTPDebug = SMTP::DEBUG_SERVER;
}
$this->mail->Host = SMTP_HOST;
$this->mail->SMTPAuth = true;
$this->mail->Username = SMTP_USERNAME;
$this->mail->Password = SMTP_PASSWORD;
$this->mail->SMTPSecure = SMTP_ENCRYPTION;
$this->mail->Port = SMTP_PORT;
} else {
$this->mail->isMail();
}
/** Add the BCC in case there are some given */
if (count(BCC_RECIPIENTS) > 0) {
foreach (BCC_RECIPIENTS as $bcc) {
// Validate BCC address
if (filter_var($bcc, FILTER_VALIDATE_EMAIL)) {
$this->mail->addBCC($bcc);
}
}
}
$this->mail->setFrom($from_address, $from_name);
$this->mail->addAddress($to_address, $to_name);
$this->mail->Subject = $subject;
$this->mail->Body = $body;
return $this->mail;
} catch (Exception $e) {
$this->ErrorInfo = $e->getMessage();
}
}
/**
* @throws Exception
*/
public function toAdmin(string $subject,
string $body,
?string $from_address = null,
?string $from_name = null): PHPMailer
{
if (!$from_address) {
$from_address = MAIL_FROM_ADDRESS;
}
if (!$from_name) {
$from_name = MAIL_FROM_NAME;
}
// Validate to address
if (!filter_var(MAIL_TO_ADDRESS, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid to address "'.MAIL_TO_ADDRESS.'" for admin');
}
// Validate from address
if (!filter_var($from_address, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid from address "'.$from_address.'" for admin');
}
return $this->handleMail(
subject: $subject,
body: $body,
to_address: MAIL_TO_ADDRESS,
to_name: MAIL_TO_ADDRESS_NAME,
from_address: $from_address,
from_name: $from_name
);
}
}
und
$mail = new MailHelper();
$mail->toAdmin(
subject: '*subject*',
body: $body,
from_address: $inputs['email'],
from_name: $inputs['name']
)->send();
Kann mir hier geholfen werden? :-)
Danke & Gruss