This is my code to send HTML email but its not working properly. Just raw html code is shown in the email.
$emailID = $this->input->post('email');
$name = $this->input->post('name');
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['mailtype'] = 'html';
$config['crlf'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['validate'] = FALSE;
$this->email->set_newline("\r\n");
$this->email->from($emailID,$name);
$this->email->to('vishvajitmahida009#gmail.com');
$this->email->subject('Email Testing');
$this->email->message('from clientside mail');
$this->email->send();
$message = $this->load->view('emailfile/file','',TRUE);
$this->load->library('email', $config);
$this->email->from($emailID,$name);
$this->email->to($emailID,$name);
$this->email->subject('Email Testing');
$this->email->message($message);
$this->email->send();
//echo $this->email->print_debugger();
Add $this->email->initialize($config);:
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['mailtype'] = 'html';
$config['crlf'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['validate'] = FALSE;
$this->email->initialize($config);
Related
I have a PHP code connected to mysql database. I want to send user login data from the postman tool and receive it in the php code and then return it to the result
The problem is that when sending data do not get the result
PHP CODE
<?php
$response = array();
header("Content-type: application/json; charset=utf-8");
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
include 'db/db_connect.php';
include 'functions.php';
//Get the input request parameters
$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE); //convert JSON into array
//Check for Mandatory parameters
if(isset($input['username']) && isset($input['password']) ){
$username = $input['username'];
$password = $input['password'];
$query = "SELECT full_name , password_hash , salt , email , id_con , born , city , country و card_id ,cardstatus, cardvalue FROM member WHERE username = ?";
if($stmt = $con->prepare($query)){
$stmt->bind_param("s",$username);
$stmt->execute();
$stmt->bind_result($fullName , $passwordHashDB ,$salt, $email , $countryid , $born , $city , $country ,$cardid ,$cardstatus, $cardvalue);
if($stmt->fetch()){
//Validate the password
if(password_verify(concatPasswordWithSalt($password,$salt),$passwordHashDB)){
$response["status"] = 0;
$response["message"] = "Login successful";
$response["full_name"] = $fullName;
$response["card_id"] = $cardid;
$response["email"] = $email;
$response["id_con"] = $countryid;
$response["country"] = $country;
$response["city"] = $city;
$response["born"] = $born;
$response["cardstatus"] = $cardstatus;
$response["cardvalue"] = $cardvalue;
}
else{
$response["status"] = 1;
$response["message"] = "Invalid username and password combination";
}
}
else{
$response["status"] = 1;
$response["message"] = "Invalid username and password combination";
}
$stmt->close();
}
}
else{
$response["status"] = 2;
$response["message"] = "Missing mandatory parameters";
}
//Display the JSON response
echo json_encode($response);
?>
SCREENSHOT
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!
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
)
);
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";
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..