Hello i need help on PHPMailer here is my code :
$message = 'main message';
$bccmessage = 'BCC Message';
include '../inc/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
$mail->Host = "smtphost";
$mail->SMTPAuth = true;
$mail->Username = "email#domain.com";
$mail->Password = "xxxxxx";
$mail->From = "email#domain.com";
$mail->FromName = "foo.com";
$mail->AddAddress($mainemail);
$mail->AddBCC($bccemail);
$mail->AddBCC($bccemail);
$mail->Subject = "Subject";
$mail->Body = "$message";
if(!$mail->Send())
{
echo '<pre>Error: '.$mail->ErrorInfo.'</pre>';
exit;
} else {
//Display result
echo '<div class="success">message Sent</div>';
}
My question is how can i manage that the "AddAddress" get $message and the "AddBCC" get the $bccmessage message.
You will have to send 2 separate emails to accomplish that.
You could do something like:
$oMail->Body = $sToMessage;
$oMail->addAddress($sToEmail);
$oMail->send();
// clear
$oMail->ClearAddresses();
$oMail->Body = $sBccMessage;
$oMail->addAddress($sBccEmail);
$oMail->send();
But I'd have to advice against it. You would do better to wrap the email sending in an function - and calling it twice with different parameters.
Related
Good day. I need to send two different messages to two different people (user and admin). Tell me how to do this?
My mail php
<?php
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
$name = $_POST['name'];
$email = $_POST['email'];
$mail->isSMTP();
$mail->Host = 'smtp.mail.ru';
$mail->SMTPAuth = true;
$mail->Username = 'pmewilberries#mail.ru';
$mail->Password = '123456789456';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('pmewilberries#mail.ru');
$mail->addAddress($email);
$mail->Subject = 'Заявка на участие в интенсиве Wildberries';
// $mail->Body($body);
// $mail->isHTML(true);
$body = 'Hello'
$mail->msgHTML($body);
$mail->send()
?>
Send one message, change the properties that are different, then send the second one:
$mail->addAddress($email);
$body = 'Hello';
$mail->msgHTML($body);
$mail->send();
$mail->clearAddresses();
$mail->addAddress($email2);
$body = 'Hello2';
$mail->msgHTML($body);
$mail->send();
It will be slightly quicker if you set keepalive, which will make it re-use the existing connection for the second message:
$mail->SMTPKeepAlive = true;
Try this,
$mail->addAddress($email);
$body = 'Hello';
$mail->msgHTML($body);
$mail->send();
$mail->ClearAllRecipients();
$mail->addAddress($email2);
$body = 'Hello2';
$mail->msgHTML($body);
$mail->send();
Look at the code for clearAllRecipients;
I'm using Migadu mail server and PHP Mailer to set up a simple contact form.
This is the PHP file:
<?php
require("includes/class.phpmailer.php");
require("includes/class.smtp.php");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$smtpHost = "smtp.migadu.com";
$smtpUsername = "info#mywebsite.com";
$smtpPassword = "mypassword";
$to = 'info#mywebsite.com';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->IsHTML(true);
$mail->CharSet = "utf-8";
$mail->Host = $smtpHost;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->From = $smtpUsername;
$mail->FromName = $name;
$mail->AddAddress($to);
$mail->AddReplyTo($email);
$mail->Subject = "Contact Form";
$body = '<h1>Contact!</h1>';
$body .= '<p><b>Name: </b>'.$name.'</p>';
$body .= '<p><b>Email: </b>'.$email.'</p>';
$body .= '<p><b>Message: </b>'.$message.'</p>';
$mail->Body = $body;
$sentStatus = $mail->Send();
if($sentStatus){
echo json_encode(['status' => 'ok']);
}else{
echo json_encode(['status' => 'error', 'errorType' => 'server']);
}
?>
But it's not sending the mail. Also, it's not throwing any error, it just stuck in loading forever.
It's not showing any error because you're not displaying any errors - look in the ErrorInfo property. All of the examples provided with PHPMailer do this, so go look at them to see how to do that.
It's most likely that it's not "stuck forever", it's just that the timeout is long, and that's probably because your ISP blocks outbound SMTP, which is very common. The troubleshooting guide tells you how to diagnose this. Your ISP probably has an alternative method for sending email, for example they may provide their own relay, so you should refer to their docs.
<?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 have php code using phpmailer to send two different messages to two users. I have duplicate the code twice to send both mails, but that's makes the process takes long tome to complete the task. is there any solution to make my code more simple
//// -------------------- send email. to student adviser ----------------------------------------------------------
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->Username = "rms#gmail.com";
$mail->Password = "12121212";
$mail->AddAddress($advisoremail);
$mail->FromName = "RMS-NCT";
$mail->Subject = "New Request from: ".$_SESSION['UID'];
$mail->Body = "Dear Mr. Adviser you have got new request from 26s12115 ... click here to access it. http://localhost/rms/";
//-----------------------------------------------------------------------
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
// ------------send email to student ----------------------
$mail = new PHPMailer();
$mail->Username = "rms#gmail.com"; // your GMail user name
$mail->Password = "12121212";
$mail->AddAddress($_SESSION['UEMAIL']);
$mail->FromName = "RMS-NCT";
$mail->Subject = "Receipt for your new Request";
$mail->Body = "Dear Student .. Your request has been sent.. you will get response as soon as possible.";
//-----------------------------------------------------------------------
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
As long as you have two different subjects and bodies, I would say no. There is no way to simplify this task.
But you could put the task in one function, that gets the different parameters. So you just have one function to call.
require("phpmailer/class.phpmailer.php");
function send_mail($email, $subject, $body) {
$mail = new PHPMailer();
$mail->Username = "rms#gmail.com";
$mail->Password = "12121212";
$mail->AddAddress($email);
$mail->FromName = "RMS-NCT";
$mail->Subject = $subject;
$mail->Body = $body;
//-----------------------------------------------------------------------
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
}
require("phpmailer/class.phpmailer.php");
function sendMail($to = "trash#domain.com", $subject = "", $body = "", $from = "RMS-NCT")
{
$mail = new PHPMailer();
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = "rms#gmail.com";
$mail->Password = "12121212";
$mail->From = $mail->Username;
$mail->FromName = $from;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send()) {
return false;
}
return true;
}
In code, smth like this:
$Address = $advisoremail;
$Subject = "New Request from: " . $_SESSION['UID'];
$Body = "Dear Mr. Adviser ...";
if (sendMail($Address, $Subject, $Body)) {
echo "Message has been sent";
} else {
echo "Mailer Error: " . $mail->ErrorInfo;
}
I have used PHPMailer for SMTP and there is problem in sending mail with error "Mailer Error: The following From address failed: no-reply#mydomain.org.uk"
My code is as follows:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "localhost;"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = $email_address;
$mail->FromName = $email_address;
$mail->AddAddress($arrStudent[0]["email"]);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject";
$theData = str_replace("\n", "<BR>", $stuff);
$mail->Body = $theData; // "This is the <b>HTML body</b>";
$mail->AltBody = $stuff;
if (!$mail->Send()) {
$sent = 0;
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
i researched everything and when i debug inside class.smtp.php i found error the function "get_lines()" is returning error value "550 Authentication failed"
The code was working fine previously, i am wondering how this problem came suddenly.
Desperate for some help.
Thanks,
Biplab
public function sendEmail ( $subject, $to, $body, $from = FALSE ) {
require_once('mailer.class.php');
$mailer = new PHPMailer();
//do we use SMTP?
if ( USE_SMTP ) {
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = SMTP_HOST;
$mailer->Port = SMTP_PORT;
$mailer->Password = '';
$mailer->Username = '';
if(USE_SSL)
$mailer->SMTPSecure = "ssl";
}
$mailer->SetFrom($from?$from:ADMIN_EMAIL, ADMIN_NAME);
$mailer->AddReplyTo ( ADMIN_EMAIL, ADMIN_NAME );
$mailer->AddAddress($to);
$mailer->Subject = $subject;
//$mailer->WordWrap = 100;
$mailer->IsHTML ( TRUE );
$mailer->MsgHTML($body);
require_once('util.class.php');
$mailer->AltBody = Util::html2text ( $body );
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if ( ! $mailer->Send() ) {
return FALSE;
}
else {
$mailer->ClearAllRecipients ();
$mailer->ClearReplyTos ();
return TRUE;
}
}
I've used like that... SetFrom should be used in place of From... that's your error buddy... :))
try adding belowe line to php.ini
extension=php_openssl.dll
restart and try again
I am using YII's Mailer with PHPMailer, and this works for me:
$mail = Yii::createComponent('application.extensions.mailer.EMailer');
$mail->Username = $this->SMTP_USERNAME; // SMTP username
$mail->Password = $this->SMTP_PASSWORD; // SMTP password
$mail->SMTPAuth = true;
$mail->From = $this->fromAddress;
$mail->Host = $this->SMTP_SERVER_ADDRESS;
$mail->FromName = $this->fromName;
$mail->CharSet = 'UTF-8';
$mail->Subject = Yii::t('mailer', $this->subject);
$mail->Body = $this->message;
$mail->AddReplyTo($this->toAddress);
$mail->AddAddress($this->toAddress);
$mail->IsSMTP(true);
$mail->IsHTML(true);
$mail->Send();
Hope that helps?