I'm trying to send email from a database. I get email from the databse and send message to email I get from database.
My code is working good, but I have problem the sending mail - it is only sent for the first one.
include("config.php");
include ("library.php");
include ("classes/class.phpmailer.php");
$sql = "select * from Table ";
$result = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($result)) {
if ($row['Date_Registry'] == date("Y-m-d") ){
$sql= "select * from write_message where ID='$m'";
$result = mysql_query($sql, $conn);
$row = mysql_fetch_array($result);
$email= $row['EMAIL'];
$masg = $row['Write_message'];
$Message_name = $row['Message_name'];
$email = $email;
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->Host = SMTP_HOST; //Hostname of the mail server
$mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPAuth = true; //Whether to use SMTP authentication
$mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
$mail->Password = SMTP_PWORD; //Password for SMTP authentication
$mail->SetFrom(SMTP_UNAME, "Helli"); //From address of the mail
// put your while loop here like below,
$mail->Subject = $Message_name; //Subject od your mail
$mail->AddAddress($email); //To address who will receive this email
}
}
Just add the function clearAddress above the AddAddress function (as below) and it will work.
include("config.php");
include ("library.php");
include ("classes/class.phpmailer.php");
$sql = "select * from Table ";
$result = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($result)) {
if ($row['Date_Registry'] == date("Y-m-d") ){
$sql= "select * from write_message where ID='$m'";
$result = mysql_query($sql, $conn);
$row = mysql_fetch_array($result);
$email= $row['EMAIL'];
$masg = $row['Write_message'];
$Message_name = $row['Message_name'];
$email = $email;
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->Host = SMTP_HOST; //Hostname of the mail server
$mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPAuth = true; //Whether to use SMTP authentication
$mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
$mail->Password = SMTP_PWORD; //Password for SMTP authentication
$mail->SetFrom(SMTP_UNAME, "Helli"); //From address of the mail
// put your while loop here like below,
$mail->Subject = $Message_name; //Subject od your mail
$this->phpMailerObj->clearAddresses(); //Clear Addresses
$mail->AddAddress($email); //To address who will receive this email
$this->phpMailerObj->Body = "body";
$this->phpMailerObj->msgHTML("Message");
try {
$this->phpMailerObj->Send();
return;
} catch (Exception $exc) {
return $error['error'] = "Email cound not be sent";
}
}
}
Related
Here's The PHP Code
I'm able to send the mail. If I comment out the $_FILES['attachment']['name'];
$conn = new mysqli($servername, $username, $password, $dbname);
$category = $_POST['category'];
$attachment = $_FILES['attachment']['name'];
$query = "select Name, email, status, category from mailer where status='subscribed' and category='$category'";
$result = $conn->query($query);
while($row = $result->fetch_assoc()) {
$name = $row['Name'];
$email = $row['email'];
$status = $row['status'];
$category1 = $row['category'];
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//send code
//Recipients
$mail->addAddress($email);
$mail->AddAttachment($attachment);
Change $attachment = $_FILES['attachment']['name']; to $attachment = $_FILES['attachment']['tmp_name']; as name refers to the original file name and tmp_name to the (temporary) file path on the server. And you need to specify the file on the server in AddAttachment() call.
$message = 'This for sending an attachment by using php mailer';
$Sender = 'youremail#xyz.com';
$subject = "Attachment Test";
$Recipiant = 'recipient#xyz.com';
$subject = $subject;
$host= "yourdomain.com"; // sets GMAIL as the SMTP server
$port=465; // set the SMTP port for the GMAIL server
$username="info#yourdomain.com"; // GMAIL username
$pwd="domain mail password";
include 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsMail();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = $host; // sets GMAIL as the SMTP server
$mail->Port = $port; // set the SMTP port for the GMAIL server
$mail->Username = $username; // GMAIL username
$mail->Password = $pwd; // GMAIL password
$mail->AddReplyTo($Sender,"");
$mail->From = $Sender;
$mail->FromName = $SenderName;
$mail->Sender = $Sender;
$mail->Subject = $subject;
$FileName = 'PRO1158.pdf';
$mail->Body = "<br>".$message."<br>";
if($FileName!=""){
$mail->AddAttachment("Invoices/".$FileName); // attach files
}
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 50; // set word wrap
//$mail->MsgHTML($body);
$mail->AddAddress("$Recipiant","");
$mail->IsHTML(true); // send as HTML
$mail->Send();
$mail->ClearAddresses();
First you need to upload the file into a folder then place the file path in
$mail->AddAttachment();
exapmle :
$attachment="D:\uploads\img.jpg"
$mail->AddAttachment($attachment);
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
{
...
}
}
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>';
}
}
}
i have a problem with my PHPmailer function.
I want my registration form to send a mail to every new registered member.
I get this error code. SMTP Error: Could not authenticate.
I have contacted my webhost, and they say that i have the right username,password, smtp server etc.
include "class.smtp.php";
include "class.phpmailer.php";
$Host = "mail.smtp.com"; // SMTP servers
$Username = "support#mymail.com"; // SMTP password
$Password = "mymailpassword";
$From = "support#mymail.com";
$FromName = "My name";
$ToEmail = $mail;
$ToName = $username;
$header = str_replace('%homepage_url%', $hp_url, $_language->module['mail_subject']);
$Message = str_replace(Array('%nickname%', '%username%', '%password%', '%activationlink%', '%pagetitle%', '%homepage_url%'), Array(stripslashes($nickname), stripslashes($username), stripslashes($pwd1), stripslashes($validatelink), $hp_title, $hp_url), $_language->module['mail_text']);
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = $Host;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $Username;
$mail->Password = $Password;
$mail->From = $From;
$mail->FromName = $FromName;
$mail->AddAddress($ToEmail , $ToName);
$mail->WordWrap = 50; // set word wrap
$mail->Priority = 1;
$mail->IsHTML(true);
$mail->Subject = $header;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($Message);
$mail->CharSet="UTF-8";
if(!$mail->Send()){
redirect("index.php",$_language->module['register_successful'],10);
$show = false;
} else {
redirect("index.php",$_language->module['register_successful'],10);
$show = false;
}
I'm building a registration form using MySQL to store data and work perfectly. when users register will be sent a verification email to activate user accounts. the problem is not the verification email sent to the email users but user data and the activation code can get into mysql.
Here code for insert.php
<?php
//panggil file config.php untuk menghubung ke server
include('config.php');
//tangkap data dari form
$nama = $_POST['nama'];
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$alamat = $_POST['alamat'];
$telp = $_POST['telp'];
$password = mysql_real_escape_string($_POST['password']);
// regular expression for email check
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
if(preg_match($regex, $email))
{
$password=md5($password); // encrypted password
$activation=md5($email.time()); // encrypted email+timestamp
$count=mysql_query("SELECT id_user FROM user WHERE email='$email'");
// email check
if(mysql_num_rows($count) < 1)
{
$query = mysql_query("insert into user (nama,email,telp,password,alamat,username,activation) VALUES('$nama', '$email', '$telp', '$password', '$alamat', '$username', '$activation')") or die(mysql_error());
if ($query)
{
header('location:index.php');
}
// sending email
include ('sendEmail.php');
$to=$email;
$subject="Email verification";
$body='Hi, <br/> <br/> Silakan verifikasi dengan klik link di bawah ini. <br/> <br/> Verifikasi';
sendEmail($to,$subject,$body);
$msg= "Registration successful, please activate email.";
}
else
{
$msg= 'The email is already taken, please try new.';
}
}
else
{
$msg = 'The email you have entered is invalid, please try again.';
}
// HTML Part
//}
?>
and here the code sendEmail.php
<?php
function sendEmail($to,$subject,$body)
{
require ('class.phpmailer.php');
$from = "me#kumistebal.web.id";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "ssl://smtp.gmail.com"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "*********#gmail.com"; // SMTP username
$mail->Password = "*********"; // SMTP password
$mail->SetFrom($from, 'From Name');
$mail->AddReplyTo($from,'From Name');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($email, $to);
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
}
?>
Does anyone have any idea how to do?
Instead of $mail->Host = "ssl://smtp.gmail.com"; // SMTP host
just use
$mail->Host = "smtp.gmail.com"; // SMTP host
You need to change host with $mail->Host = "smtp.gmail.com"; // SMTP host and $mail->Port =25; // set the SMTP port