Change Message-ID PHPMailer - php

Hello I am making a php based mailing application that will connect with a external smtp server and send emails. Now I have managed to match everything but the Message-ID's #domain-name and Sender domain name are not matching...
This is the result I am getting :
Wrong Message ID Header
and this is the result I should be getting (this email is sent from Mailwizz connected with the same SMTP server I am trying to connect with my application)
Expected Message ID Header
send.php file which I am using to connect with SMTP using PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp*****************';
$mail->SMTPAuth = true;
$mail->Username = 'notify#send.al***********';
$mail->Password = '**********';
$mail->SMTPSecure = 'tls';
$mail->Port = 80;
$mail->SetFrom('jon#al***********','John Adams');
$mail->Sender = 'notify#send.al*********';
$mail->addAddress('*********#gmail.com');
$mail->Subject = 'Hello This is a TEST FROM SMTP';
$mail->isHTML(false);
$mail->Body = 'Hello let me know when its received';
$mail->addCustomHeader('X-Sender', 'notify#send.al**********');
$mail->XMailer=null;
$mail->MessageID = md5('HELLO'.(idate("U")-1000000000).uniqid()).'-'.$type.'-'.$id.'#send.al*********';
if(!$mail->send()){
echo 'Error is '.$mail->ErrorInfo;
}
else{
echo 'Message has been sent!';
}
?>

In my experience, you do not need to use addCustomHeader if you want to set the MessageID.
Assuming that you want to set the Message ID to be [random]#send.alok, then please use the following:
$mail->MessageID = "<" . md5('HELLO'.(idate("U")-1000000000).uniqid()).'#send.alok>';
Hence, please the following will be ok:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require './Exception.php';
require './PHPMailer.php';
require './SMTP.php';
$user='smtp_xxxxxx_user';
$pass='smtp_password';
$mail = new PHPMailer(true);
try {
$mail->CharSet ="UTF-8";
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.xxxxx.com';
$mail->Username = $user;
$mail->Password = $pass;
$mail->MessageID = "<" . md5('HELLO'.(idate("U")-1000000000).uniqid()).'#send.alok>';
$mail->setFrom('jon#alxxxx.com', 'Jon Al');
$mail->addAddress('jon#gmail.com');
$subject="test";
$message="test123";
$mail->Port = 25;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
// echo 'Success';
} catch (Exception $e) {
//echo 'Failed';
}
?>
You may refer to the screen dump below for the result

Related

Problems with sending an email using smtp mandrill

I have a problem with my code, the intension is to send an email using smtp.mandrill.com and use the alert to know when the email is sended, otherwise send another alert when fails to send the email. And I recived the alert confirming the email, but is'nt sended. If anyone knows the problem I thanks a lot your help.
This is the code.
<?php
$name = $_POST["first_name"];
$telefono = $_POST["telephone"];
$texto = $_POST["text"];
$body =
"Name: ".$name."<br>
Telephone: ".$telephone."<br>
Message: ".$text;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPmailer/Exception.php';
require 'PHPmailer/PHPMailer.php';
require 'PHPmailer/SMTP.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0; //"2" Enable verbose debug output. Change to "0" to hide all the letters n.n
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.mandrill.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'example#example.com'; //SMTP username
$mail->Password = 'Any API key'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465;
//Recipients
$mail->setFrom('example#example', 'Example');
$mail->addAddress('example#example'); //Add a recipient
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Contact';
$mail->Body = 'New message from <b>Contact</b><br><br>'.$body;
$mail->CharSet = 'UTF-8';
$mail->send();
echo '<script>
alert("The data was sent successfully.");
window.history.go(-1);
</script>';
} catch (Exception $e) {
echo '<script>
alert("An error occurred while sending the data.");
window.history.go(-1);
</script>';
}
?>
Update your SMTP SECURE AND PORT TO:
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
That solved my issues with using Mandrill with SMTP.

PHPMailer redirecting to GitHub

When I try to run a simple PHPMailer test script it is redirecting me to GitHub site.
My script is given below. Can someone please help?
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../PHPMailer/src/PHPMailer.php';
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587 or 465
$mail->IsHTML(true);
$mail->Username = "myaccountname";
$mail->Password = "mypassword";
$mail->setFrom('fromemailid', 'from name');
$mail->Subject = 'Test Mail';
$mail->Body = 'Test mail body';
$mail->AddAddress("someone#gmail.com");
if (!$mail->send())
{echo "Mailer Error: " . $mail->ErrorInfo;
return false;
} else {
return true;
}
?>
Which folder you exactly adding your code. As see your code

SMTP: CLIENT: 535 5.7.3 Authentication Unsuccessful

I'm trying to send an email every time someone inputs a form on my website but the SMTP isn't working. It keeps on showing me this error. The credentials of the email account are correct. Also, I have already tried SMTPAuth = False; but still the same result.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'TLS';
$mail->SMTPAuth = true;
$mail->Username = 'sender#test.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
//Recipients
$mail->setFrom('test#test.com', ' Services');
$mail->addAddress('recipient#test.com', $name); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Contact Us Message';
$mail->Body = 'Sent a message, This is the message :';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header('Location: tempage.php');
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
What is going wrong? Thanks for your help.
I'm also struggling 2 days with same issue.
After i find this solution. It's working fine.
Try this.
$mail->Host = 'smtp.office365.com';
change to
$mail->Host = 'smtp.live.com';

PHPMailer not working on Web Hosting

I have been able to send emails using xampp, but when I tried to use it in my online server it doesn't seem to work. I tried changing the values on what was written on my email account information. It doesn't seem work, I was hoping for any guidance or help for anyone who has encountered this problem.
This is the email account details
then this is my code for my mailer.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->setFrom('email', 'Test Message');
$mail->addAddress("email");
$mail->isHTML(true);
$mail->Subject = 'This is the Subject';
$mail->Body = 'This has been sent';
$mail->AltBody = 'This has been sent';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: <br>', $mail->ErrorInfo;
}
I get the error SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed.
I am still kinda new to this, so sorry in advance for anything. I feel I am soo close to getting it work haha!
Thanks in advance for any help! :D
it's so simple... Use this code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
$name = 'Name to be displayed!';
$message = 'Never Give Up!';
$subject = 'Test mail!';
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'yourmail#gmail.com';
$mail->Password = 'youpass';
$mail->SMTPSecure = 'tls';
$mail->addReplyTo($to, $name);
$mail->setFrom($to, $name);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHtml($message);
$mail->send();
?>
Hope you added PHPMailer Library in Root Directory if not then please check the path also

PHPMailer, no Email and no Error

I want to send a large mail from my webspace. This mail contains many variables, which are inputed through a form.
First I tried the php mail(), but I didn't receive any mail. I used mail() in another part of my website and from this script I receive a mail. So the function should work on my webspace.
For the large mail I tried to use PHPMailer, because the mail() didn't work as expected. The code for the PHPMailer is:
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxx.xxx.de';
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Username = 'xxx';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->From = 'xxx#xxx.de';
$mail->FromName = 'xxx';
$mail->addAddress('xxx#xxx.de', 'xxx xxx');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $mess;
$mail->AltBody = $mess;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
After the script has finished, I don't get an error and I don't receive a mail. What could be a problem for this? How can I check, why my PHPMailer is not working? Has the require path to be absolute or relative?
Try this way:
//... another option code
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
//...another your code
dump($mail->send());
Each time ,you open this php-page,the mail result will let you know.what are you missed.
use try/catch as follow :
$mail = new PHPMailer(true); // enable exceptions
try {
$mail->isSMTP();
//....
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}

Categories