How to format my email php mailer - html

When I send the email with html tags.Gmail shows the tags also.Why is that? any solution?how to ad images bold text colored text according to my code?
here is my email content code
smtpmailer("$email", 'website#yahoo.com', '<html><body>website.lk Password recovery', 'Password recovery','Dear "'.$name."\"\n\nUse your new password to login and reset your password as you wish.\nTo reset password go to your \"My Account\" page and click \"Change my password\"\n\n"."Here is your new password:\n\n"."Password: "."$password"."\n\nBack to get bump: www.website.lk\n\nRegards,\n website.lk Admin\n</body></html>");
$reset_msg="Recovery completed. Check your e-mail !";
}else{
$reset_msg="Error in sending email.Try again !";
}

By including the below headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
Source,
http://php.net/manual/en/function.mail.php
if you are using PHP Mailer,
$mail->MsgHTML($body);
$mail->AddAttachment("images/image1.gif");
$mail->AddAttachment("images/image2.gif");

if you are using this function:
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
Then change the line of $mail->Body = $body; to code blow:
$mail->MsgHTML($body);
This change will allow you to send HTML emails via PHPMailer, You can also add $mail->CharSet = 'UTF-8'; to avoid future problems with special characters ...

Use $mail->IsHTML(ture);
If on phpmailer..

Related

Output ('',S); breaking code with Mpdf and PHPmailer

I am trying to set up sending a pdf attachment with mPDF and PHPMailer.
mPDF is woking fine when output is download to browser, the minute I change it to Output('',S) the code stops working with Error 500 and I have tried different versions of the mailer code with no luck. I cant find any error with the syntax and all online tutorials seem to agree with the code. I am stumped
My code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once __DIR__ . '/vendor/autoload.php';
// Grab variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$mpdf = new \Mpdf\Mpdf();
$data = "";
$data .= "<h1>Testing this</h1>";
//Add Data
$data .='<strong>First Name</strong> '. $fname.'<br>';
$data .='<strong>Last Name</strong> '. $lname.'<br>';
$data .='<strong>Email</strong> '. $email .'<br>';
if($message)
{
$data .='<br/><strong>Message</strong><br/> '. $message.'<br>';
}
//WritePdf
$mpdf->WriteHtml($data);
//output to bowser
$pdf = $mpdf->output('',S);
$enquirydata = [
'First Name' => $fname,
'Last Name' => $lname,
'Email' => $email,
'Message' => $message
];
sendEmail($pdf,$enquirydata);
function sendEmail($pdf, $enquirydata)
{
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'in-v3.mailjet.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('myemails#gmail.com', 'Test Form');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
// Attachments
// 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}";
}
}
?>
If you are in a localhost, comment the line with the validation $email->isSMTP();
It works for me in my local.

How to Make Forgot Password in Codeigniter, Password send in email

Hello guyz i have create small kind of application in codeigniter,in this application i am doing forgot password module, i have created a function but dont know why its not working, i need random password have to been send in mail which will be autogenerated , but email method does not work, so give me some suggestion.
Here is My view:
<form action="<?php echo base_url() . "welcome/forgotpassword" ?>" method="POST">
<div class="form-group has-feedback">
<input type="email" class="form-control" placeholder="Email" name="user_email" />
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-4">
<input type="submit" class="btn btn-primary btn-block btn-flat" value="Send">
</div>
</div>
</form>
Here is my Controller:
public function forgotpassword(){
$email = $this->input->post('user_email');
$findemail = $this->main_model->ForgotPassword($email);
$this->load->view('forgotpassword');
if ($findemail) {
$this->main_model->sendpassword($findemail);
} else {
$this->session->set_flashdata('msg', 'Email not found!');
}
}
Here is My model:
public function sendpassword($data) {
$email = $data['user_email'];
print_r($data);
$query1 = $this->db->query("SELECT * from user_registration where user_email = '" . $email . "'");
$row = $query1->result_array();
if ($query1->num_rows() > 0) {
$passwordplain = "";
$passwordplain = rand(999999999, 9999999999);
$newpass['user_password'] = md5($passwordplain);
$this->db->where('user_email', $email);
$this->db->update('user_registration', $newpass);
$mail_message = 'Dear ' . $row[0]['full_name'] . ',' . "\r\n";
$mail_message .= 'Thanks for contacting regarding to forgot password,<br> Your <b>Password</b> is <b>' . $passwordplain . '</b>' . "\r\n";
$mail_message .= '<br>Please Update your password.';
$mail_message .= '<br>Thanks & Regards';
$mail_message .= '<br>Your company name';
require FCPATH . 'assets/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = "tls";
$mail->Debugoutput = 'html';
$mail->Host = "ssl://smtp.googlemail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "xxxxxxxxx#gmail.com";
$mail->Password = "xxxxxxxx";
$mail->setFrom('xxxxxxx#gmail.com', 'admin');
$mail->IsHTML(true);
$mail->addAddress('user_email', $email);
$mail->Subject = 'OTP from company';
$mail->Body = $mail_message;
$mail->AltBody = $mail_message;
if (!$mail->send()) {
$this->session->set_flashdata('msg', 'Failed to send password, please try again!');
} else {
echo $this->email->print_debugger();
$this->session->set_flashdata('msg', 'Password sent to your email!');
}
}
}
This function may have something to do with it... can you show us that?
$findemail = $this->main_model->ForgotPassword($email);
When you use print_r($data) is anything returned?
If not, $query1 will be 0 or null and everything will break.
// core function
public function sendpassword($data) {
// include your libary at the top
require FCPATH . 'assets/PHPMailer/PHPMailerAutoload.php';
// email retrieved from the ForgotPassword() method.
$email = $data['user_email'];
// get the user_info array row
$query1 = $this->db->query("SELECT * from user_registration where user_email = '" . $email . "'");
$row = $query1->result_array();
if ($query1->num_rows() > 0) {
// assign users name to a variable
$full_name = $row['full_name'];
// generate password from a random integer
$passwordplain = rand(999999999, 9999999999);
// encrypt password
$encrypted_pass = $this->pass_gen($passwordplain);
$newpass['user_password'] = $encrypted_pass;
// update password in db
$this->db->where('user_email', $email);
$this->db->update('user_registration', $newpass);
// begin email functions
$result = $this->email_user($full_name, $email, $passwordplain);
echo $result;
}
}
// email sending
public function email_user($full_name, $email, $passwordplain) {
// compose message
$mail_message = 'Dear ' . $full_name. ',' . "\r\n";
$mail_message .= 'Thanks for contacting regarding to forgot password,<br> Your <b>Password</b> is <b>' . $passwordplain . '</b>' . "\r\n";
$mail_message .= '<br>Please Update your password.';
$mail_message .= '<br>Thanks & Regards';
$mail_message .= '<br>Your company name';
// email config
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = "tls";
$mail->Debugoutput = 'html';
$mail->Host = "ssl://smtp.googlemail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "xxxxxxxxx#gmail.com";
$mail->Password = "xxxxxxxx";
$mail->setFrom('xxxxxxx#gmail.com', 'admin');
$mail->IsHTML(true);
$mail->addAddress('user_email', $email);
$mail->Subject = 'OTP from company';
$mail->Body = $mail_message;
$mail->AltBody = $mail_message;
// send the mail
if (!$mail->send()) {
return $this->email->print_debugger();
$this->session->set_flashdata('msg', 'Failed to send password, please try again!');
} else {
return $this->email->print_debugger();
$this->session->set_flashdata('msg', 'Password sent to your email!');
}
}
// Password encryption
public function pass_gen($password) {
$encrypted_pass = md5($password);
return $encrypted_pass;
}
$query1 = $this->db->query("SELECT * from user_registration where user_email = '" . $email . "'");
it is has sql injection!

phpmail gmail Could not connect to SMTP

Tried using the code below which has been working for years until last week. Some change on Gmail side perhaps. But its just not working even if various combinations are tried.
Allow unsafe apps is ON still unable to connect SMTP
<?php
sendMail("tosomeid#gmail.com", "fromsomeid#gmail.com", "test", "test msg<br>hello!");
function sendMail($to, $from, $subject, $message) {
try {
//$to='tosomeid#gmail.com';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From:' . $from . ' <fromsomeid#gmail.com>' . "\r\n";
ini_set("sendmail_from", "fromsomeid#gmail.com");
require_once("class.phpmailer.php");
require_once("class.smtp.php");
set_time_limit(240);
$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAutoTLS = false;
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 4;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 587; // or 587 // set the SMTP port for the GMAIL server
$mail->Username = "fromsomeid#gmail.com"; // SMTP account username
$mail->Password = "password123"; // SMTP account password
$mail->From = "fromsomeid#gmail.com";
$mail->SMTPSecure = 'tls';
$mail->AddAddress("tosomeid#gmail.com");
$mail->SetFrom('fromsomeid#gmail.com', $from);
$mail->AddReplyTo("fromsomeid#gmail.com", $from);
$mail->AddBCC("tosomeid#gmail.com");
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->WordWrap = 50;
echo 'sendMail to=>' . $to;
if (!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo '<br>Message was sent successfully to selected recipients.';
}
} catch (Exception $ex) {
echo'EXCEPTION <br>';
echo '<br>Caught exception: ' . $ex->getMessage() . "\n".$ex->getTraceAsString();
}
}
?>
You add SMTPOption to config Phpmailer
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);

Problems with sending HTML Email with PDF attachment using PHPMailer

Please help me, i'm trying to send an email with an HTML body and a PDF-file attached. I'm using PHPMailer. I tried a lot but all I get in my mail is this:
--b1_422917e00bd74f108a49b5d3d858e74d Content-Type: text/html; charset = "iso-8859-1" Content-Transfer-Encoding: 8bit Hello World --b1_422917e00bd74f108a49b5d3d858e74d Content-Type: application/pdf; name="factuur.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="factuur.pdf" JVBERi0xLjQKJcfsj6IKMTEgMCBvYmoKPDwvTGVuZ3RoIDEyIDAgUi9GaWx0ZXIgL0ZsYXRlRGVj b2RlPj4Kc3RyZWFtCnic7VtZs9zEFa5A2CYpQyALSUiiNyQqI3pfeGOrOECgMJc8BPJg38Xbta8x NqbyL/Jn85yvF6mPpNbMXMoPVCrlskvT6j59+izfWdT+pmG9UA0Lf4aH43ubt69x2dz8dsOb8Ofh zY2zrGfeNU7yXmvf3Ns4wXrDxDhiHes50431vGdGlt8K741pjjfDiMEMbZuBgNHhp2qGLYbfxxvP fa+9Gke8CTOG9flXoT8MZAaG1QOD8yMcb87e2sieCwXST3DUj/D3Tj7ytb/8bx751uaLzTcNV1r3 0kVtcy5Vr3ijmeqNwFzfc+51tgLVfHCx+XwT7SMIRbMgsUAZkzkPlFygBCq2V83D00jfOZAUJtE3 eHamwSoBJhvJlOidzfR1or97iXAWm/i8xBy0xHLZDytsfYXoPV2hNes9z0tcWgKxKq5NMA8RVJCn QVzO6UjKaJxb6XTyJCa4yzcb1XNjjI0D9Pn4XvPeUdjBQ3Y9k841R2cb7OydlyJx1kBtTirbWA19 YuHRvc1X7U+6LYxJYR5vn+m2CprG6vbZbivCI7PtT8MM47znrn2uc2CTG9s+3215mKAUBiMJZYxv X+gkJoSnFzsMYnPVvtSJXjGtbfv1/W4re8cEF+3POg6FCWfan3cKTwxrvn4YqFrGNMd7Ft5z314Z 17+cGBROta9gI4ktOZ7+efQRzi5gN5CshLTL2fX87Er3GvYYz34NNITygrfXw3G5sKq9EQ6jpZRK thfkmY6X2ffJ6N2Os17ClNsmjAqDc6j2S+zhlJKifRSY98wb1z4Mj5Jr355CoFYLjB0noo6Z9lai et cetera..
I'm using this code:
$message = "<b>Hello World</b>";
$fromname = "Tester";
$subject = "Test - 3";
$headers = "Content-Type:text/html\n";
$to = "test#test.nl";
$attachment = $_SERVER['DOCUMENT_ROOT'] . "/facturen/test.pdf";
$mail = new PHPMailer();
$body = preg_replace('/\[\]/',"",$message);
$mail->IsSMTP();
$mail->Host = "smtp.test.nl";
$mail->SMTPAuth = true;
$mail->Host = "smtp.test.nl";
$mail->Port = 25;
$mail->Username = $user;
$mail->Password = $pass;
$mail->IsHTML(true);
$mail->SetFrom($user, $user);
$mail->FromName = $fromname;
$mail->Subject = $subject;
$mail->Body = $body;
$mail->addCustomHeader($headers);
$address = $to;
$mail->AddAddress($address, $address);
$mail->AddAttachment($attachment, "factuur.pdf", $encoding = 'base64', $type = 'application/pdf');
if(!$mail->Send()) {
echo "Fout";
} else {
echo "Goed";
}
Anyone an idea how to resolve this?
try removing:
$body = preg_replace('/\[\]/',"",$message);
and
$headers = "Content-Type:text/html\n";

sending html in php not working

// Assign contact info
$name = stripcslashes($_POST['name']);
$emailAddr = stripcslashes($_POST['email']);
$issue = stripcslashes($_POST['issue']);
$comment = stripcslashes($_POST['message']);
$subject = stripcslashes($_POST['subject']);
// Set headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: $name <$emailAddr>";
$headers .= "\r\n";
$headers .= "Reply-To: $emailAddr";
// Format message
$contactMessage =
"<div>
<p><strong>Name:</strong> $name <br/>
<strong>E-mail:</strong> $emailAddr <br/>
<p><strong>Message:</strong> $comment </p>
<p><strong>Sending IP:</strong> $_SERVER[REMOTE_ADDR]<br/>
<strong>Sent via:</strong> $_SERVER[HTTP_HOST]</p>
</div>";
// Send and check the message status
$response = (mail('myemail#myserver.com', $subject, $contactMessage, $headers) ) ? "success" : "failure" ;
$output = json_encode(array("response" => $response));
header('content-type: application/json; charset=utf-8');
echo($output);
`
what is wrong with the above code? i do not get html email from it
after i changed Header From part i only get plain text emails
Change
$headers = "From: $name <$emailAddr>";
To
$headers .= "From: $name <$emailAddr>"; // you forgot to concatenation previous headers.