I have a php script which requires phpmailer and I got it to work already but I need to send the email to 3000 persons. How do I do that? I've tried searching over the net but all the examples show only to enumerate the emails addresses. Mine comes from a database with queries. I have the following code :

$mail             = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->SMTPAuth      = true;
$mail->SMTPKeepAlive = true;
$mail->Port          = 465;  
$mail->Username      = "my@gmail.com";
$mail->Password      = "password";
$mail->SetFrom('my@email.com', 'First Last');
$to = "my@gmail.com";
$mail->AddAddress('$to',"First Last");
$mail->Subject = "Test Mail";
$message = "messages go here";
$mail->MsgHTML($message);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "An e-mail was sent to $to with the subject: $subject";;}

Any suggestion is appreciated. Thanks!

Recommended Answers

All 9 Replies

So what exactly are you looking for? If it is looping query results, then there are examples in the code snippets.

which code snippets? I haven't found sources wherein they use query results from the database and loop it. Can you help?

EDIT: I meant I wanted to know how I could email to multiple BCC's.

I made some research and some say that using phpmailer moves the sent mail to the spam folder. Can you give me advice on this?

It's possible, but then again: I'm sure if you send me a regular email there's a good change that will land in my spam too. There are ways to add headers to provide more trustworthy information in the email, so it won't get flagged as easy. But there's always the change it gets flagged. The people who blurt out such statements rarely back it up IMHO.

I'm trying to learn swiftmailer but the problem is it should have arrays for the to and from addresses. I use dreamweaver so when I make queries, it generates its own codes so I'm not sure how I can code $address => $name and loop codes on my own if dreamweaver generates codes like this

<?php require_once('lib/swift_required.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_mail, $mail);
$query_get_sender = "SELECT * FROM `from`";
$get_sender = mysql_query($query_get_sender, $mail) or die(mysql_error());
$row_get_sender = mysql_fetch_assoc($get_sender);
$totalRows_get_sender = mysql_num_rows($get_sender);

mysql_select_db($database_mail, $mail);
$query_get_recipient = "SELECT * FROM `to`";
$get_recipient = mysql_query($query_get_recipient, $mail) or die(mysql_error());
$row_get_recipient = mysql_fetch_assoc($get_recipient);
$totalRows_get_recipient = mysql_num_rows($get_recipient);


$transport = Swift_SmtpTransport::newInstance($row_get_sender['smtp_host'], $row_get_sender['smtp_port'])
  ->setUsername($row_get_sender['usern'])
  ->setPassword($row_get_sender['passw'])
  ;

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance()

                ->setSubject('Test Mail')
                ->setFrom($row_get_sender['setfrom_email'],$row_get_sender['setfrom_mailname'])
                ->setTo()
                ->setBody($msg)
                ->setBcc($row_get_recipient['email'])
                ->setContentType('text/html')
                ->setReturnPath($row_get_sender['setfrom_email'])
                ;

$result = $mailer->send($message);      

?>
<?php
mysql_free_result($get_sender);

mysql_free_result($get_recipient);
?>

i need a mailer to send my email to about 10,000

You could open new topic.
But answer to your question could be done with Mailchimp service.
And if you want to use it dinamically from your PHP application, you can use their API system.

This code send 3000 bulk emails symantaniously to all.

step 1: create index.php page and paste the below code.
step 2: Now download phpMailer https://storage.googleapis.com/google-code-archive-downloads/v2/apache-extras.org/phpmailer/PHPMailer_5.2.4.zip

format: format.png

step 4: create a database name lets says "database" and create table "email"
after that create 2 columns "id","email" in the table & insert your all email address in email column.

index.php

<?php
error_reporting(0);
require("PHPMailer_5.2.4/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPDebug  = 2; 

$mail->Host = "smtp.gmail.com"; // specif smtp server
$mail->SMTPSecure= "ssl"; // Used instead of TLS when only POP mail is selected
$mail->Port = 465; // Used instead of 587 when only POP mail is selected
$mail->SMTPAuth = true;

$mail->From = "xxx@gmail.com";
$mail->FromName = "xxxx";
$mail->Username = "xxx@gmail.com"; // SMTP username
$mail->Password = "xxxx"; // SMTP password

$db = mysqli_connect("localhost", "root", "", "database");  //db connection
$sql = "SELECT email FROM email";        //select only email column
$result = mysqli_query($db, $sql); 
while($row = mysqli_fetch_array($result))
{
 if(!empty($row['email']))     //if email column is not empty
 {
     $email_list.=$row['email'].",";       //concatinate all rows value and saperate with them ',' e.g abc,abd,               

 }

}
 $email_list= rtrim($email_list,','); //trim the rightmost ',' and remove it e.g abc,abd

$to_array = explode(',',$email_list);   //remove all ',' from strings
foreach($to_array as $address)           // fetch one by one 
{
    $mail->addAddress($address, 'name'); 
    $mail->AddReplyTo($address, "naame");   // add one by one email id and send it
}

$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = 'for testing perpose ';
$mail->Body = 'I have been receiving multiple emails  ';

if($mail->Send()) {echo "Send mail successfully";}
else {echo "Send mail fail";} 
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.