Error using phpMailer and isHTML - php

Im using phpMailer to send an image like attachment and a message in HTML. But the page show me that is not working.
include 'PHPMailer-master/class.phpmailer.php';
$mail = new PHPMailer();
$mail->From = 'someone#some.com';
$mail->FromName = 'someone';
$mail->AddAddress( 'someone#some2.com' );
$mail->Subject = 'Message Subject';
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
$mail->AltBody="This is text only alternative body.";
$mail->IsHTML(true);
$file_to_attach = '../images/logo.png';
$mail->AddAttachment( $file_to_attach , 'logo.png"' );
if(!$mail->Send())
echo "Error sending: " . $mail->ErrorInfo;;
else
echo "Letter is sent";
Using this code works perfectly to me send a message with HTML and an attachment.
<?php
require_once 'PHPMailer-master/class.phpmailer.php';
$mail = new PHPMailer(true);
try {
$mail->AddAddress('someone#some.com', 'a name');
$mail->SetFrom('someone2#some2.com', 'another name');
$mail->AddReplyTo('someone3#some3.com', 'another name');
$mail->Subject = 'PHPMailer Test';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->IsHTML(true);
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
$mail->AddAttachment('../images/logo.png');
$mail->Send(); echo "Message Sent OK\n";
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
catch (Exception $e) {
echo $e->getMessage();
}
?>

You are using $email variable to new object PHPMailer but in line 8,9 and 14,15 you're using $mail.
$mail->AltBody="This is text only alternative body.";
$mail->IsHTML(true);
if(!$mail->Send())
echo "Error sending: " . $mail->ErrorInfo;
just use $email.

I see you are using
$file_to_attach = '../images/logo.png';
to get the name for the image file. However the file name is already set in:
$email->AddAttachment( $file_to_attach , 'logo.png"' );
Try putting the whole path in $email->AddAttachment(); like this $email->AddAttachment('../images/Logo.pgn');

Related

I am unable to send mail with PHPMailer

This is my index.php file
Please suggest what is wrong with the below code that i am not receiving mails or if there is any other easy way of sending emails.
<?php
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth=false;
$mail->SMTPSecure='tls';
$mail->Host='smtp.gmail.com';
$mail->port='587';
$mail->isHTML();
$mail->Username='demoXXXXX#gmail.com';
$mail->Password='XXXXXX';
$mail->SetFrom('no-reply#gmail.com');
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->addAddress("parkingston#gmail.com");
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
Include class.phpmailer.php & classes/class.smtp.php first .
<?php
require "autoload.php";
include "class.phpmailer.php"; // include the class name
include "class.smtp.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth=false;
$mail->SMTPSecure='tls';
$mail->Host='smtp.gmail.com';
$mail->port='587';
$mail->isHTML();
$mail->Username='demoXXXXX#gmail.com';
$mail->Password='XXXXXX';
$mail->SetFrom('no-reply#gmail.com');
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->addAddress("parkingston#gmail.com");
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
You can easily download this both file if you do not have it. Hope it will help you.

Could not instantiate mail function - Codeigniter and PHPMailer

I want to send an email using PHPMailer and I'm using Codeigniter
public function check_email(){
$response = array('error' => false);
$email = $this->input->post('email');
$check_email = $this->fp_m->check_email($email);
if($check_email){
$this->load->library('phpmailer_library');
$mail = $this->phpmailer_library->load();
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = "Reset Password";
$mail->Body = "
Hi,<br><br>
In order to reset your password, please click on the link below:<br>
<a href='
http://example.com/resetPassword.php?email=$email
'>http://example.com/resetPassword.php?email=$email</a><br><br>
Kind Regards,<br>
Kokushime
";
if($mail->send()){
$response['error'] = false;
$response['message'] = "The Email Sent. Please Chect Your Inbox";
}else{
$response['error'] = true;
$response['message'] = "There's Something wrong while sending the message". $mail->ErrorInfo;
;
}
}else{
$response['error'] = true;
$response['message'] = 'The email that you entered is not associated with admin account';
}
echo json_encode($response);
}
But it gives me error Could not instantiate mail function.
BTW, I'm not using SMTP because i don't need that..
I hope that you can help me :)
You did not include your PHPMailer config. Since you are not using SMTP do you have this set?
$mail->isSendmail();
Also, assuming you are using CI3 it would probably be easier if you installed PHPMailer with composer and had it autoload.
I just tested this and it works fine using sendmail.
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class Phpmailer_test extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->isSendmail();
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addReplyTo('info#example.com', 'Information');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
}

PHPMailer "CharSet" incompatibility with "AltBody" and "AddEmbeddedImage" at the same time

I have this code where sending body in UTF-8 is not working:
<?php
require '../phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->setFrom('from#example.com', 'First Last');
$mail->addAddress('whoto#example.com', 'John Doe');
$mail->Subject = 'PHPMailer mail() test';
$mail->AltBody = 'This is a plain-text message body';
$mail->AddEmbeddedImage('img_00/Mailing_febrero2015_02.jpg', 'fondo', 'fondo.jpg');
$mail->Body = 'árbol';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
But if I remove the "AltBody" or "AddEmbeddedImage" it sends correctly in UTF-8. Why this occurs (I have the last PHPMailer version)?
There is another correct way to add an alternative body and embedded image at the same time?

sending attachment fails with php mailer function codeigniter

Here I am trying to send email with an attachment, all codes are running fine but I didn't find any attachment along with the mail, but the same file is getting printed in hyper link that I had given with success message. I have used php mailer class in codeigniter.
public function sendmailto()
{
$this->load->library('phpmail');
$mail = new PHPMailer();
$body = "hello";
$mail->AddReplyTo("reply#mymail.com","First Last");
$mail->SetFrom('noname#mymail.com', 'First Last');
$mail->AddReplyTo("mail#mymail.com","First Last");
$address = "abcd#mymail.com";
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment("../../uploads/a.pdf"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!<a href='../../uploads/a.pdf' >click</a>" ;
}
}
I think its path issue you can try this
$attachment = base_url().'/uploads/a.pdf';
$mail->AddAttachment($attachment); // attachment
I think you should change
$mail->MsgHTML();
to
$mail->Body;

Sending Plain text emails using PHPMailer

I have a problem sending plain text emails using PHPMailer.
I have text that I read from a text file and mail it to mail recipient via PHPMailer
When the recipient gets the actual email, the formatting of the mail is not like in the text file, everything is in one line, no new lines and tabs are included in the email that I send. Text wrapping is totally off.
Code:
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$address = "test#test.com";
$mail->AddAddress($address, "John Doe");
$mail->SetFrom(EMAIL_TEST_FROM);
$mail->AddReplyTo(EMAIL_TEST_REPLY);
$mail->Subject = $action." REGISTRATION ".$formName.$tld;
$mail->From = EMAIL_TEST;
$mail->MsgHTML(file_get_contents($newFile));
if($mail->Send()){
return true;
}
You are setting $mail->MsgHTML() to a plain text message, and since whitespace formatting is ignored in HTML, you're getting an inline text.
I haven't used PHPMailer for a while, but from memory try:
$mail->Body = file_get_contents($newFile);
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$address = "test#test.com";
$mail->AddAddress($address, "John Doe");
$mail->SetFrom(EMAIL_TEST_FROM);
$mail->AddReplyTo(EMAIL_TEST_REPLY);
$mail->Subject = $action." REGISTRATION ".$formName.$tld;
$mail->From = EMAIL_TEST;
// Very important: don't have lines for MsgHTML and AltBody
$mail->Body = file_get_contents($mailBodyTextFile);
// $mail->Body = $_POST["msg"]; //If using web mail form, use this line instead.
if($mail->Send()){
return true;
}
Try below code which works fine:
try {
$mail->AddAddress('jitpal#domain.com', 'Jit Pal');
$mail->SetFrom('testuser#domain.com', 'Test User');
$mail->Subject = "All machine's tests.";
$mail->Body = "All machine's tests working fine.";
$mail->Send();
echo "<br/>Message sent successfully...<br/><br/>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}

Categories