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.
Related
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
My PHPmailer from Github does work on my local host but not on my Firebase hosted website. When I submit on my website it downloads the file. Now how to solve it? Do I have to put some extra code in? Someone experienced with Firebase?
<?php
// get variables from the form
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Load Composer's autoloader
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try{
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = '°°°°°°°'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
// Sender
$mail->setFrom($email, $name);
// Recipients
$mail->addAddress('example#gmail.com', 'Luk Ramon'); // Add a recipient
// Body content
$body = "<p>You received an email from your website <br>name:<strong>".$name." </strong><br>subject: <strong>".$subject."</strong><br>message:<br><i>".$message."</i></p> Contact back on ".$email;
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Company-name message from '.$name;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Firebase hosting does not support PHP which is why the file is being downloaded.
You'd need to write this in Javascript to use with Firebase - https://firebase.google.com/docs/hosting/functions
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';
I wrote a PHP mailer code to send emails to multiple recipients. But unfortunately i found a drawback in script, i am fetching emails from database, lets see if it fetches 5 emails and try to send email to all of them and no.3 email is not a valid address(any reason) then my script just "exist" the function entirely by sending mail to 1-2 and showing error invalid email 3 and "exit"(not continuing further), what i want is rather to "exit" the function it should just skip that iteration statement(email) and then go on to next one like the "continue" statement. Also in code the message and subject are being sent from another page which consists of a form.
Here is the code :
<?php
// 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;
if(isset($_SESSION['userIs']))
{
$message = $_POST['message'];
$subject = $_POST['subject'];
//Load composer's autoloader
require './PHPMailer/vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'trying.test.00'; // SMTP username
$mail->Password = 'trying.test.00'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('trying.test.00#gmail.com', 'Tester');
include('./create-connection.php');
$get_list = "select name,email from signup";
$result = $conn->query($get_list) ;
if($result->num_rows>0)
{
while($row_list = $result->fetch_assoc())
{
$to = $row_list['email'];
$name = $row_list['name'];
$mail->addAddress($to,$name); // Add a recipient
}
}
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = strip_tags($message);
if($mail->send())
{
echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-check"></i></div>';
}
} catch (Exception $e) {
echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-times"></i></div>';
}
$conn->close();
}
?>
You could validate the email addresses before sending.
Or better:
You could validate the email addresses before inserting in the database so you know that the database contains only valid data.
Just needed to add a validation to check if the email is valid or not without sending email inside while loop, then if email is valid process the mail else "continue" the current iteration(email) and jump to next email.
Thus after changing code ->
<?php
// 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;
if(isset($_SESSION['userIs']))
{
$message = $_POST['message'];
$subject = $_POST['subject'];
//Load composer's autoloader
require './PHPMailer/vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'trying.test.00'; // SMTP username
$mail->Password = 'trying.test.00'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('trying.test.00#gmail.com', 'Tester');
include('./create-connection.php');
$get_list = "select name,email from signup";
$result = $conn->query($get_list) ;
if($result->num_rows>0)
{
while($row_list = $result->fetch_assoc())
{
//add a validation here
if(!filter_var($row_list['email'], FILTER_VALIDATE_EMAIL))
{
echo "invalid email".$row_list['email'];
continue;
}
else
{
$to = $row_list['email'];
$name = $row_list['name'];
$mail->addAddress($to,$name); // Add a recipient
}
}
}
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = strip_tags($message);
if($mail->send())
{
echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-check"></i></div>';
}
} catch (Exception $e) {
echo '<div class=""><b>'.$to.'</b> -> Status <i class="fa fa-times"></i></div>';
}
$conn->close();
}
?>
I have used the PHP mailer function to send a mail. This code has worked on XAMPP and 000webhost.com, but its not working on aws.
<?php
require("\PHPMailer\PHPMailer\class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
//$mail->SMTPDebug = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "****#gmail.com"; // Enter your SMTP username
$mail->Password = "************"; // SMTP password
$webmaster_email = $_POST['email']; //Add reply-to email address
$email= "***#gmail.com"; // Add recipients email address
$name= "Recipient's name"; // Add Your Recipient’s name
$mail->From = $webmaster_email;
$mail->FromName = $_POST['name'];
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,$_POST['name']);
$mail->WordWrap = 500; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "website inquiry - Support";
$mail->Body = nl2br($_POST['message']);
$mail->AltBody = nl2br($_POST['message']); //Plain Text Body
if($mail->Send())
echo 1;
else
echo 0;
?>
Please tell me where I am going wrong.
When I am executing this code on aws..its echoing 0
Thanks in advance!
Try with throwing errors by adding "true" to constructor:
try {
new PHPMailer(true);
...your code here...
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
So perhaps there are some more informations about the error.