0

I have problems when I setup mail forwarding in PLESK.

I setup a mail sending PHP script, using mail() function, but after getting errors on receiving the mails, my hosting (interserver.net) suggested the following:

I would suggest you use PHPMailer with SMTP authentication to send out mails to avoid issues with mails being rejected/marked as spam when using PHP mail(). Please check and let us know for further assistance.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../../PHPMailer/src/Exception.php';
require '../../PHPMailer/src/PHPMailer.php';
require '../../PHPMailer/src/SMTP.php';
ini_set("SMTP", "albavaluecentre.ro");
ini_set("smtp_port", "25"); 

$errorMSG = "";
$name = "";
$email = "";
$price = "";
// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}
// Price
if (empty($_POST["price"])) {
    $errorMSG .= "Price is required ";
} else {
    $price = $_POST["price"];
}

$EmailTo = "office@albavaluecentre.ro ";

$Subject = "✨New Message Received from Domain Selling✨";
 
$headers = 'From: office@albavaluecentre.ro' . " " .
'Cc: '.$email.'' . " " .
'Bcc: '.$email.'' . " " .
'Reply-To: '.$email.'' . " " .
'X-Mailer: PHP/' . phpversion();

$Subject = '✨New Message Received ✨ Bid from Client: '.$price;

// prepare email body text
$Body = "";
$Body .= "NAME: ";
$Body .= $name;
$Body .= "<br/>";
$Body .= "EMAIL: ";
$Body .= $email;
$Body .= "<br/>";
$Body .= "PRICE: ";
$Body .= $price;
$Body .= "<br/>";
$Body .= "DOMAIN: "; 
$Body .= $_SERVER['HTTP_HOST'];  

$mail2 = new PHPMailer();
$mail2->isSMTP();
$mail2->isHTML(true);
$mail2->CharSet = "UTF-8";
$mail2->Host = 'albavaluecentre.ro';
$mail2->SMTPAuth = true;
$mail2->Username = 'office@albavaluecentre.ro'; 
$mail2->Password = 'XXX';
$mail2->SMTPSecure = 'tls';
$mail2->Port = 25;
$mail2->setFrom($email, 'BidClient');
$mail2->addAddress('office@albavaluecentre.ro', 'Bid Office');

$mail2->Subject = $Subject;
$mail2->Body = $Body;

if($mail2->send())
{
    echo 'Message has been sent';
}
else
{
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail2->ErrorInfo;
}
?>

for website: link

I changed the PHP mail() to PHPMailer as suggested above to:

https://pastebin.com/r1n2kSuz

but I got the following errors, when I send an email from website:

  1. on office@albavaluecentre.ro:
  1. on ecclesiasoft@yahoo.com:

Is the PHP code wrong or what I'm doing wrong? (sorry, but I'm a PHP newbie)

Steve
  • 13
  • 2
  • 1
    In almost all cases, nowadays, you can only use an email address in the _send from_ that belongs to the same domain as that which is associated with the SMTP server you're using. In your case that would probably be: "albavaluecentre.ro". So you cannot send an email with "example@yahoo.com" in the from address. – KIKO Software Jul 27 '23 at 10:17
  • 1
    Use a fixed "From" address which is associated with the SMTP domain. Otherwise, the server may reject it, or other mail systems may classify it as suspicious, or spam. Because, basically what you're trying to do is impersonate someone else's mailbox! If you need to include the originator's email address, put it in the Reply-To field of the message, and/or in the main message body. Don't attempt to send the mail as if you were that person. If you are authenticating to the mailserver as `office@albavaluecentre.ro` , the server _may_ require you to use _only_ that address in the "From" field. – ADyson Jul 27 '23 at 10:20
  • 1
    P.S. I cannot access any of your error messages as that site is blocked in my current network. As per [ask] and related guidance, please paste all relevant information _directly_ into your question in text format, so it is a) accessible to everyone reading your post, and b) preserved for future readers (in case, for any reason, the external links are removed or changed). You can [edit] your post. Thanks. – ADyson Jul 27 '23 at 10:21
  • 1
    There is lots of excellent guidance about problems you can have when sending emails using PHP (both with mail() and SMTP in fact) at [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) - it is essential reading for anyone attempting this task. – ADyson Jul 27 '23 at 10:24
  • @ADyson The error messages: [err1](https://pastebin.com/KQRrDzK1), [err2](https://pastebin.com/vgUjWbvk). So how can I implement this in my code ? – Steve Jul 27 '23 at 11:05
  • @ADyson `$mail2->SetFrom('office@albavaluecentre.ro', 'MyMailbox'); $mail2->AddReplyTo($email, 'Client'); $mail2->AddAddress('office@albavaluecentre.ro', 'MyMailbox');` I'm now receiving the email without an error on yahoo, but the FROM is the office@albavaluecentre.ro. When I press Reply, it is OK. Is it possible to show the client email as From field? – Steve Jul 27 '23 at 11:36
  • 1
    No, that is not possible. The FROM field is what you set it to. This way people can be more sure from which email address the email came, and you cannot pretend to own an email address that you don't. – KIKO Software Jul 27 '23 at 11:52
  • 1
    Please read the comment from @ADyson about PasteBin again. The request was to edit your question and not link to external resources for essential parts of your question. – KIKO Software Jul 27 '23 at 11:54
  • `Is it possible to show the client email as From field`...no. But why do you care? In this case you're sending the email to yourself anyway. Put the From name in the subject line if you need to see it without opening the mail – ADyson Jul 27 '23 at 12:25
  • thank you all for your help – Steve Jul 27 '23 at 19:39

0 Answers0