Currently I am using the following script. This script will send an email to email1, but not always.
When somebody adds the second email2, the email only goes to number 2.
I want to change the script to sent to both emails (1 and 2) when email2 exists.
Tried a lot of things, but can't get it right.
// Email
if(!$row->email2){
$email = $row->email1;
} else{
$email = $row->email2;
// Tried this below and a lot more
if(!$row->email2){
$email = $row->email1;
} else{
$email = $row->email1;
$email = $row->email2;
// E-mail to
if(!is_array($email)){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
} else{
foreach($email as $email){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
$emails = array();
if($email = $row->email1) {
$emails[] = $email;
}
if($email = $row->email2) {
$emails[] = $email;
}
foreach($email as $email){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
}
Related
I'd like to use this send.php script for a contact form. Right now when an email is sent, a user inputs their name, email, and the email of the person to send the email to.
The email is sent from #Email_Placeholder and both #Email_Placeholder, $email, and #email_2 gets a copy of the email.
I'd like to add the option for an addReplyTo and addCC so that both $email and $email_2 receives an email when either of the 3 parties makes a reply to the original email thread.
What is the correct syntax so that $email, and $email_2 are added to the addReplyTo and addCC parameters so that they receive every email in the original thread?
//Lines to edit. Goal add $email and $email_2 to addReplyTo and addCC
$mail->addReplyTo('#Email_Placeholder', 'Information');
$mail->addCC('#Email_Placeholder'); //should be changed to the name of the app owner's email
Send.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); //sanitize data
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$email_2 = filter_var($_POST['email_2'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
/*added line of code*/
if(empty($name)){
header("location: index.php?nouser");
exit();
}
if(empty($email)){
header("location: index.php?noemail");
exit();
}
if(empty($message)){
header("location: index.php?noemail");
exit();
}
/*if(empty($message)){
header("location: index.php?nomessage");
exit();
}//end validation*/
//added line; 'honeypot'
if(empty($_POST['phone2'])){
//Information that needs to be filled out to be able to send an email through phpmailer
//Will use an SMTP service. SMTP is basically like a mail server to send mail
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 1;
//$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = false;
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = "25";
$mail->Username = "#Email_Placeholder";
$mail->Password = "Password";
$mail->SetFrom("#Email_Placeholder");
$mail->setFrom('#Email_Placeholder', 'Greeting');
// Add a recipient : used to also send to name
//This sends an email to both $email and $email_2
$mail->addAddress($email);
$mail->addAddress($email_2);
//This makes it so that #Email_Placeholder receives a copy of the emails
$mail->addReplyTo('#Email_Placeholder', 'Information');
$mail->addCC('email#gmail.com');
/*$mail->addBCC('bcc#example.com');*/
//Body content
$body = "<p><strong>Hello, </strong>" . $email . " How are you?
<br>
<b> Please Notice: </b><br>
<br> <br> Your message was Delivered: " . $message . ". Hello" . $name2 .
"</p>";
$mail->isHTML(true);
//subject line of email
$mail->Subject = $name;
//for html email
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
//the email gets sent
header("Location: http://www.test-domain.com");
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>
I am trying to send email to All users from Mysql Database. For this I have written below Code, but it is sending email to First record only. What's wrong I am doing. I need to send email to all users.
if ($_POST['do'] == 'mail') {
$result = $db->query("SELECT email FROM members WHERE status='Active'");
$input="This is a text message";
$userdetails = $db->fetch_array($result);
$emails = implode(",", $userdetails);
$message = $input;
$mail = new mail();
$mail->setFrom($settings['email_support'], $input->pc['name']);
$mail->addTo($emails);
$mail->setSubject('subject text!');
$mail->setBodyText($message);
$mail->send();
}
It seems your code perfect. It can be possible you are fetching only 1 row. If not, then try this it will help you.
if ($_POST['do'] == 'mail') {
$result = $db->query("SELECT email FROM members WHERE status='Active'");
$input = "This is a text message";
foreach ($result as $row) {
$message = $input;
$mail = new mail();
$mail->setFrom($settings['email_support'], $input->pc['name']);
$mail->addTo($row['email']);
$mail->setSubject('subject text!');
$mail->setBodyText($message);
$mail->send();
}
}
Have tried send email to multiple recipients but only 1 data is sent:
function reminder(){
$recipients= $this->user_model->view();
var_dump($recipients[0]->email);
$emaill = $recipients->email;
$recipientsmail= $emaill.',';
$email = $recipientsmail;
$judul = 'Test Email';
$deskripsi = 'TESt Email';
$config = [...]; //config for email is OK
$this->load->library('email', $config);
$this->email->from('tes');
$this->email->to($email);
$this->email->subject($judul);
$this->email->message($deskripsi);
$this->email->send();
return TRUE;
}
is something wrong in my code?
Please Help Me
That's the way I use to send multiple emails in codeigniter. Instead of put all the email directions in a variable ($email), use a foreach to loop the array and follow the details in the code:
function reminder(){
$recipients= $this->user_model->view();
var_dump($recipients[0]->email);
$judul = 'Test Email';
$deskripsi = 'This is a test';
$emailuser = 'user123#gmial.com';//for example
$nameuser = 'name of the user';
$config = [...]; //config for email is OK
$this->load->library("email");
foreach ($recipients as $value) {
$this->email->initialize($config);
$this->email->from($emailuser, $nameuser);
$this->email->to($value->email);
$this->email->subject($judul);
$this->email->message($deskripsi);
if($this->email->send()){
$this->session->set_flashdata("email_sent","Email sent successfully.");
}else{
$this->session->set_flashdata("email_sent","Error in sending Email.");
}
}
return TRUE;
}
And with this you could send more than one email. I hope it helps you.
I've been trying many different ways to prevent PHP Mailer from appending every email address coming from my DB to every single email.
I feel it doesn't look right for every user to receive an email from my website and being able to see a few other thousand emails appended to that same email as well.
Here's my code, I already tried to many diferent things:
$query_select = "SELECT * FROM users";
$query = mysqli_query($connection, $query_select);
if($select_to == 'all'){
while($row = mysqli_fetch_assoc($query)) {
$user_email = $row['user_email'];
$send_to = $user_email;
$mail->setFrom('example#gmail.com', 'My website');
$mail->addAddress($send_to);
$mail->isHTML(true);
$mail->Subject = $subject_email;
$mail->Body = $content_email;
$mail->AltBody = $content_email;
}
} else {
if($select_to == 'single'){
$send_to = $to_single_email;
}
}
if($mail->send()){
$confirm = 'Sent.';
$send = array(
'confirm'=>$confirm
);
echo json_encode($send);
} else {
$confirm = 'There was en error while sending this email.';
$send = array(
'confirm'=>$confirm
);
echo json_encode($send);
}
$mail->send() will send to all addresses via a single message. Instead you need to send one message per recipient.
The while() loop would be refactored similar to:
while($row = mysqli_fetch_assoc($query)) {
$user_email = $row['user_email'];
$send_to = $user_email;
$mail->setFrom('example#gmail.com', 'My website');
$mail->addAddress($send_to);
$mail->isHTML(true);
$mail->Subject = $subject_email;
$mail->Body = $content_email;
$mail->AltBody = $content_email;
$mail->send();
$mail->clearAllRecipients();
}
The call to clearAllRecipients() will clear out the previous addresses that would otherwise accumulate via addAddress()
You'll also need to add the error checking within the loop for each call of send()
I'm trying to send an email to multiple recipients using phpmailer and getting the emails list from my table in the database, however, whenever I make a request only one recipient, the first one on the list gets the email. *I know this might be marked as duplicate but I couldn't find a similar example using mysql
<?php
if (isset($_POST['submit'])) {
if (empty($errors)) {
$thisType = mysql_prep($_POST["partner_type"]);
$content = $_POST["content"];
$header = $_POST["header"];
$query = "SELECT * FROM partners_list WHERE partner_type = '$thisType' ";
$result = mysqli_query($connection, $query);
$count = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result)){
require_once("phpMailer/class.phpmailer.php");
require_once("phpMailer/class.smtp.php");
require_once("phpMailer/language/phpmailer.lang-uk.php");
$to_name = "Order";
$subject = $header;
$message = $content;
$message = wordwrap($message,70);
$from_name = "thisCompany";
$from = "noreply#thicCompany.com";
$mail = new PHPMailer();
$mail->AddAddress($row['email'], "Test Message"); // add each DB entry to list of recipients
$mail->FromName = $from_name;
$mail->From = $from;
$mail->Subject = $subject;
$mail->Body = $message;
$result = $mail->Send();
// Success
$_SESSION["message"] = "e-mail successfully sent.";
redirect_to("successpage.php");
}
}
} else {
}
?>