I am trying to send the multiple mails through my mailer from the below code, But it is showing error
apache http error occurred.
<?php
require 'phpmailer/PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.domain.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->setFrom('info#domain.com', 'Name');
$mail->addReplyTo('info#domain.com', 'Name');
// read the list of emails from the file.
$email_list = file("maillist1.csv");
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++) {
$email_list[$counter] = trim($email_list[$counter]);
}
// implode the list in
require 'phpmailer/PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.domain.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->setFrom('info#domain.com', 'Name');
$mail->addReplyTo('info#domain.com', 'Name');
// read the list of emails from the file.
$email_list = file("maillist1.csv");
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++) {
$email_list[$counter] = trim($email_list[$counter]);
}
// implode the list into a single variable, put commas in, apply as $to value.
$to = implode(",",$email_list);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
if ( mail($to,$subject,$message) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
}
?>to a single variable, put commas in, apply as $to value.
$to = implode(",",$email_list);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
if ( mail($to,$subject,$message) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
}
?>
Related
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "myemail";
$mail->Password = "myemailpassword";
if(isset($_POST['submit'])){
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail->IsHTML(true);
$mail->AddAddress("myemail", "name");
$mail->SetFrom($from);
$mail->AddReplyTo($from);
$mail->Subject = $subject;
$content = $message;
$mail->MsgHTML($content);
if(!$mail->Send()) {
echo "Error while sending Email.";
var_dump($mail);
}
else {
echo "Email sent successfully";
}
}
?>
the code works if I enter the details myself rather than getting them from my html form. for example for $mail->SetFrom("myemail", "my name")
tried defining the variable when I call it in the mail function that didn't work either
I have a script that I'm running using phpmailer - it's supposed to send 2 emails - a verification email and a request email. for some reason it's sending the verification email twice. I've gone over the code 100 times and I can't figure out why. Relevant code is below - it's not nested in any loop or condition. the "code" is a get variable and "auth" is pulled from a database.
<?php
if ($code==$auth) {
$mailtitle="message 1";
$message="message text";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.mysite.org";
$mail->From = "authorize#mysite.org";
$mail->FromName = "mysite";
$mail->AddAddress($dre);
$mail->SMTPAuth = "true";
$mail->SMTPSecure = 'tls';
$mail->Username = "authorize#mysite.org";
$mail->Password = "thepassword";
$mail->Port = "587";
$mail->Subject = $mailtitle;
$mail->Body = $message;
$mail->isHTML(true);
$mail->WordWrap = 180;
$mail->send();
$mailtitle="message 2 title";
$message="message text";
$mail1 = new PHPMailer();
$mail1->IsSMTP();
$mail1->Host = "mail.mysite.org";
$mail1->From = "authorize#mysite.org";
$mail1->FromName = "mysite";
$mail1->AddAddress('authorize#mysite.org');
foreach ($pea as $px) {
$mail1->addBCC($px);
}
foreach ($sea as $sx) {
$mail1->addBCC($sx);
}
$mail1->SMTPAuth = "true";
$mail1->SMTPSecure = 'tls';
$mail1->Username = "authorize#mysite.org";
$mail1->Password = "thepassword";
$mail1->Port = "587";
$mail1->Subject = $mailtitle;
$mail1->Body = $message;
$mail1->isHTML(true);
$mail1->WordWrap = 180;
$mail1->send();
}
?>```
You may disable the BCC by REMOVING the following
foreach ($pea as $px) {
$mail1->addBCC($px);
}
foreach ($sea as $sx) {
$mail1->addBCC($sx);
}
I have developed a site in PHP and added a PHPMailer function for my contact us page but the mailer functionality is not working. Contacted with support team but they didn't help me tried a lot with that.
<?php
if(isset($_POST['submit_contact']))
{
require 'PHPMailer/PHPMailerAutoload.php';
$to = "gmail#gmail.com";
$name = $_POST['uname'];
$email = $_POST['email'];
$phone = $_POST['phonenumber'];
$companyname = $_POST['companyname'];
$country = $_POST['country2'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "xxxx#gmail.com";
$mail->Password = "PASSword1#3";
$message = array();
$message[] = 'Name : '.trim($name).' ';
$message[]='Phone : '.trim($phone).' ';
$message[]='Email : '.trim($email).' ';
$message[]='Company Name : '.trim($companyname).' ';
$message[]='Country : '.trim($country).' ';
$message = implode('<br/>', $message);
$mail->SetFrom($email);
$mail->Subject = 'Here is the subject';
$mail->Body = $message;
$mail->AddAddress($to);
if(!$mail->send()) {
$msg = "Error while sending email";
$msgclass = 'bg-danger';
header("Location: /");
die();
}
else {
$msg = 'A mail with recovery instruction has sent to your email.';
$msgclass = 'bg-success';
}
}
?>
u used gmail,u should set host to smtp.gmail.com, SMTPSecure to ssl,SMTPAuth to true,port to 465,u alse should use the app password,that u can see https://support.google.com/mail/answer/7126229
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = false;
$mail->SMTPSecure = 'none';
$mail->Host = "localhost";
$mail->Port = 25;
$mail->IsHTML(true);
Solved the issue with this update in SMTP
<?php
require 'PHPMailerAutoload.php';
//echo !extension_loaded('openssl')?"Not Available":"Available <br/>";
$name = $_POST['username'];
$email = $_POST['email'];
$number = $_POST['phone'];
$profession = $_POST['profession'];
$to = 'example#gmail.com';
$subject = 'user registration';
$phone = "phone number:".$number;
$message = "client details:"."\n"."Name:".$name."\n"."email:".$email."\n"."phone number:".$number."\n"."profession:".$profession;
$headers = "From:".$email;
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom($email, $name);
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send()) {
header("Location: ../../thankyouNew.html");
}
else {
header("Location: ../../somethingWrong.html");
}
?>
code is executing else block, i want to send mail to example#gmail.com and return user to the thankyou.html page after the mail function is executed.I am new to this php and i would highly appreciate the help thank you in advance.
forget the below lines........
You don't actually specify where you want to send the email. You need to use the addAddress() method, as shown below. This method requires one parameter, but you may supply two - in the same way your setFrom() method has; first the target address, then an optional display name.
$mail = new PHPMailer;
// ...
$mail->setFrom($email, $name);
$mail->addAddress($to); // Add this method to specify a recipient
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send()) {
// ...
}
// ...
I need to send the email to the multiple recipients but I am getting error in my code. I need to send the email to multiple recipients.
<?php
require 'phpmailer/PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.domain.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->setFrom('info#domain.com', 'name');
$mail->addReplyTo('info#domain.com', 'name');
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);
if(!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
?>
<script>alert('<?php echo $error ?>');</script>
<?php
}
else {
echo "Message Sent Successfully";
}
}
?>
Try this code.
require 'PHPMailer/PHPMailerAutoload.php';
function SendPHPMail($to, $from, $subject, $htmlContent, $attachments = array())
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'emailAddress#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => true,
'allow_self_signed' => true
)
);
$mail->From = 'emailAddress#gmail.com'; //sender emailAddress
$mail->FromName = 'name'; //sender name
//Here $to has multiple emailAddress
//$to = array('address1#domain.com','address2#domain.com','address3#domain.com');
if(!empty($to)){
foreach($to as $emailAddress){
$mail->addAddress($emailAddress);
}
} else{
throw new \Exception('No emails found!');
}
if(!empty($attachments)){
foreach($attachments as $attachment){
$mail->addAttachment($attachment);
}
}
//$mail->addCC();
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $htmlContent;
if(!$mail->send()) {
throw new \Exception($mail->ErrorInfo);
}
}
instead of $mail->addAddress($to_id); use something like this:
$mail->addAddress("peter#doe.com , hannes#mail.com , manuel#domain.com", "...");
You can use just like that:
$mail->AddAddress('email1#domain.com', 'First Email');
$mail->AddAddress('email2#domain.com', 'Second Email');
Or if you have emails in an array $_POST['toid'], than you can use AddAddress() in a loop.