I have a running CodeIgniter email configuration using office 365 SMTP TLS Encryption.
But my HTML message was broken
$email_message = [
'name' => "USERNAME",
'code' => "03940",
];
$message = $this->load->view('emails/code_email', $email_message, true);
$this->load->library('email');
$config["protocol"] = "smtp";
$config["smtp_host"] = "smtp.office365.com";
$config["smtp_port"] = "587";
$config[ "smtp_crypto"] = "tls";
$config["smtp_timeout"] = "7";
$config["smtp_user"] = "some#email.com";
$config["smtp_pass"] = "someemailpassword";
$config["charset"] = "utf-8";
$config["newline"] = "\r\n";
$config["mailtype"] = "html"; // or text
$config["validation"] = true;
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$this->email->from('some#email.com', 'Some Email');
$this->email->to('user#email.com');
$this->email->subject('Test Lang');
$this->email->message($message);
if ($this->email->send()) {
echo 'OKS';
} else {
echo $this->email->print_debugger();
}
This is what I got:
email i got
This what I expected to show:
email expected
Related
Here's my code for generating pdf and send it through email
$dompdf->set_paper($customPaper);
$dompdf->render();
ob_end_clean();
$file = $dompdf->output();
file_put_contents($pdfname,$file);
$name = 'sample';
$subject = 'sample subject';
$body = 'Hello '.$empname.' , here\'s your payslip';
if(!empty($email)){
$mail = new PHPMailer();
// smtp settings
$mail->isSMTP();
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "sample#gmail.com"; // gmail address
$mail->Password = "sample123"; // gmail password
$mail->Port = 587;
$mail->IsHTML(true);
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
// email settings
$mail->isHTML(true);
$mail->setFrom($email,$name);
$mail->addAddress($email);
$mail->Subject = ($subject);
$mail->Body = $body;
$mail->AddAttachment($pdfname); [check the picture][1]
if($mail->send()){
$status = "success";
$response = "Email is sent!";
} else {
$status = "failed";
$response = "Something is wrong: <br/>". $mail->ErrorInfo;
}
I want to put the generated pdf to the specific folder to be organized, because it just appear somewhere. thankyou very much for your help
I am using Amazon ses account to send email.
All the credentials are working fine in core php with class.phpmailer.php
But not working with codeigniter email class on the same server
And not showing me any error with echo $this->email->print_debugger();
Please take a look at code
core php code
//include phpmailer
require_once('class.phpmailer.php');
//SMTP Settings
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "email-smtp.us-west-2.amazonaws.com";
$mail->Username = "AKIAIF4U4***5J36ROIA";
$mail->Password = "AhI9Nn********GSo4u8r0xNK/OI";
//
$mail->SetFrom('donotreply#*****.com', 'medi***'); //from (verified email address)
$mail->Subject = "Email Subject"; //subject
//message
$body = "This is a test message vipul.";
$body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
//recipient
$mail->AddAddress("vipul.kumar#******.com", "Test Recipient");
//Success
if ($mail->Send()) {
echo "Message sent!"; die;
}
//Error
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
codeigniter code
public function test_aws_email($data=array()){
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'tls://email-smtp.us-west-2.amazonaws.com',
'smtp_port' => 465,
'smtp_user' => 'AKIAIF4U4***5J36ROIA',
'smtp_pass' => 'AhI9Nn********GSo4u8r0xNK/OI'
);
$config['crlf'] = "\r\n";
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from("donotreply#******.com", 'Medi***');
$this->email->to('vipul.kumar#******.com');
$this->email->subject('TEST FOR AWS ');
$this->email->message('AWS AUTHENTICATION CLEAR ');
$this->email->set_mailtype("html");
$this->email->send();
echo $this->email->print_debugger();
}
Here is my Main function
The user register successfully but don't receive any email.
public function register(){
$this->load->view("Home/home_header");
if(isset($_POST["user_register"])){
$this->form_validation->set_rules('name', 'First Name', 'alpha|htmlspecialchars|trim|required');
$this->form_validation->set_rules('username', 'Last Name', 'htmlspecialchars|trim|required');
$this->form_validation->set_rules('email', 'Email', 'valid_email|trim|required');
$this->form_validation->set_rules('confirm-mail', 'Confirm Email', 'valid_email|trim|required|matches[email]');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confirm-password', 'Confirm Password', 'required|matches[password]');
$this->form_validation->set_rules('address', 'Address', 'htmlspecialchars|trim|required');
$this->form_validation->set_rules('country', 'Country', 'required');
$this->form_validation->set_rules('male-female', 'Gender', 'required');
if($this->form_validation->run() == TRUE){
$status = 0;
$data = array();
$data["first-name"] = $_POST["name"];
$data["username"] = $_POST["username"];
$data["mail"] = $_POST["email"];
$data["confirm-mail"] = $_POST["confirm-mail"];
$data["password"] = hash('md5',$_POST["password"]);
$data["confirm-password"] = hash('md5',$_POST["confirm-password"]);
$data["address"] = $_POST["address"];
$data["country"] = $_POST["country"];
$data["male-female"] = $_POST["male-female"];
$data["status"] = $status;
$email = $_POST["email"];
$saltid = md5($email);
if($this->db->insert("register",$data)){
if( $this->User_functions->sendmail($email,$saltid)){
//echo 'Succesful';
redirect(base_url().login);
}else{
echo "Sorry !";
}
}
}
}
$this->load->view("Home/user_registration");
$this->load->view("Home/home_footer");
}
And Here's the mail function. I am getting user email from the input and store the user record to the database. Record stores successfully and page redirects to the login page. But the user don't get email on registration. Help me to resolve this issue.
function sendmail($email,$saltid){
// configure the email setting
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com'; //smtp host name
$config['smtp_port'] = '465'; //smtp port number
$config['smtp_user'] = '*******#gmail.com';
$config['smtp_pass'] = '***********'; //$from_email password
$config['mailtype'] = 'html';
//$config['charset'] = 'iso-8859-1';
//$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
//$this->email->initialize($config);
$this->load->library('email', $config);
$url = base_url()."user/confirmation/".$saltid;
$this->email->from('gulfpro354#gmail.com', 'GulfPro');
$this->email->to($email);
$this->email->subject('Please Verify Your Email Address');
$message = "<html><head><head></head><body><p>Hi,</p><p>Thanks for registration with DGaps.</p>
<p>Please click below link to verify your email.</p>".$url."<br/><p>Sincerely,</p><p>DGaps Team</p></body></html>";
$this->email->message($message);
return $this->email->send();
}
If I used any wrong code for this, also highlight that code.
Thanks
Ammar Khaliq
Please use :
echo $this->email->print_debugger();
after $this->email->send(); by remove return
It will show you the reason why you are not able to send an email
I have used this method of email.
public function sendmail($user_id, $email){
require("plugins/mailer/PHPMailerAutoload.php");
$mail = new PHPMailer;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$from = "example#gmail.com";
$from_label = "ABC";
$subject = "Please verify your email address.";
$url = base_url()."user/".$user_id."/".md5($user_id);
$message = "<p>Hi,</p><p>Thanks for registration with Portfolio Times.</p>
<p>Please click below link to verify your email.</p>".$url."<br/><p>Sincerely,</p><p>DGaps Team</p>";
$mail->setFrom($from,$from_label);
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//$mail->SMTPDebug = 2;
if( !$mail->send()){
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo "<div class='alert alert-success pull-right' style='width:400px' align='center'>".'×'."Email Sent Successfully...</div>";
//return true;
}
}
I am having problem with sending mail using phpmailer. I am a beginner programmer. I am trying to make contact form. My code is as follow(submit.php). Please suggest me.. Thanks in advance .
session_start();
require_once 'libs/phpmail/PHPMailerAutoload.php';
$errors = array();
if(isset($_POST['name'], $_POST['phone'],$_POST['mail'],$_POST['message'])){
$fields = array(
'name' => $_POST['name'],
'phone' => $_POST['phone'],
'email' => $_POST['mail'],
'message' => $_POST['message']
);
foreach ($fields as $field => $data) {
if(empty($data)){
$errors[] = 'The '. $field . ' field is required';
}
}
if(empty($errors)){
$m = new PHPMailer;
$m -> isSMTP();
$m -> SMTPAuth = true;
//$m -> SMTPDebug = 2;
$m -> Host = 'smtp.gmail.com';
$m -> Username = 'xxxx#gmail.com';
$m -> Password = 'xxxx';
$m -> SMTPSecure = 'ssl';
$m -> Port = 465;
$m -> isHTML();
$m -> Subject = 'Contact form submitted';
$m -> Body = 'From: ' . $fields['name']. '('. $fields['phone'] . $fields['email']. ')'.'<p>' .$fields['message'] .'</p> ';
$m -> FromName = 'Contact';
// $m ->addReplyTo($fields['email'], $fields['name']);
$m -> addAddress('ssss#gmail.com', 'xxxxxxxx');
if($m->send()){
header('Location: thanks.php');
die();
}else{
$errors[] = 'Sorry could not send email. Please try again';
}
}
}else{
$errors[] = 'some thing went wrong';
}
$_SESSION['error'] = $errors;
$_SESSION['field'] = $fields;
header('Location: form.php');
My setting phpmailer, everything works
function __construct ($to, $subject, $body) {
date_default_timezone_set('Etc/UTC');
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
$mail->CharSet = 'UTF-8';
//Set the hostname of the mail server
$mail->Host = "mail.xxxxxx.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->AuthType = 'PLAIN';
//Username to use for SMTP authentication
$mail->Username = "xxxx";
//Password to use for SMTP authentication
$mail->Password = "xxxx";
//Set who the message is to be sent from
$mail->setFrom('erp#xxxxxx.com');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($to);
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($body);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
$this->mail = $mail;
}
function SendMail () {
//send the message, check for errors
if (!$this->mail->send()) {
return "Mailer Error: " . $this->mail->ErrorInfo;
} else {
return true;
}
}
// using
$email = $this->request->getPost('email');
$smtp = new \SmtpClient($email, 'Test', $template);
$result = $smtp->SendMail();
Remove
$m -> Subject = 'Contact form submitted';
And try again.
When I remove the subject it worked.
I am trying to send email on codeigniter with attach file.
I always receive email successfully. However , I never receive with attach file. Below is code and highly appreciate for all comments.
$ci = get_instance();
$ci->load->library('email');
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.gmail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "test#gmail.com";
$config['smtp_pass'] = "test";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$ci->email->initialize($config);
$ci->email->from('test#test.com', 'Test Email');
$list = array('test2#gmail.com');
$ci->email->to($list);
$this->email->reply_to('my-email#gmail.com', 'Explendid Videos');
$ci->email->subject('This is an email test');
$ci->email->message('It is working. Great!');
$ci->email->attach( '/test/myfile.pdf');
$ci->email->send();
$this->email->attach()
Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function multiple times. For example:
public function setemail()
{
$email="xyz#gmail.com";
$subject="some text";
$message="some text";
$this->sendEmail($email,$subject,$message);
}
public function sendEmail($email,$subject,$message)
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'abc#gmail.com',
'smtp_pass' => 'passwrd',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('abc#gmail.com');
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);
$this->email->attach('C:\Users\xyz\Desktop\images\abc.png');
if($this->email->send())
{
echo 'Email send.';
}
else
{
show_error($this->email->print_debugger());
}
}
i have this problem before , the problem with the path file , so i change the path file to
$attched_file= $_SERVER["DOCUMENT_ROOT"]."/uploads/".$file_name;
$this->email->attach($attched_file);
And it works fine with me
With Codeigniter 3.1.0 I had same problem. Seems that there is missing a "\r\n":
Content-Type: application/pdf; name="test.pdf"<br>
Content-Disposition: attachment;<br>
Content-Transfer-Encoding: base64<br>
JVBERi0xLjYNJeLjz9MNCjQzNyAwIG9iag08PC9MaW5lYXJpemVkIDEvTCA3OTUyMTYvTyA0Mzkv<br>
RSA2ODEwODcvTiA0L1QgNzk0ODA3L0ggWyA1NjQgMjYxXT4+DWVuZG9iag0gICAgICAgICAgICAg<br>
should be:
Content-Type: application/pdf; name="test.pdf"<br>
Content-Disposition: attachment;<br>
Content-Transfer-Encoding: base64<br>
<br>
JVBERi0xLjYNJeLjz9MNCjQzNyAwIG9iag08PC9MaW5lYXJpemVkIDEvTCA3OTUyMTYvTyA0Mzkv<br>
RSA2ODEwODcvTiA0L1QgNzk0ODA3L0ggWyA1NjQgMjYxXT4+DWVuZG9iag0gICAgICAgICAgICAg<br>
I changed line 725 in system/libraries/Email from
'content' => chunk_split(base64_encode($file_content)),<br>
to
'content' => "\r\n" . chunk_split(base64_encode($file_content)),<br>
It works for me, but not the perfect fix.
Try putting the full path in $ci->email->attach();
On windows this would like something like
$ci->email->attach('d:/www/website/test/myfile.pdf');
This method has worked well for me in the past.
If you want to send attachment in email Without uploading file on server, please refer below.
HTML View file
echo form_input(array('type'=>'file','name'=>'attach_file','id'=>'attach_file','accept'=>'.pdf,.jpg,.jpeg,.png'));
Controller file
echo '<pre>'; print_r($_FILES); shows below uploaded data.
[attach_file] => Array
(
[name] => my_attachment_file.png
[type] => image/png
[tmp_name] => C:\wamp64\tmp\php3NOM.tmp
[error] => 0
[size] => 120853
)
We will use temporary upload path [tmp_name] where attachment is uploaded, because we DO NOT want to upload attachment file on server.
$this->email->clear(TRUE); //any attachments in loop will be cleared.
$this->email->from('your#example.com', 'Your Name');
$this->email->to('someone#example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
//Check if there is an attachment
if ( $_FILES['attach_file']['name']!='' && $_FILES['attach_file']['size'] > 0 )
{
$attach_path = $_FILES['attach_file']['tmp_name'];
$attach_name = $_FILES['attach_file']['name'];
$this->email->attach($attach_path,'attachment',$attach_name);
}
$this->email->send();
use path helper
$this->load->helper('path');
$path = set_realpath('./images/');
on email line
$this->email->attach($path . $your_file);
here i am using phpmailer to send mail
here full code is mention below
$this->load->library('My_phpmailer');
$mail = new PHPMailer();
$mailBody = "test mail comes here2";
$body = $mailBody;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1;// enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true;// enable SMTP authentication
$mail->Host = "ssl://smtp.gmail.com"; // sets the SMTP server
$mail->Port = 465;// set the SMTP port for the GMAIL server
$mail->Username = "YourAccountIdComesHere#gmail.com"; // SMTP account username
$mail->Password = "PasswordComesHere";// SMTP account password
$mail->SetFrom('SetFromId#gmail.com', 'From Name Here');
$mail->AddReplyTo("SetReplyTo#gmail.com", "Reply To Name Here");
$mail->Subject = "Mail send by php mailer";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment($cdnStorage . '/' . $fileName);
$address ='WhomeToSendMailId#gmail.com';
$mail->AddAddress($address, "John Doe");
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Here Is Full Source Code
$validation_rules = array(
array('field' => 'name', 'rules' => COMMON_RULES),
array('field' => 'email', 'rules' => COMMON_RULES),
array('field' => 'message', 'rules' => COMMON_RULES),
);
$this->validation_errors($validation_rules);
$name = $this->input->post('name');
$email = $this->input->post('email');
$message = $this->input->post('message');
$this->load->library('email');
//upload file
$attachment_file = "";
if (!empty($_FILES) && isset($_FILES["attachment_file"])) {
$image_name = $_FILES["attachment_file"]['name'];
$ext = pathinfo($image_name, PATHINFO_EXTENSION);
$new_name = time() . '_' . $this->get_random_string();
$config['file_name'] = $new_name . $ext;
$config['upload_path'] = "uploads/email/";
$config['allowed_types'] = "*";
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('attachment_file')) {
$finfo = $this->upload->data();
$attachment_file = base_url() . 'uploads/email/' . $finfo['file_name'];
} else {
$error = $this->upload->display_errors();
$this->msg = $error;
$this->_sendResponse(5);
}
}
$this->email->from($email, "$name")
->to("example#gmail.com")
->subject("FeedBack From $name")
->message($message)
->attach($attachment_file);
if ($this->email->send()) {
// temp pass updated.
$this->msg = "Email send successfully.";
$this->_sendResponse(1);
} else {
$this->msg = "Internal server error/Something went wrong.";
$this->_sendResponse(0);
}
$this->load->library('email'); // Loading the email library.
$this->email->clear(TRUE);
$this->email->from($user_email, $name);
$this->email->to('email#gmail.com');
$this->email->subject("Some subject");
$this->email->message("Some message");
if($_FILES['userfile']['name']!='' && $_FILES['userfile'['size'] > 0){
$attach_path = $_FILES['userfile']['tmp_name'];
$attach_name = $_FILES['userfile']['name'];
$this->email->attach($attach_path,'attachment',$attach_name);
}
$this->email->send();