How to send multiple emails from database as CC with PHPMailer? - php

I have a database. The fields are id, email and city. I want to send some email to all emails in my database.
In e.g case: I live in a city, so I want to send an email to all emails matchting my city.
I have wrote my method code like this:
$kota = mysql_real_escape_string(stripcslashes( $_POST['kota']));
$baca_kota = mysql_query("SELECT email FROM conselor WHERE kota='$kota'");
while($read = mysql_fetch_array($baca_kota))
{
$emailcc = $read['email'];
}
require '../phpmailer/PHPMailerAutoload.php';
require '../phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'xxxxxxxxxxxxxxxxxx'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxxxxxxxxxxx'; // SMTP username
$mail->Password = 'xxxxxxxxxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($email,$name);
$mail->addAddress('stevanlai04#gmail.com'); // Add a recipient
$mail->addReplyTo($email);
$mail->addCC($emailcc);

Do $mail->addCC($emailcc); inside the loop:
while ($read = mysql_fetch_array($baca_kota)) {
$emailcc = $read['email'];
$mail->addCC($emailcc);
}

all you need to call Send() method from your PHPmailer sdk in your
while
$this->mail->IsSMTP();
$this->mail->SMTPAuth = true;
$this->mail->Host = 'mail.example.com';
//$this->mail->SMTPSecure = 'ssl';
$this->mail->Port =25;
$this->mail->Username = 'mail#example.com';
$this->mail->Password = 'your password';
foreach($listemails as $key=>val){
$this->mail->SetFrom($val["email address"], 'topic');
$this->mail->Send();
}
Edit: Also you can create a function that function send email and it returns itself! for example
function sendemail($emailaddres){
// call your phpmailler medhot
/ and return it
$this->mail->Send()
return sendemail($val["email address"])

Related

How to make "PHPmailer" work in a function

I don't know how to send a mail using PHPmailer in a function.
I want to do something similar to this - when I'll want to send a mail, I'll just call a function (like send::mail()). here is my code:
<?php
require '.\vendor\autoload.php';
//PHPMailer Object
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsSendmail();
//From email address and name
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->setFrom('my.bot.mail#gmail.com', 'Bot name');
$mail->Password = "########"; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;
//To address and name
$mail->addAddress("mymail#gmail.com"); //Recipient name is optional
//Address to which recipient will reply
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
$mail->Mailer = "sendmail";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
can someone help me put it into a function? when I tried, the "use" command didn't work.
First, you must determine the data.
$mailHost = '';
$mailSMTPAuth = '';
$mailUsername = '';
$mailPassword = '';
$mailSMTPSecure = '';
$mailPort = '';
$mailSenderAddress = '';
$mailSenderName = '';
Then you can generate a function that contains only the recipient, subject and message, and you can send the mail easily by pull the settings into this function:
function sendMail($sendMailAdress, $sendMailName, $sendMailSubject, $sendMailMessage) {
global $mailHost;
global $mailSMTPAuth;
global $mailUsername;
global $mailPassword;
global $mailSMTPSecure;
global $mailPort;
global $mailSenderAddress;
global $mailSenderName;
require_once('class/phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $mailHost; // Specify main and backup SMTP servers
$mail->SMTPAuth = $mailSMTPAuth; // Enable SMTP authentication
$mail->Username = $mailUsername; // SMTP username or mail
$mail->Password = $mailPassword; // SMTP password
$mail->SMTPSecure = $mailSMTPSecure; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $mailPort; // TCP port to connect to
$mail->CharSet = 'utf-8';
$mail->setFrom($mailSenderAddress, $mailSenderName);
$mail->addAddress($sendMailAdress, $sendMailName); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $sendMailSubject;
$mail->Body = $sendMailMessage;
if(!$mail->send()) {
return false;
} else {
return true;
}
}
The function will work like this:
sendMail('hello#test.com', 'Test User', 'Subject of test mail', 'And your mail message);

How to send eFax fax through PHP?

I am trying to send a fax via the eFax service. As I understand, this should be as easy as sending a simple email to the specific address.
I use PHPMailer for emails. Ordinary emails are sending fine. Even when I send to an efax address $mail->send returns true, that means that the email was sent seccessfully, but the recipient didn't receive the fax.
Any suggestions?
Thank you.
function sendFax($fax, $msg) {
$efax_number = $fax . "#efaxsend.com";
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = EMAIL_HOST; // Set mailer to use SMTP
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = EMAIL_SMTP_USERNAME;
$mail->Password = EMAIL_SMTP_PASSWORD; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
//From email address and name
$mail->From = "orders#orderapp.com";
$mail->FromName = "Test";
//To address and name
$mail->addAddress($efax_number); // SEND EMAIL TO USER
//Send HTML or Plain Text email
$mail->isHTML(false);
$mail->Subject = ' New Order Fax ';
$mail->Body = $msg;
$mail->AltBody = "Sample";
$result = $mail->send();
if (!$result)
{
...
}
else
{
...
}
}

Echo or Print after = in PHP

while ($apa = mysql_fetch_array($query)){
$username = print($apa['username']);
$pass = print(md5(md5($apa['password'])));
require '../phpmailer/PHPMailerAutoload.php';
require '../phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.royaleducation.web.id'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxx'; // SMTP username
$mail->Password = 'xxxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($email,$name);
$mail->addAddress($email); // Add a recipient
$mail->addReplyTo($email);
$mail->isHTML(true); // Set email format to HTML>
$mail->msgHTML(
"<center><h1>Your data</h1></center><br><br>
username =".$username."<br>
password =".$password
);
the $username and $password won't appear..
am i wrong to put print after $username and $password??
or there is another way to print out the value in phpmailer?? because if i type or echo inside $mail->msgHTML , its getting error
If you use print function, parameters show on output and print always return 1, so you can't use for assign values.
If you have problem $mail->msgHTML function you can use $mail->Body as below:
while ($apa = mysql_fetch_array($query)){
$username = $apa['username'];
$pass = md5(md5($apa['password']));
require '../phpmailer/PHPMailerAutoload.php';
require '../phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.royaleducation.web.id'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'idxxxx'; // SMTP username
$mail->Password = 'xxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($email,$name);
$mail->addAddress($email); // Add a recipient
$mail->addReplyTo($email);
$mail->isHTML(true); // Set email format to HTML>
$mail->Body ="<center><h1>Your data</h1></center><br><br>username =".$username."<br>password =".$password;

PHPMailer - make the sender mail variable

I'm using php mailer and i cant specify the sender mail , i want to be a variable every one type his email to be here , but it cant be done i must type my email and my password , so anyone know how it can be done
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "usermail"; // GMAIL username
$mail->Password = ""; // GMAIL password
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->addReply=$_POST['email'] ;
$mail->addAddress=$_POST['email'];
$mail->Subject=$_POST['subject'];
$mail->Body=$_POST['message'] .$_POST['email'];
$mail->Sender=$_POST['email'];
try this code
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
#$mail->Send()
Go through this link -> Own-Email-System-using-SMTP-and-PHPMailer
OR,
Please elaborate your issues so that i can help you properly .
NOTE:If "Less secure app access" is turned off for your Gmail account, you have to turn it on. => Click Here To see . Is it ON or OFF
<?php
require_once 'vendor/autoload.php';
$mail = new PHPMailer\PHPMailer\PHPMailer;
//Enable SMTP debug mode
$mail->SMTPDebug = 0;
//set PHPMailer to use SMTP
$mail->isSMTP();
//set host name
$mail->Host = "smtp.gmail.com";
// set this true if SMTP host requires authentication to send mail
$mail->SMTPAuth = true;
//Provide username & password
$mail->Username = "YOUR_GMAIL_EMAIL_ID";
$mail->Password = "YOUR_GMAIL_PASSWORD";
$mail->SMTPSecure = "tls";
$mail->Port = 587;// Enter port number
$mail->ClearReplyTos();
$mail->addReplyTo("YOUR_GMAIL_EMAIL_ID", $_POST['name']); //$_POST['name'] => YOUR_GMAIL_NAME . You Can directly give "YOUR_GMAIL_NAME"
$mail->SetFrom("YOUR_GMAIL_EMAIL_ID", $_POST['name']);
$mail->addAddress($_POST["email"]); //TO WHOM U R SENDING MAIL
//Subject
$mail->isHTML(true);
$mail->Subject =" ".$_POST['subject']." ";
$body = "<html>
<head> </head>
<body>
".$_POST['message']." <br>
</body>
</html>";
$mail->MsgHTML($body);
if(!$mail->send()) {
$error_message = "Mailer Error : ". $mail->ErrorInfo;
echo $error_message;
} else {
echo "Email Sent Successfully";
}
?>

class.phpmailer.php returns SMTP connect() failed error in 000webhost.com

I use class.phpmailer.php to send a confirmation email. It works perfectly in my local server, but when I upload it to the server in 000webhost.com it no longer works.
I just discovered mail() is not working anymore either.
Is there something I can do to solve this?
Here is the code of the function I use to send every mail in my system:
code
function correoEnviar($asunto, $mensaje, $mensajeTexto, $email)
{
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mail.yahoo.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxx#yahoo.com.mx'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'xxxx#yahoo.com.mx';
$mail->FromName = 'xxxx';
$mail->addAddress($email); // Name is optional
$mail->addReplyTo('xxxx#yahoo.com.mx', 'Information');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
//$mail->Subject = 'Por favor confirme su correo para obtener el libro El fractal y el =?UTF-8?Q?dise=C3=B1o=C2=A0gr=C3=A1fico?=';
$mail->Subject = $asunto;
$mail->Body = $mensaje;
$mail->AltBody = $mensajeTexto;
if(!$mail->send()) {
return $mail->ErrorInfo;
}
else
return 1;
}
echo correoEnviar($asunto, $mensaje, $mensajeTexto, "recipent#gmail.com");
?>
code
Are you sure you are including the file class.phpmailer.php, your code seems fine.
Paste the server's mailserver log, it will provide a better insight.
Your code is fine, it might be a server issue.
A sample code showing SMTP settings
require ("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn of SMTP authentication
$mail->Username = "YAHOO ACCOUNT"; // SMTP username
$mail->Password = "YAHOO ACCOUNT PASSWORD"; // SMTP password
$mail->SMTPSecure = "ssl";
$mail->Host = "YAHOO HOST"; // SMTP host
$mail->Port = 465;

Categories