I have a problem sending multiple email addresses functions. If I send a single email address, I can work. If I send multiple emails to the receiver, they cannot receive my message. I am using the PHPMailer function to do the email function with XAMPP. I am using the PHP array function to put the receiver's address in the array and use the while function to loop the receiver address to send it.
Below is my coding:
$address = array('st9overfindsolution#gmail','st7overfindsolution#gmail');
require 'class/class.phpmailer.php';
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
// $mail->SMTPDebug = 1;
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = '1233aqqq';
$mail->SMTPSecure = 'ssl';
$mail->From = $_POST["email"];
$mail->FromName = $_POST["name"];
$mail->AddCC($_POST["email"], $_POST["name"]);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
while(list ($key, $val) = each($address)){
$mail->AddAddress($val);
}
What I've tried?
1.I put all receiver email addresses in the array $address = array('st9overfindsolution#gmail','st7overfindsolution#gmail'); and use below while function code, but cannot work.
2.If send single email address without using while function, just can work, like below coding:
Hope someone can guide me on how to solve this problem. Thanks.
i have created new function named "sendmyemail" and inserted the code inside this function, so you can sent multiple emails by calling this function with email address.
<?php
/**
* This example shows sending a message using a local sendmail binary.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
function sendmyemail($whotoEmail){
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($whotoEmail);
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//Read an HTML message body from an external file, convert referenced images to
embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent to '.$whotoEmail.'!';
}
}
sendmyemail('abcduser#gmail.com');
sendmyemail('efguser#gmail.com');
sendmyemail('hijkuser#gmail.com');
sendmyemail('lmnouser#gmail.com');
Related
I am trying to send an email to multiple different BCC recipients.
Every recipient gets an identical text body BUT, he also needs to receive his own individual email in it.
I'm looping through a JSON to add emails with the $mail->addBCC() function.
And I need that the $body that is sent to every individual user will contain his own individual address.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
//*** -> $allUsers is a JSON
function sendemails_ex($allUsers, $subject, $body)
{
$emailFrom = "noreply#slandergold.com";
$emailFromName = "slandergold.com";
if ($allUsers=="" || $subject=="" || $body=="")
{
exit();
}
$smtpUsername = "abcdKLARK";
$smtpPassword = "1t%y$R5$4";
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = "mail.slandergold.com";
$mail->Port = 26; //587; // TLS only
$mail->SMTPSecure = false; //'tls'; // ssl is depracated
$mail->SMTPAuth = false; //true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$arr = json_decode($allUsers,true);
foreach($arr as $item)
{
$mail->addBCC($item['Email'], $item['Fullname']);
}
$mail->isHTML(true);
$mail->Subject = $subject;
// ***********
// is there a way to make it
// so that every individual BCC recipient
// will get a body with the individual addition of:
// $userEmail = ?
// $body."<br />This is your email: ".$userEmail;
// ***********
$mail->msgHTML($body);
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}
}
As the comments say, you can’t send different emails to different recipients using BCC. You need to send each message individually, and the definitive way to do that is provided in the mailing list example provided with PHPMailer. There are also notes in the project wiki about how to send to lists efficiently.
Guys i am now working with php mailer library.i have some errors related to mail()
functions.
This my source code
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.google.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'venki14101996#gmail.com'; // SMTP username
$mail->Password = '8903273610'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'venki14101996#gmail.com';
$mail->FromName = 'VENKAT';
$mail->AddAddress('venkat14101996#gmail.com', 'Josh Adams'); // Add a recipient
$mail->AddAddress('rishi27052001#gmail.com'); // Name is optional
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <strong>in bold!</strong>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
THIS SHOWs ME A ERROR MESSAGE
Message could not be sent.Mailer Error: The following From address failed: venki14101996#gmail.com : Called Mail() without being connected
At before i use this source code before
but i don't have the vendor/autoload file in my phpmailer folder
but i download this file in Github
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
* This uses traditional id & password authentication - look at the gmail_xoauth.phps
* example to see how to use XOAUTH2.
* The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
*/
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//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';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
//Section 2: IMAP
//Uncomment these to save your message in the 'Sent Mail' folder.
#if (save_mail($mail)) {
# echo "Message saved!";
#}
}
//Section 2: IMAP
//IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php
//Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php
//You can use imap_getmailboxes($imapStream, '/imap/ssl') to get a list of available folders or labels, this can
//be useful if you are trying to get this working on a non-Gmail IMAP server.
function save_mail($mail)
{
//You can change 'Sent Mail' to any other folder or tag
$path = "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail";
//Tell your server to open an IMAP connection using the same username and password as you used for SMTP
$imapStream = imap_open($path, $mail->Username, $mail->Password);
$result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
imap_close($imapStream);
return $result;
}
enter code here
Try adding the below code,
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
I am using a code to find the individuals that check in late and send emails to them. also I find those that haven't come at all and send emails to them too. However, it is not working. I get the names and emails correctly, but the $mail object is null, and I don't understand why.
This is my code:
mail_sender.php (this is what i call to send the message)
<?php
function custom_mail($name, $surname, $email, $message, $subject){
//$mail->SMTPDebug = 3; // Enable verbose debug output
require './PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
global $mail;
var_dump($mail);
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com;'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '****'; // SMTP username
$mail->Password = '****'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = ****; // TCP port to connect to
$mail->From = '****';
$mail->FromName = '****';
$mail->addAddress($email, $name." ".$surname); // Add a recipient
$mail->addCC('****');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = ucwords($name).' '.ucwords($surname).'! <br />'.$message;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//
if(!$mail->send()) {
echo 'email -> '.$email.' name -> '.$name.' surname -> '.$surname.'<br />';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else {
$mail->ClearAllRecipients(); //clears the list of recipients to avoid other people from getting this email
}
}
?>
I think this may be your problem:
$mail = new PHPMailer();
global $mail;
var_dump($mail);
This just doesn't look like a good idea - if you already have a variable called $mail defined globally, it may overwrite your PHPMailer instance, making it null. Change the order to this:
global $mail;
$mail = new PHPMailer();
var_dump($mail);
I don't see that there's much reason to make this available globally at all - if you want to re-use the instance across multiple calls, this won't help - you should be declaring it statically to do that, like this:
static $mail;
if (!isset($mail)) {
$mail = new PHPMailer();
}
var_dump($mail);
can anyone provide me the way of sending mail through php with attached object as well .plz i am new in this kindly help me in this. is there any server to be installed for this? the mail should this on any email account aswell plz help me in this.can any one provide me the link of tutorial i used the tutorial HERE it display me the error Fatal error: Call to undefined function IsSMTP() in C:\wamp\www\EMS3\mail.php on line 13 plz help me in this
There is an error in the example provided there. Use the following code.
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
update you also need to set the external SMTP server your using. if your using google. i believe its smtp.gmail.com
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Secure = "ssl";
You can use an external PHP library for sending mail, which I have used internally on a localhost, but still configured the parameters to send to external sources. The package is Swift Mailer.
In localhost you can't send any mail. After hosting only this is possible.
I want to attach a image as an attachment using mail() function.
I am using xampp and want the image to be sent from my computer to an email id.
This code is sending text email easily:
<?php
if(mail('abc#gmail.com','Hello','Testing Testing','From:xyz#gmail.com'))
{
echo "Success";
} else {
echo "Fail";
}
?>
I want to add an image after it using normal mail method of php.
you need to use the pear library for composing or sending the mail.
include_once('Mail.php');
include_once('Mail_Mime/mime.php');
$message = new Mail_mime();
$message->setTXTBody($text);
$message->addAttachment($path_of_uploaded_file);
$body = $message->get();
$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);
here is a way
You could use the Mail class from the Zend library, very simple and no reliance on PEAR.
Its been covered in a previous question here.
I suggest to use Swiftmailer. It is up to date, easy to install and use. You can install it via PEAR, but there are lots of other options you might find more convenient as well.
Example code to send a mail with an attachement taken from the manual:
require_once 'lib/swift_required.php';
// Create the message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject('Your subject')
// Set the From address with an associative array
->setFrom(array('john#doe.com' => 'John Doe'))
// Set the To addresses with an associative array
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
// Give it a body
->setBody('Here is the message itself')
// And optionally an alternative body
->addPart('<q>Here is the message itself</q>', 'text/html')
// Optionally add any attachments
->attach(Swift_Attachment::fromPath('my-document.pdf'));
this is using php and ajax it will work 100%
<?php
include "db.php";
if(isset($_POST['tourid']))
{
$to=$_POST['email'];
$file_name = "test/sample.pdf";
require 'class/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); //Sets Mailer to send message using SMTP
$mail->Host = ''; //Sets the SMTP hosts of your Email hosting, this for Godaddy
$mail->Port = ''; //Sets the default SMTP server port
$mail->SMTPAuth = true; //Sets SMTP authentication. Utilizes the Username and Password variables
$mail->Username = ''; //Sets SMTP username
$mail->Password = ''; //Sets SMTP password
$mail->SMTPSecure = ''; //Sets connection prefix. Options are "", "ssl" or "tls"
$mail->From = ''; //Sets the From email address for the message
$mail->FromName = ''; //Sets the From name of the message
$mail->AddAddress($to, 'Name'); //Adds a "To" address
$mail->WordWrap = 50; `` //Sets word wrapping on the body of the message to a given number of characters
$mail->IsHTML(true); //Sets message type to HTML
$mail->AddAttachment($file_name); //Adds an attachment from a path on the filesystem
$mail->Subject = 'Customer Details'; //Sets the Subject of the message
$mail->Body = 'Please Find Tour details in attached PDF File.'; //An HTML or plain text message body
if($mail->Send()) //Send an Email. Return true on success or false on error
{
$message = '<label class="text-success">Tour Details has been send successfully...</label>';
echo $message;
unlink($file_name);
}
}
else
{
echo "sending error";
}
?>