Hello all,
I am not new to php but what I want to try is new to me. i am designing a website (elbonfx.com)that I want to include a kind of newsletter system into. Now I have a form where visitors can insert their details and it gets sent to my email address. I can also send email to each subscriber one by one. Now I have heard of phpmailer and I want to give it a shot, like I can use it to send newsletters to all my subscribers at once individually. I can design a form which will have the fields I need to send the emails to my subscribers, like my name, email address, return address, subject and message body, and it will also have its action as the php page where the phpmailer class is included, but that is as far as my knowledge about it goes. How do I configure the phpmailer class? How do I call it? Inshort, how do I use the phpmailer class with my form to send mails to multiple recipients? Any help will be appreciated. Thanks in advance.

Recommended Answers

All 5 Replies

It is very easy to incorporate and use the PHPMailer class, with the PHP mailer class they will be giving examples as well, please check them properly.

Or here the code goes like this

<?php
require_once('class.phpmailer.php');
$bodyOfMail = file_get_contents('mail_contents.html');
send_mail('saiprem', 'm.saiprem@gmail.com', $bodyOfMail)

function send_mail($name, $email, $bodyOfMail)
{
	
	$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
	
	$mail->IsSMTP(); // telling the class to use SMTP
	
	try {
	  //$mail->SMTPDebug  = 2;                    // enables SMTP debug information (for testing)
	  $mail->SMTPAuth   = true;                  // enable SMTP authentication
	  $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
	  $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
	  $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
	  $mail->Username   = "gmailusername@gmail.com";  // GMAIL username
	  $mail->Password   = "gmailpassword";            // GMAIL password

	  $mail->AddReplyTo('replytothemailid@anydomain.com', 'Sender Name');
	  $mail->AddAddress($email, $name);
	  $mail->SetFrom('wecanevensetFrom@domain.com', 'Sender Name');

	  $mail->Subject = 'Subject Of the email';

	  $mail->MsgHTML($bodyOfMail);
	  $mail->AddAttachment('images/if_anyattachments_are_needed.gif');      // attachment
	  $mail->AddAttachment('images/if_anyattachments_are_needed.jpg'); // attachment
	  if($mail->Send())
	  {
	  	return 1;
	  }
	  else
	  {
	  	return 0;
	  }
	  //echo "Message Sent OK</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!
	}
}

mail_contents.html is nothing but, you can create and design your page in html and then you can sent that mail

It is very easy to incorporate and use the PHPMailer class, with the PHP mailer class they will be giving examples as well, please check them properly.

Or here the code goes like this

<?php
require_once('class.phpmailer.php');
$bodyOfMail = file_get_contents('mail_contents.html');
send_mail('saiprem', 'm.saiprem@gmail.com', $bodyOfMail)

function send_mail($name, $email, $bodyOfMail)
{
	
	$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
	
	$mail->IsSMTP(); // telling the class to use SMTP
	
	try {
	  //$mail->SMTPDebug  = 2;                    // enables SMTP debug information (for testing)
	  $mail->SMTPAuth   = true;                  // enable SMTP authentication
	  $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
	  $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
	  $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
	  $mail->Username   = "gmailusername@gmail.com";  // GMAIL username
	  $mail->Password   = "gmailpassword";            // GMAIL password

	  $mail->AddReplyTo('replytothemailid@anydomain.com', 'Sender Name');
	  $mail->AddAddress($email, $name);
	  $mail->SetFrom('wecanevensetFrom@domain.com', 'Sender Name');

	  $mail->Subject = 'Subject Of the email';

	  $mail->MsgHTML($bodyOfMail);
	  $mail->AddAttachment('images/if_anyattachments_are_needed.gif');      // attachment
	  $mail->AddAttachment('images/if_anyattachments_are_needed.jpg'); // attachment
	  if($mail->Send())
	  {
	  	return 1;
	  }
	  else
	  {
	  	return 0;
	  }
	  //echo "Message Sent OK</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!
	}
}

mail_contents.html is nothing but, you can create and design your page in html and then you can sent that mail

Thank you my friend for your input. I am trying to make sense of the code you sent. I could not see how to implement multiple recipients in the code. I am looking at sending to 25 to 40 recipients at a time. How do I implement this in the code you sent. I am trying to understand the code as much as I can. I don't just believe in copying and pasting code; I need to understand how code works before using it.

Thanks all the same for pointing me in the right direction.

create an array of the 25 to 40 recepient then make a loop on your mailer.

Hi phpbeginners. How do I create the array? Please can you look at the code submitted by my other friend and show where and how to use the array? Thanks for your input.

Hi I didn't tried this, but it would helps you, add as many as userrs you need and put them like this

$mail->AddAddress($email1, $name1);
 $mail->AddAddress($email2, $name2);
 $mail->AddAddress($email3, $name3);
 $mail->AddAddress($email4, $name4);
 $mail->AddAddress($email5, $name5);
 $mail->AddAddress($email6, $name6);

If you are getting the data from database, then retrieve their email and name from DB and loop it as

for($i=0; $i<$count_of_database_result;$i++)
{
 $mail->AddAddress($data['email'], $data['name']);
}
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.