I am using PHPMailer for sending emails. But I have to write that long code of sending emails on every page. So I thought of trying to put the whole stuff into a function and just calling it whenever I want to make the things dry, simple and easier. But its not working when trying to send emails. I tried the following:
functions.php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$settings = $pdo->prepare("SELECT * FROM settings");
$settings-> execute();
$set = $settings->fetch();
function newMail($name, $email, $sub, $msg, $set) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = $set['set_smtp_host'];
$mail->Port = $set['set_smtp_port'];
$mail->SMTPSecure = $set['set_smtp_security'];
$mail->IsHTML(true);
$mail->SMTPAuth = true;
$mail->Username = $set['set_smtp_uname'];
$mail->Password = $set['set_smtp_pass'];
$mail->setFrom($set['set_noreply_email'], $set['set_site_name']);
$mail->addAddress($email, $name);
$mail->Subject = $sub;
$mail->Body = $msg;
$mail->Send();
}
Now I tried calling the function on another page (where functions.php is included) this way:
$fname = (!empty($_POST['fname']))?$_POST['fname']:null;
$email = (!empty($_POST['email']))?$_POST['email']:null;
$sub = ''.$title.' - Account Verification Link';
$msg = 'SOME BODY MESSAGE';
if(newMail($fname, $email, $sub, $msg)){
echo alert_success("Registration successful! Please check your email and click on the activation link to activate your account. If you did not receive any email within 5 minutes then <a href='resend.php'>click here</a> to resend it.");
}else{
echo alert_success("Registration successful! But unfortunately, we could not send you a verification email. Please <a href='resend.php'>click here</a> to resend it.");
}
Here its always returning the else message. Am I wrong coding something here?
Modify your function newMail at the end by :
return $mail->Send();
The send method return true in case the mail is sent , so you function should return this value , if not :
if(newMail(...)){ }
Will always be false that why the else case is applied.
function newMail($name, $email, $sub, $msg, $set) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = $set['set_smtp_host'];
$mail->Port = $set['set_smtp_port'];
$mail->SMTPSecure = $set['set_smtp_security'];
$mail->IsHTML(true);
$mail->SMTPAuth = true;
$mail->Username = $set['set_smtp_uname'];
$mail->Password = $set['set_smtp_pass'];
$mail->setFrom($set['set_noreply_email'], $set['set_site_name']);
$mail->addAddress($email, $name);
$mail->Subject = $sub;
$mail->Body = $msg;
return $mail->Send(); // add return here
}
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
I am using PHPMailer to send emails. I have created a function that sends 3 emails to 3 different email addresses (sending 9 emails in total).
The first email address is receiving all the 3 emails.
The second email address is receiving 2 emails.
The third email address is receiving only 1 email.
Why this happening?
Here is my code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'lib/phpmailer/vendor/autoload.php';
$mail = new PHPMailer(true);
$mail1 = phpmaileremail($reciever1, $usertype1, $file, $subject1, $body1);
$mail2 = phpmaileremail($reciever2, $usertype2, $file, $subject2, $body2);
$mail3 = phpmaileremail($reciever3, $usertype3, $file, $subject3, $body3);
function phpmaileremail($reciever,$usertype, $file, $subject, $body)
{
global $mail;
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxx#gmail.com';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('xxx', 'xxx');
$mail->addAddress($reciever);
$mail->addAddress($reciever, $usertype);
$mail->addAttachment($file);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = 'NA';
$mail->send();
echo "Mail sent";
}
Because you're reusing the $mail object to addAddress() and send(). So the first time you call phpmaileremail() the first address gets the email. Then when you call it for the second time the second address is added and the first and second address get the email. And so on.
A simple solution would be to create the $mail object inside the phpmaileremail() function:
function phpmaileremail($reciever,$usertype, $file, $emailsubject, $email_body )
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = 'XXXXXXXX#gmail.com';
$mail->Password = 'XXXXXXXXXXXXXXXXXX';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
$mail->addAddress($reciever);
$mail->addAddress($reciever, $usertype);
// Attachments
$mail->addAttachment($file); // Add attachments
$mail->isHTML(true);
$mail->Subject = $emailsubject;
$mail->Body = $email_body;
$mail->AltBody = 'NA';
$mail->send();
echo "Mail sent";
}
PS: Not that it matters, but reciever is written receiver. I've made that mistake as well.
Kiko's answer will work, however it's not the best way. As its name suggests, addAddress adds an address, it doesn't set absolutely or replace existing recipients you've already added.
PHPMailer has a standard function to clear the list of addresses you're ending to called clearAddresses, so the right approach is to call that after each message you send and add the new address before sending the next one, so the sequence will be roughly:
addAddress();
send();
clearAddresses();
addAddress();
send();
and so on. This is most clearly demonstrated in the mailing list example provided with PHPMailer, which does its sending in a loop, calling clearAddresses each time around.
You can achieve the same thing using a new instance of PHPMailer each time (which has the effect of clearing addresses, but also clears everything else too), but it's more efficient to re-use the instance. This is especially true if you're sending over SMTP (which you are) because it will allow you to make use of keepalive, which dramatically reduces the overhead of making an SMTP connection. If you use a new instance, the connection is dropped and recreated each time. You can achieve this inside your function by making the PHPMailer instance static:
function phpmaileremail($reciever, $usertype, $file, $emailsubject, $email_body)
{
static $mail;
if ($mail === null) {
//Set everything that remains the same all the time in here
$mail = new PHPMailer();
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = 'XXXXXXXX#gmail.com';
$mail->Password = 'XXXXXXXXXXXXXXXXXX';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPKeepAlive = true;
$mail->setFrom('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
}
$mail->addAddress($reciever, $usertype);
// Attachments
$mail->addAttachment($file); // Add attachments
$mail->isHTML(true);
$mail->Subject = $emailsubject;
$mail->Body = $email_body;
$mail->AltBody = 'NA';
$mail->send();
$mail->clearAddresses();
$mail->clearAttachments();
echo "Mail sent";
}
This has the added benefit of not using a global. Also note the use of clearAttachments, as that works the same way as addresses.
So I am trying to set up a contact us page on a website i am creating but PHPMailer just refuses to work for me.
From what I can see, it just isn't recognizing the PHPMailer class at all and I am not really sure why. Here is my PHP code for the form to send the email, as far as i can tell i have correctly installed PHPMailer/Composer, and have done everything else correctly, but i still get errors when trying to use PHPMailer at all. "PHPMailer cannot resolved to a type" and "The import PHPMailer\PHPMailer\PHPMailer cannot be resolved"
<?php
use PHPMailer\PHPMailer\PHPMailer;
require_once './vendor/autoload.php';
// Bunch of code validating all the variables from the form
function send_mail($toEmail, $endSubject, $endMessage, $endName) {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = "smtp.domain.com";
$mail->SMTPAuth = true;
$mail->Username = "LOGIN EMAIL"; // Email
$mail->Password = "PASSWORD"; // Email password
$mail->SMTPSecure = "tls";
$mail->Port = "587";
$mail->From = "example#domain.com"; //RANDOM EMAIL TO SEND THESE MESSAGES TO OUR EMAIL
$mail->FromName = $endName; //CUSTOMERS NAME
$mail->addAddress($toEmail); //WHOEVER WE ARE SENDING THESE EMAILS TO
$mail->isHTML(true);
$uri = 'http://'. $_SERVER['HTTP_HOST'];
$mail->Subject = $endSubject;
$mail->Body = $endMessage;
if($mail->send()) {
return true;
} else {
return false;
}
}
?>
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 am new to php and i want solution for variable pass in mail function
I am using this way to variable pass mail function but getting issue Code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
function Send_Mail($to,$subject,$body,$cc = "",$bcc = "")
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPDebug = false;
$mail->Username = 'xyz#gmail.com'; // Your Gmail Address
$mail->Password = '123456'; // Your Gmail Password
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'xyz#gmail.com'; // From Email Address
$mail->FromName = 'gfgg'; // From Name
$mail->addAddress($to);
$mail->addReplyTo('xyz#gmail.com', 'dfg'); // Same as From Email and From Name.
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send()) {
return array('status' => false, 'message' => $mail->ErrorInfo);
}
return array('status' => true, 'message' => 'Email Sent Successfully');
}
if (isset($_REQUEST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
echo $name, $email, $contact;
$result = Send_Mail('$name','$email','$contact');
}
pls give me soltution
?>
The Send_Mail function requires minimum 3 parameters, which are $to, $subject and $bodyin this order.
So first of all you are calling your parameters within strings
$result = Send_Mail('$name','$email','$contact');
and in the wrong order.
You should probably try this
$result = Send_Mail($email, "Subject", "Message");
You will have to define $subject and $message variables and replace them with "Subject" and "Message"
Try with -
echo "$name, $email, $contact";
$result = Send_Mail($name,$email,$contact);