Maintain xml format in mail in codeigniter - php

I have a mail function in codeigniter which is sending xml data but in my mail its not showing in proper format
Here in the below code $result contains data in xml format.
public function send_mail($id,$result){
require_once 'phpmailer/PHPMailerAutoload.php';
$mailto = "test#gmail.com";
$name = "Testing";
$content= "Result - $result <br>";
$subject= "Test Mail - $id : ";
$mail = new PHPMailer;
$mail->isSendmail();
$mail->setFrom('admin#mywebsite.com', 'Tester');
$mail->addAddress($mailto, $name);
$mail->Subject = $subject;
$mail->msgHTML($content);
if ($mail->send()) {
return true;
}else{
return false;
}
}

Add this :
$mail = new PHPMailer;
$mail->IsHTML(true);
...

Related

utf-8 for attachment in PHPmailer

I've got problem with attachment name, if there are polish language diacritical signs, it won't display those signs.
I added just below php mailer class $mail->CharSet = 'UTF-8'; and it works for email body text( without it I had the same problem), but not for attachments. Also I had a similar problem with user name, but solved it with utf8_decode() function. Unfortunately that function is not working with attachment's name in my case.
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
//$mail->addCustomHeader('Content-Type', 'text/plain;charset=utf-8');
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
$honeypot = $_POST['honey'];
$user_name = utf8_decode($_POST['name']);
$user_email = $_POST['email'];
$user_message = $_POST['message'];
$user_phone = $_POST['phone'];
$honeypot = trim($_POST["honey"]);
$max_size = 2 * 1024 * 1204; //2mb
$attachment = $_FILES['uploaded-file'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(!empty($honeypot)) {
echo "NO SPAM!";
exit;
} else {
$mail = new PHPMailer; //From email address and name
$mail->isMail();
//sender
$mail->From = $user_email;
$mail->FromName = $user_name;
//recipient
$mail->addAddress("jaroslaw.mor#gmail.com");
//mail subject
$mail->Subject = "Zapytanie ze strony www";
$mail->isHTML(true);
//body mail
$mail->Body = "Telefon:$user_phone<br><br>Treść wiadomośći:<br>$user_message";
$mail->AltBody = "Telefon:$user_phone\n$content";
//attachment
if(isset($attachment)) {
for ($i = 0; $i < count($_FILES['uploaded-file']['name']); $i++) {
if ($_FILES['uploaded-file']['error'][$i] !== UPLOAD_ERR_OK) continue;
$file_TmpName = $_FILES['uploaded-file']["tmp_name"][$i];
$file_name = utf8_decode( $_FILES['uploaded-file']["name"][$i]);
if ($_FILES['uploaded-file']['size'][$i] > $max_size) {
echo "file is too big";
die();
}
else{
move_uploaded_file($file_TmpName, "uploads/" . $file_name);
$mail-> AddAttachment("uploads/". $file_name);
}
}//for
}//isset
if(!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
exit();
}
else {
header("Location: sent.html");
exit();
}//if send else
}//honey else end
}//post end
You can try to add this code before attachment upload:
setlocale( LC_ALL, "pl_PL.UTF-8" );
Try force encoding for attachment:
$mail->AddAttachment("uploads/", $file_name, $mail::ENCODING_BASE64, PHPMailer::filenameToType($file_name).'; charset=utf-8');
I tried all methods from here - without a positive result. By the accident I opened PHPMailer.php and noticed that there are some options connected with charset encoding, changed settings there and now it works.
Thx all for clues.

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

How to use phpmailer and send mail as html from ckeditor

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'];

how to variable pass mail function in php

I am new to php and i want solution for variable pass in mail function
I am using this way to variable pass mail function but getting issue Code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
function Send_Mail($to,$subject,$body,$cc = "",$bcc = "")
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPDebug = false;
$mail->Username = 'xyz#gmail.com'; // Your Gmail Address
$mail->Password = '123456'; // Your Gmail Password
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'xyz#gmail.com'; // From Email Address
$mail->FromName = 'gfgg'; // From Name
$mail->addAddress($to);
$mail->addReplyTo('xyz#gmail.com', 'dfg'); // Same as From Email and From Name.
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send()) {
return array('status' => false, 'message' => $mail->ErrorInfo);
}
return array('status' => true, 'message' => 'Email Sent Successfully');
}
if (isset($_REQUEST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
echo $name, $email, $contact;
$result = Send_Mail('$name','$email','$contact');
}
pls give me soltution
?>
The Send_Mail function requires minimum 3 parameters, which are $to, $subject and $bodyin this order.
So first of all you are calling your parameters within strings
$result = Send_Mail('$name','$email','$contact');
and in the wrong order.
You should probably try this
$result = Send_Mail($email, "Subject", "Message");
You will have to define $subject and $message variables and replace them with "Subject" and "Message"
Try with -
echo "$name, $email, $contact";
$result = Send_Mail($name,$email,$contact);

PHPMailer doesn`t work with AddAddress property set

I'm trying to create a contact form using phpMailer and I get in firebug this:
NetworkError: 500 Internal Server Error - path/process.php
uncaught exception: [object Object]
each time I'm trying to run the code below.
Please note that the error is not shown anymore if I remove $mail->AddAddress line, that's why I suspect this line to be the cause.
Instead a new error is displayed: You must provide at least one recipient email address when I remove it.
<?php
$name = $_POST['firstName'];
$email = $_POST['email'];
require("phpmailer.inc.php");
$mail = new PHPMailer(true);
$mail->IsMail();
$mail->From = $email;
$mail->Subject = "subject ";
$mail->Body = "From $name with email: $email";
$mail->WordWrap = 50;
$mail->AddAddress('my_address#gmail.com','my name');
$mail->Send();
?>
Thanks!
Rewrite it to:
<?php
$name = $_POST['firstName'];
$email = $_POST['email'];
require 'phpmailer.inc.php';
try {
$mail = new PHPMailer(true);
$mail->IsMail();
$mail->From = $email;
$mail->Subject = "subject ";
$mail->Body = "From $name with email: $email";
$mail->WordWrap = 50;
$mail->AddAddress('my_address#gmail.com','my name');
$mail->Send();
} catch (Exception $e) {
echo $e->getMessage();
}
and you will catch the error

Categories