PHPMailer sending multiple emails to every receipents in database - php

I am using this code to send email to everyone in the database in a loop. Problem occurred when I find out that user 1 in database also getting all emails sent to other users in the database. User 2 gets all email sent to other users after him and so on. I want to send a single email to everyone customized with their name and emails. Did check and applied couple of solutions from this forum but they are not working.
#SEND EMAILS
require '../includes/PHPMailer/PHPMailerAutoload.php';
require '../includes/PHPMailer/config.php';
if(isset($_POST['submit'])){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = $smtp_server_w;
$mail->Username = $username_w;
$mail->Password = $password_w;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->setFrom("test#test.com", "Test Name");
$sql_mail = "SELECT * FROM users";
$run_mail = mysqli_query($conn, $sql_mail);
while ($row_mail = mysqli_fetch_assoc($run_mail)) {
$name = $row_mail["user_name"];
$email = $row_mail["user_email"];
$mail->ClearAddresses();
$mail->addAddress('test#test.com', 'Test Name');
$mail->addBCC($email, $name);
$mail->isHTML(true); // Set email format to HTML
$bodyContent = "<p>Multiple Messages</p>";
$mail->Subject = $row_mail['user_name'].", New job is available " ;
$mail->Body = $bodyContent;
$mail->AltBody = $bodyContent;
if(!$mail->send()) {
echo "Message could not be sent.";
$error = '<div class="alert alert-danger"><strong>Mailer Error: '. $mail->ErrorInfo.'</strong></div>';
} else {
$error = '<div class="alert alert-success"><strong>Message has been sent</strong></div>';
}
}
}

Related

how do i send a user message to their mail from phpmailer?

Hi guys, please I'm having problem with my code. I want to accept user
email and verify it from my database then send the user an email using
PHPMailer but it's giving me error, I guess I did some bunch of error
coding here. please how do I go about it? this question might be silly
but I really need assistance from anyone, please. Thanks.
here is what I've tried out.
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$con = mysqli_connect("localhost","root","password007","voting");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$email = mysqli_real_escape_string($con,$_POST['email']);
$sql = "SELECT VotersID,Email,First_name FROM voters WHERE Email = '$email' ";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
$votersID = $row['VotersID'];
$first = $row['First_name'];
if($count == 1) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '******#gmail.com';
$mail->Password = '*******';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';
$mail->From = '*****#gmail.com';
$mail->FromName = 'West End University College';
$mail->addAddress($email, $first);
$mail->addReplyTo('*****#gmail.com', 'West End University College');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'code for login Confirmation';
$mail->Body = 'Hi, your code is:';
$mail->addAttachment($votersID);
}
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
else{
echo "Message sent successfully";
}
}
?>
Errors :
Notice: Undefined variable: mail in
C:\xampp\htdocs\voters\accConfirm.php on line 45 Fatal error: Call to
a member function send() on null in
C:\xampp\htdocs\voters\accConfirm.php on line 45

405 Not Allowed nginx phpmailer

I have just uploaded a page and find out the phpmailer isn't sending emails though it was sending in localhost. Getting 405 Not Allowed nginx. Any help? following is the code...
if (isset($_POST['msg_submit'])) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = $smtp_server_w;
$mail->Username = $username_w;
$mail->Password = $password_w;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->setFrom("test#gmail.com", "test");
$sql_mail = "SELECT * FROM order_details ORDER BY id DESC LIMIT 1";
$run_mail = mysqli_query($conn, $sql_mail);
while ($row_mail = mysqli_fetch_assoc($run_mail)) {
$mail->addAddress($row_mail['user_email'], $row_mail['user_name']);
$mail->isHTML(true);
$bodyContent = message;
$mail->Subject = test message ;
$mail->Body = $bodyContent;
$mail->AltBody = $bodyContent;
if(!$mail->send()) {
echo "Message could not be sent.";
$error = '<div class="alert alert-danger"><strong>Mailer Error: '. $mail->ErrorInfo.'</strong></div>';
} else {
$error = '<div class="alert alert-default"><strong>Message has been sent</strong></div>';
}
}
}

GMX contact form

I'm trying to create a simple SMTP-contact form with the help of phpMailer for my homepage but it's not working. It should forward the information of the fields "name, mail, subject, message" to my mailbox (in this case "name, mail, subject, message"). The input fields are also named "name, mail, subject, message" in my html code.
Can anyone tell me what I'm doing wrong? Thanks for any advice.
<?
require('class.phpmailer.php');
require('class.smtp.php');
$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->Host = 'mail.gmx.de';
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Mailer = "ssl";
$mail->Password = "****";
$mail->Username = "test#gmx.com";
$mail->SMTPAuth = "true";
$mail->FromName = $_POST['name'];
$mail->AddAddress = $_POST['mail'];
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];
if(!$mail->Send())
{
echo 'E-Mail not send.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo "<h5>" . 'Thanks for your message' . "</h5>";
}
?>
The error message says you are missing the recipient address, you have to add one using:
$mail->AddAddress("youemail#youremail.com", "Your Name");
//Your Name is optional. so alternatively you could do:
//$mail->AddAddress("youemail#youremail.com");
The part of the code you reference $mail->Username = "test#gmx.com"; simply sets the username for authenticating with the SMTP server, not the recipient address.

Remove previous message header when reply, using PHPMailer

I am using PHPMailer to create Contact form. It is working fine. But When I reply to received message, user is receiving my reply include with previous data.
owner_address#gmail.com = admin Email Address.
User Name = Email sender from Contact Form.
On Wed, Jul 30, 2014 at 1:35 AM, User Name<owner_address#gmail.com> wrote:
First Email Data
It is showing user name correctly, but it is showing my email address (not the user's email)
I want to remove email address or change it to user address.
Below is my code;
<?php
if (!isset($_POST['your_name']))
{
return false;
}
$name = $_POST['your_name'];
$email = $_POST['your_email'];
$subject = $_POST['your_subject'];
$msg = $_POST['description'];
include "PHPMailerAutoload.php"; // include the class name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "owner_address#gmail.com";
$mail->Password = "owner_password";
$mail->addReplyTo($email, $name);
$mail->SetFrom($email, $name);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->AddAddress("owner_address#gmail.com");
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "OK";
}
?>
Thank you,
Sameera Silva

How to retrieve more than one data in email?

I have a table named 'laptop' and a column inside the table named 'Lap_War_Expiry'. I need to send an email to user when the warranty of the laptop is going to expire soon. For your information, there is more than one warranty that will expired in a day. But the code below is only send the same data from table 'laptop'. Why is that happened and what I have to add in the code so that the email send will retrieve two or more data from database ?
This is my coding for sending the email :
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
// ======== get database data ==================
$link = mysql_connect("localhost","root","");
$database="master_inventory";
mysql_select_db ($database,$link) OR die ("Could not open $database" );
$query = 'SELECT Lap_PC_Name, Lap_War_Expiry FROM laptop'; //xyz is id of desired user
name.
$result1 = mysql_query($query);
while($row = mysql_fetch_array($result1)) {
$Lap_PC_Name = $row['Lap_PC_Name'];
$Lap_War_Expiry = $row['Lap_War_Expiry'];
}
$mail->Username = "email#gmail.com";
$mail->Password = "somepassword";
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true;
$mail->From = "email#gmail.com";
$mail->FromName = "AMS";
$mail->addAddress("emailaddress","AMS");
$mail->Subject = "Notification on warranty expiry";
$mail->Body = "Dear Madam,<br/><br />The licensed for the following PC will expired
in less than one month.<br /><br /> PC Name : ".$Lap_PC_Name. "<br />Date of expired :"
.$Lap_War_Expiry;
if(!$mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>
You need to move everything from below your while loop into it.
EDIT
Change
while($row = mysql_fetch_array($result1)) {
$Lap_PC_Name = $row['Lap_PC_Name'];
$Lap_War_Expiry = $row['Lap_War_Expiry'];
}
to
while($row = mysql_fetch_array($result1)) {
$Lap_PC_Name = $row['Lap_PC_Name'];
$Lap_War_Expiry = $row['Lap_War_Expiry'];
$mail->Username = "email#gmail.com";
$mail->Password = "somepassword";
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true;
$mail->From = "email#gmail.com";
$mail->FromName = "AMS";
$mail->addAddress("emailaddress","AMS");
$mail->Subject = "Notification on warranty expiry";
$mail->Body = "Dear Madam,<br/><br />The licensed for the following PC will expired
in less than one month.<br /><br /> PC Name : ".$Lap_PC_Name. "<br />Date of expired :"
.$Lap_War_Expiry;
if(!$mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
}

Categories