How to use phpmailer and send mail as html from ckeditor - php

I'm use phpmailer to sending email, I have problems when inserting the contents of the html code on the form ckeditor, but data sent to the e-mail text only.
This is my code:
require_once ('class.phpmailer.php');
$mail = new PHPMailer(true);
if (isset($_POST['btn_send']))
{
$smtp_username = strip_tags($_POST['username']);
$smtp_password = strip_tags($_POST['password']);
$ssl_port = strip_tags($_POST['port']);
$my_smtp = strip_tags($_POST['host']);
$my_ssl = strip_tags($_POST['type']);
$realname = strip_tags($_POST['realname']);
$subject = strip_tags($_POST['subject']);
$body = strip_tags($_POST['editor']);
$emaillist = strip_tags($_POST['emaillist']);
//...now get on with sending...
try
{
//$mail->isSendmail();
$mail->isSMTP();
$mail->Body = ($body);
$mail->isHTML(true);
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "$my_ssl";
$mail->Host = "$my_smtp";
$mail->Port = "$ssl_port";
$mail->AddAddress($emaillist);
$mail->Username = "$smtp_username";
$mail->Password = "$smtp_password";
$mail->SetFrom("$smtp_username", "$realname");
$mail->AddAddress($emaillist);
$mail->epriority = "3";
$mail->AddReplyTo("$smtp_username");
$mail->Subject = "$subject";
$mail->encode = ("yes");
$mail->CharSet = "utf-8";
if($mail->Send())
{
$msg = "<div class='alert alert-success'>
Hi,<br /> bro mail terkirim ke ".$emaillist."
</div>";
}
}
catch(phpmailerException $ex)
{
$msg = "<div class='alert alert-warning'>".$ex->errorMessage()."</div>";
}}
I do not know what went wrong

You need edit this row
$body = strip_tags($_POST['editor']);
to $body = $_POST['editor'];
And add this line before mail send
$mail->isHTML(true);
Function strip_tags removes html markup.
But you need filter value another way.

Please edit this line
$body = strip_tags($_POST['editor']); to $body = $_POST['editor'];
the strip_tags() function will remove all tags from your posted value. The you cant get the the html tags.
Please enable html in your PHP mailer by adding this line
$mail->isHTML(true);
So please edit the code and try this way.
This is working fine in my case.

If you are using Codeigniter, you might face a problem if doing:
$body = $this->input->post('some_field_with_html_body');
You can fix it by doing:
$body = $_POST['some_field_with_html_body'];

Related

PHPMailer not working when trying to create using a PHP Function

I am using PHPMailer for sending emails. But I have to write that long code of sending emails on every page. So I thought of trying to put the whole stuff into a function and just calling it whenever I want to make the things dry, simple and easier. But its not working when trying to send emails. I tried the following:
functions.php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$settings = $pdo->prepare("SELECT * FROM settings");
$settings-> execute();
$set = $settings->fetch();
function newMail($name, $email, $sub, $msg, $set) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = $set['set_smtp_host'];
$mail->Port = $set['set_smtp_port'];
$mail->SMTPSecure = $set['set_smtp_security'];
$mail->IsHTML(true);
$mail->SMTPAuth = true;
$mail->Username = $set['set_smtp_uname'];
$mail->Password = $set['set_smtp_pass'];
$mail->setFrom($set['set_noreply_email'], $set['set_site_name']);
$mail->addAddress($email, $name);
$mail->Subject = $sub;
$mail->Body = $msg;
$mail->Send();
}
Now I tried calling the function on another page (where functions.php is included) this way:
$fname = (!empty($_POST['fname']))?$_POST['fname']:null;
$email = (!empty($_POST['email']))?$_POST['email']:null;
$sub = ''.$title.' - Account Verification Link';
$msg = 'SOME BODY MESSAGE';
if(newMail($fname, $email, $sub, $msg)){
echo alert_success("Registration successful! Please check your email and click on the activation link to activate your account. If you did not receive any email within 5 minutes then <a href='resend.php'>click here</a> to resend it.");
}else{
echo alert_success("Registration successful! But unfortunately, we could not send you a verification email. Please <a href='resend.php'>click here</a> to resend it.");
}
Here its always returning the else message. Am I wrong coding something here?
Modify your function newMail at the end by :
return $mail->Send();
The send method return true in case the mail is sent , so you function should return this value , if not :
if(newMail(...)){ }
Will always be false that why the else case is applied.
function newMail($name, $email, $sub, $msg, $set) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = $set['set_smtp_host'];
$mail->Port = $set['set_smtp_port'];
$mail->SMTPSecure = $set['set_smtp_security'];
$mail->IsHTML(true);
$mail->SMTPAuth = true;
$mail->Username = $set['set_smtp_uname'];
$mail->Password = $set['set_smtp_pass'];
$mail->setFrom($set['set_noreply_email'], $set['set_site_name']);
$mail->addAddress($email, $name);
$mail->Subject = $sub;
$mail->Body = $msg;
return $mail->Send(); // add return here
}

PHPMailer with Migadu not working

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 mail() function not working using phpmailer

<?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()) {
// ...
}
// ...

PHPMailer sending multiple emails to every receipents in database

I am using this code to send email to everyone in the database in a loop. Problem occurred when I find out that user 1 in database also getting all emails sent to other users in the database. User 2 gets all email sent to other users after him and so on. I want to send a single email to everyone customized with their name and emails. Did check and applied couple of solutions from this forum but they are not working.
#SEND EMAILS
require '../includes/PHPMailer/PHPMailerAutoload.php';
require '../includes/PHPMailer/config.php';
if(isset($_POST['submit'])){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = $smtp_server_w;
$mail->Username = $username_w;
$mail->Password = $password_w;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->setFrom("test#test.com", "Test Name");
$sql_mail = "SELECT * FROM users";
$run_mail = mysqli_query($conn, $sql_mail);
while ($row_mail = mysqli_fetch_assoc($run_mail)) {
$name = $row_mail["user_name"];
$email = $row_mail["user_email"];
$mail->ClearAddresses();
$mail->addAddress('test#test.com', 'Test Name');
$mail->addBCC($email, $name);
$mail->isHTML(true); // Set email format to HTML
$bodyContent = "<p>Multiple Messages</p>";
$mail->Subject = $row_mail['user_name'].", New job is available " ;
$mail->Body = $bodyContent;
$mail->AltBody = $bodyContent;
if(!$mail->send()) {
echo "Message could not be sent.";
$error = '<div class="alert alert-danger"><strong>Mailer Error: '. $mail->ErrorInfo.'</strong></div>';
} else {
$error = '<div class="alert alert-success"><strong>Message has been sent</strong></div>';
}
}
}

PHPMailer tweek

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.

Categories