Sending multiple email from same page phpmailer - php

i am trying to send email to user and to admin at the same time using phpmailer but only one email is sent.. either to user or to admin..
i have tried every possible way with no luck. please help
below is my code.
$html.="This is my message to user";
$bcc = "xxx#gmail.com";
$to= $clientemail;
$subject = "This is my subject;
$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "$host"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
try {
$mail->From = $from;
$mail->FromName = $Name;
$mail->AddAddress(''.$to.'');
$mail->AddBCC('xxx#gmail.com');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = $html;
$mail->Send();
// echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
$message="Thank you for your request.";
$html="this is my second message to admin";
$bcc = "xxx#gmail.com";
$to= $admin;
$subject = "message to admin;
$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;
$mail2 = new PHPMailer();
$mail2->IsSMTP();
$mail2->Host = "$host"; // specify main and backup server
$mail2->SMTPAuth = true; // turn on SMTP authentication
$mail2->Username = $username; // SMTP username
$mail2->Password = $password; // SMTP password
$mail2->Port = 587;
$mail2->SMTPSecure = 'tls';
try {
$mail2->From = $from;
$mail2->FromName = $from;
$mail2->AddAddress(''.$to.'');
$mail2->AddBCC('xxx#gmail.com');
$mail2->WordWrap = 50;
$mail2->IsHTML(true);
$mail2->Subject = $subject;
$mail2->Body = $html;
$mail2->AltBody = $html;
$mail2->Send();
// echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}

I'm not sure if you want to send the same email or not to differents persons, but the first improvement to do is to factorize your code.
// here an example of a function to send the SAME email to differents persons
// $emails is an array
function sendMail($emails, $subjects, $msg) {
$mail = new PHPMailer();
$mail->IsSMTP();
try {
$mail->Host = "$host"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->From = $from;
$mail->FromName = $Name;
foreach ($emails as $to) {
$mail->AddAddress($to);
}
$mail->AddBCC('xxx#gmail.com');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = $html;
if(!$mail->Send()) {
// log if needed
}
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
}
now you can use this function with differents messages or subjects... whatever you want!
EDIT :
to send different message to users and admins :
//settings for admin
$emails = array('admin#mail.com', 'admin2#mail.com'); // or just array('admin#mail.com');
$subjects = 'my sub';
$msg = 'my msg';
sendMail($emails, $subjects, $msg);
//settings for users
$emails = array('user#mail.com');
$subjects = 'different or not sub';
$msg = 'different or not msg';
sendMail($emails, $subjects, $msg);

Related

Mail configuration error in amazon hosting using php?

I designed my website and hosted on my amazon ec2 instance and I bought my domain in godaddy (www.mydomain.com).Now I want a mail configuration in my contact form page in website.. Below its my code , I don't know where am I mistake the code?
<?php
if(isset($_REQUEST['submit']))
{
try
{
$name = $_POST['name'];
echo "<script type='text/javascript'>alert('$name')
</script>";
$email = $_POST['email'];
echo "<script type='text/javascript'>alert('$email')
</script>";
$subject = $_POST['subject'];
echo "<script type='text/javascript'>alert('$subject')
</script>";
$message = $_POST['message'];
echo "<script type='text/javascript'>alert('$message')
</script>";
$response ="";
$body = <<<EOD
<div style='font-size:18px'>
<b> Name </b> : $name <br />
<b> Email address </b> : $email <br />
<b>Message </b> : $message <br />
</div>
EOD;
$to = "XXXXX#gmail.com";
require_once($_SERVER['DOCUMENT_ROOT'].'/samplemail/lib/class.phpmailer.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/samplemail/lib/class.smtp.php');
$mail = new PHPMailer(true);
//$mail->Host = "relay-hosting.secureserver.net"; // your SMTP Server
// echo $res;
$mail->IsSMTP();
$mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->SMTPDebug=true;
$mail->SMTPAuth = true; // Auth Type
$mail->Port = 25;
$mail->IsSendmail();
//$mail->SMTPSecure = "ssl";
$mail->Username = "support#mydomain.com";
$mail->Password = "******";
$mail->Sender = "supportexample#mydomain.com";
$mail->From = "supportexample#mydomain.com";
$mail->AddReplyTo($email);
$mail->FromName = "Example";
$mail->AddAddress($to);
//$mail->AddAddress("desired recipient no.2 optional");
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body=$body;
$mail->WordWrap = 50;
$mail->Send();
echo "<script type='text/javascript'>alert('Mail Send Successfully')
</script>";
}
catch (phpmailerException $e) {
echo "<script type='text/javascript'>alert('Failed')
</script>";
echo $e->errorMessage();
}
}
?>
It gives an error
Could not execute: /var/qmail/bin/sendmail
Try This One. This Might Helpful. You Have To Use Different Email For From Mail.
Instead Of Using
$mail->From = "support#mydomain.com"
You Have To Use Another Email Here :
$mail->From = "supportexample#mydomain.com";
require_once('phpmailer/class.phpmailer.php');
require_once('phpmailer/class.smtp.php');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "email-smtp.us-east-1.amazonaws.com"; // SMTP HOST
$mail->SMTPAuth = true;
$mail->Username = "support#mydomain.com"; // SMTP username
$mail->Password = "********"; // SMTP password
$mail->SMTPSecure = "ssl"; // Enable TLS encryption, `ssl` also accepted
$mail->Port = "465"; // TCP port to connect to 587
$mail->From = "supportexample#mydomain.com";
$mail->FromName = "Domain Example";
$mail->addAddress("XXXX#gmail.com");
$mail->addReplyTo("supportexample#mydomain.com");
$mail->isHTML(true);
$mail->Subject = "Hello Test";
$mail->Body = "Test Message Working With Us";
if($mail->send()){
return true;
}
else{
return $mail->ErrorInfo;
}

PHPMailer: Attempt to assign property of non-object

I'm frustrated to fix this.
One time I made it work. The next day, an error annoys me:
Warning: Attempt to assign property of non-object in C:\XAMPP\htdocs\HAF\includes\sendmail.php on line 356
Help me fix this :(
Here's my code:
sendmail.php
class SendMail {
function notification($recipient, $name, $subject, $message) {
global $email;
$email->Host = "smtp.gmail.com";
$email->SMTPAuth = true;
$email->Username = "******#gmail.com";
$email->Password = "**********";
$email->SMTPSecure = "tls";
$email->Port = 465;
$email->setFrom('admin#gmail.com', 'My WebApp');
$email->addAddress($recipient);
$email->isHTML(true);
$email->Subject = $subject;
$email->Body = $message;
if(!$email->send()) {
return false;
} else {
return true;
}
}
}
index.php
$email = "jaydenjames#gmail.com";
$name = "Jayden James";
$message = "Welcome {$name}!";
$SendMail->notification($email, $name, 'Welcome guest!', $message);
update answer with gmail configuration....
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'MyUsername#gmail.com';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;
$mail->From = 'MyUsername#gmail.com';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('anotherValidGmail#gmail.com');
$mail->AddReplyTo('phoenixd110#gmail.com', 'Information');
$mail->IsHTML(true);
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = "Hello";
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}

Sendmail sending to multiple addresses but in seperate emails

I'm trying to send multiple separate emails to multiple addresses. The below code sends it in 1 email with multiple TO addresses. This becomes an issue because everyone in the email can see each other's email addresses.
Is there a way to send separate emails?
<?php
$smtp = 'xxx.com';
$port = 25;
$secure = 'tls';
$username = 'test#xxx.com';
$pass = '';
$from = 'test#xxx.com';
$to = 'info#xxx.com';
$to1 = '';
$subject = 'Test Email';
$content = $mail_content;
require_once("include/class.phpmailer.php");
$mail=new PHPMailer(true);
$mail->IsSMTP();
try{
$mail->Host = $smtp;
$mail->SMTPAuth = true;
$mail->Port = $port;
$mail->SMTPSecure = $secure;
$mail->Username = $username;
$mail->Password = $pass;
$mail->SetFrom($from);
if (isset($email) && $email) {
$mail->AddAddress($email);
}
else {
while($row = mysqli_fetch_object($result)) {
$mail->AddAddress($row->email);
echo $row->email."<br>";
}
}
$mail->Subject = $subject;
$mail->MsgHTML($content);
$mail->Send();
if (isset($email) && $email) {
?>
<script>location.href="<?php echo '../index.php' . $_REQUEST['redirect']; ?>";</script>
<?php
}
}
catch (phpmailerException $e){
echo $e->errorMessage();
}
catch (Exception $e){
echo $e->getMessage();
}
?>
Send the email in the BCC.
Look here for an example :
PHP Email sending BCC
You have a loop that checks for all of the email addresses and then assigns the email address to $mail->AddAddress().
All you need to do in this same loop is send the email with $mail->Send(); with each iteration of the loop as well
//PHPmailer object
$mail=new PHPMailer(true);
//set up
$mail->IsSMTP();
$mail->Host = $smtp;
$mail->SMTPAuth = true;
$mail->Port = $port;
$mail->SMTPSecure = $secure;
$mail->Username = $username;
$mail->Password = $pass;
$mail->SetFrom($from);
$mail->Subject = $subject;
$mail->MsgHTML($content);
//add address
if (isset($email) && $email) {
$mail->AddAddress($email);
}
else {
//loop through the DB results
while($row = mysqli_fetch_object($result)) {
//you already have this
$mail->AddAddress($row->email);
echo $row->email."<br>";
//now send it here as well
$mail->Send();
//Do more stuff here
}
}
I have not tested this on my local system but this is one way to send the mail individually for each address as an alternative to sending one mail and using bcc.

PHPMailer not working consistently

I have a web app using PHP, MySQL, and PHPMailer to send emails. The issue I am having is PHPMailer will send some of the emails, but not always. I'm thinking maybe it is because different list of emails have data issues? Below is my code that loops through the email addresses and send some, but not all the emails. I have tried to catch errors, but I can't see any in the logs.
How can I debug what is going on, and why some don't send?
else if($current_version == 'PROD'){
date_default_timezone_set('America/New_York');
require 'PHPMailer-master/PHPMailerAutoload.php';
$pageURL = 'xxxxxx';
$subject = "xxxxx ";
$body = "xxxxxx \r\n";
$body .= "xxxxx \r\n \r\n";
$body_html = str_replace("\r\n","<br/>",$body);
try {
$mail = new PHPMailer();
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "xxx#xxxx.com";
$mail->Password = "xxxx";
$mail->setFrom('xxxxx#xxxx.com', 'xxxx');
$mail->addReplyTo('xxxx#xxxx.com', 'xxxx');
$mail->Subject = $subject;
$mail->Body = $body_html;
$mail->AltBody = $body;
$sql_email_list = "SELECT email FROM tbl_email
WHERE distro_name='xxxxs'
AND is_active='Y' ";
$result_email_list = mysql_query($sql_email_list);
$num_email_list = mysql_num_rows($result_email_list);
$lost_email_list = mysql_result($result_email_list,0,'email');
$lost_email_array = explode(";",$lost_email_list);
foreach ($lost_email_array as $key => $val){
$mail->AddAddress($val);
if($carrier_email_flag_global == 'ON'){
$mail->send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
echo '<hr>Email Sent to: '.$val;
}
}
$sql_email_one = "SELECT table2.email
FROM table1
INNER JOIN table2 ON table1.cntct = table2.cntct_id
WHERE id='$id' ";
$result_email_one = mysql_query($sql_email_one);
$num_email_one = mysql_num_rows($result_email_one);
for($iiz=0;$iiz<$num_email_one;$iiz++){
$one_email = mysql_result($result_email_one,$iiz,'email');
$mail->AddAddress($one_email);
if($carrier_email_flag_global == 'ON'){
$mail->send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
echo '<hr>Email Sent to: '.$val;
}
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
echo '<hr>ERROR 001';
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
echo '<hr>ERROR 002';
}
}

PHPMailer - AUTH not accepted

I'm trying to run PHPMailer for an internal contact form and I am getting the error ERROR: AUTH not accepted from server. Here is my current code..
require_once('/includes/class.phpmailer.php');
include("/includes/class.smtp.php");
if (isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
try {
$mail->Host = "192.168.6.6";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = True;
$mail->Username = 'xxxxx';
$mail->Password = '***';
$mail->Port = 25;
$mail->AddAddress('xxx#xxx.com', 'xxxxx');
$mail->SetFrom($email);
$mail->Subject = 'New message from Contact Form';
$mail->Body = $message;
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
};
This error basically means that your attempt to authenticate was rejected by the remote server. Different PHPMailer settings (well SMTP settings) are required by different remote mail servers.
This could be caused by
Using the wrong port
Using the wrong host
Incorrect user/pass
Incorrect SMTPSecure
Example SMTP setup:
Gmail: use of phpmailer class
Hotmail: phpmailer with hotmail?
If you are using this internally, you may not need to use SMTP authentication at all, depending on your server settings. Try this and see if it works:
require_once('/includes/class.phpmailer.php');
include("/includes/class.smtp.php");
if (isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
try {
$mail->Host = "192.168.6.6";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = False;
$mail->Port = 25;
$mail->AddAddress('xxx#xxx.com', 'xxxxx');
$mail->SetFrom($email);
$mail->Subject = 'New message from Contact Form';
$mail->Body = $message;
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
};

Categories