I have some problems, meaning that I don't receive mail from a form...
the code looks like this:
public function send_email(){
$this->load->library("form_validation");
$this->form_validation->set_rules("fullName", "Full Name", "required|alpha|xss_clean");
$this->form_validation->set_rules("email", "Email Address", "required|valid_email|xss_clean");
$this->form_validation->set_rules("message", "Message", "required|xss_clean");
if($this->form_validation->run() == FALSE){
$data["message"] = "";
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_comment", $data);
$this->load->view("site_footer");
}else{
$data["message"] = "The email was successfully been sent!";
$this->load->library("email");
$this->email->from(set_value("email"), set_value("fullName"));
$this->email->to("paulharbuz#gmail.com");
$this->email->subject("Message from our form");
$this->email->message(set_value("message"));
$this->email->send();
echo $this->email->print_debugger();
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_comment", $data);
$this->load->view("site_footer");
}
}
And i get an error that looks like this:
Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.**
Is this from my php.ini file?
i've set my files reading this part on http://php.net/manual/en/ref.mail.php
i found the solution: create a file in config: email.php that looks like this
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| EMAIL CONFING
| -------------------------------------------------------------------
| Configuration of outgoing mail server.
| */
$config['wordwrap'] = FALSE;
$config['mailtype'] = 'html';
$config['crlf'] = '\r\n';
$config['charset']='utf-8';
$config['newline']="\r\n";
$config['priority']=1;
$config['protocol']='smtp';
$config['smtp_host']='ssl://smtp.googlemail.com';
$config['smtp_port']='465';
$config['smtp_timeout']='30';
$config['smtp_user']='gcc.sandbox#gmail.com';
$config['smtp_pass']='qwaszx12';
$config['newline']="\r\n";
/* End of file email.php */
/* Location: ./system/application/config/email.php */
then load this in your main controller like: $this->email->initialize($this->config->config); and that's it!
simply add this code to you helper file and call the function when you want to send mail the following method to send an email...
function send_mail($to,$subject,$message) {
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx#xxxx',
'smtp_pass' => 'xxxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from($config['smtp_user'],"Your name");
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
$result = $this->email->send();
return $result;
}
$string = 'mail content';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: z' . "\r\n";
mail(email, 'subject', $string, $headers);
I want to add little more knowledge into it if you are using gmail so this cannot be done by your own password it can be done by using specific Google password . if you get error like require application-specific password.
than search it in Google and get one and type in the field below as i did.
$config['smtp_pass']='application-specific password';
Related
I have created a controller and made a test function in the controller to test if the email is sent or not.
I checked with different email addresses, but didn't succeed. This is my code example:
public function sendmail() {
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$user_id = 1;
$name = 'Mark Alan';
$this->email->set_newline("\r\n");
$this->email->from('info#domainname.com', "Test Name");
$this->email->to('test#domainname.com');
$this->email->subject('Test Mail');
$this->email->message('This is a test message');
$this->email->send();
echo $this->email->print_debugger();
}
With Codeigniter (assuming you have auto-loaded the email library) you can either set an email preference in a config file, named email.php and these preferences are loaded automatically from there. It could look like:
// Setting Email Preferences
$config['useragent'] = 'CodeIgniter'; // Mail engine switcher: 'CodeIgniter' or could be 'PHPMailer'
$config['protocol'] = 'smtp'; // 'mail', 'sendmail', or 'smtp'
$config['mailpath'] = '/usr/sbin/sendmail';
or like in your example, you can set them manually, but need to initialize it, so don't forget this line after defining your $config[] array:
$this->email->initialize($config);
Try this code:
function sendMail()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com', //Your Host
'smtp_port' => 465, // Add 25 port if you sending from your smtp mail server
'smtp_user' => 'xxx#gmail.com', // change it to yours, server email
'smtp_pass' => 'xxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = '';
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxx#gmail.com'); // change it to yours
$this->email->to('xxx#gmail.com');// change it to yours
$this->email->subject('Resume from JobsBuddy for your Job posting');
$this->email->message($message);
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}
}
Note: Please add your smtp port and your smtp email account detail.
Applying for a job need to send an email with some of the details like name,phonenumber,email,current ctc etcc..Email is sending correctly but the problem is while sending an email in subject line i need to include name email ctc...for this i have done this but it is not accepting in this format.
$name = $this->input->post('fullname');
$email = $this->input->post('email');
$phone = $this->input->post('mobilenumber');
$currentemploymentstatus = $this->input->post('current_employment_status');
//set to_email id to which you want to receive mails
$to_email = 'yyyy#gmail.com';
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'xxxxxx#gmail.com',
'smtp_pass' => 'PASSWORD123', //$from_email password
'mailtype' =>'html',
'newline' =>"\r\n",
'crlf' =>"\r\n",
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = array();
$message[] = 'Fullname : '.trim($name).' ';
$message[] = 'Email : '.trim($email).' ';
$message[] = 'Mobile : '.trim($phone).' ';
$message[] = 'Current Employment Status : '.trim($currentemploymentstatus).' ';
//$message = implode(PHP_EOL, $message);
$message = implode('<br>', $message);
//send mail
$this->load->library('email',$config);
$this->email->from($email);
$this->email->to($to_email);
//$list = array();
$this->email->subject($name|$email);
$this->email->message($message);
$this->email->set_newline("\r\n");
$this->email->set_mailtype("html");
if ($this->email->send())
{
$this->flash->success('Thank you for applying to this post we will get back to you soon!</div>');
redirect('apply');
}
else
{
$this->flash->success('There is error in sending mail! Please try again later');
redirect('apply');
}
}
Just Solved by adding this code.
$subject = $name .' | '.$currentemploymentstatus .' | '.$expectedctc .' | '.$primaryskills;
$this->email->subject($subject);
For your problem try changing "|" to .'|'
To clean up your code and to make your life easier:
If you are using CI, check out my repository on setting up an email_model to simplify the code you are using. Instead of loading the config stuff for the library all you will need to do is hit the model to send to email queue and hit the model a second time to process the queue
https://github.com/ddell003/Email_model
I already watch and read all videos and the questions related to the problem "how to send email in php from gmail using XAMPP?? " . It run but the execution in SENDMAIL.ini is not working well
img Problem #1
this pic shown, when I clicked the enter button, the localhost/ex2/here.php or the name of your php file will permanently load unless you click the X button on the cmd of sendmail.exe..
When I clicked the X button on the cmd of sendmail.exe, this message will shown on my php file img Problem #2
And this is my code in sending email from gmail:
<?php
$message = "This message will directly POSTED to your GMAIL account";
$headers = "From: rence.samboy#gmail.com";
if(mail('rence.samboy#gmail.com', 'Example', $message, $headers))
{
echo "Text message is sent to rence.samboy#gmail.com.....<BR/>";
}
else
{
echo "Not Work..";
}
?>
**Much appreciate if you can help me to fix my problem** :)
Well... for sending email, you need to set your gmail configuration first, such as protocol, smtp port, etc.
For me... I'm using CodeIgnitor framework and this code that I've added in my controller:
public function send_mail() {
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465, //or 587
'smtp_timeout' => 7,
'smtp_user' => 'xxx#gmail.com', //your gmail account
'smtp_pass' => 'xxx', //your gmail password
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE,
'newline' => "\r\n"
);
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$subject = $_POST["subject"];
$phone = $_POST["phone"];
$shipment_code = $_POST["shipment_code"];
$array = "Phone No: ".$phone."<br>Email: ".$email."<br>Shipment Code: ".$shipment_code."<br>Message: ".$message;
//Load email library
$this->load->library('email', $config);
$this->email->from('xxx#gmail.com');
$this->email->to('xxx#xxx.com');
$this->email->cc($email);
$this->email->subject($subject);
$this->email->message($array);
//Send mail
if($this->email->send())
$this->session->set_flashdata('email_sent',"<h2>Your email was sent successfully.</h2>");
else
$this->session->set_flashdata('email_sent',"Error in sending Email.");
echo $this->email->print_debugger();
$this->load->view('views_contactus');
}
If you want to see the view, then visit this.
php.ini
SMTP=smtp.gmail.com
smtp_port=465
sendmail_from = YOUR_MAIL#gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
sendmail.ini
smtp_server=smtp.gmail.com
smtp_port=465
error_logfile=error.log
debug_logfile=debug.log
auth_username=YOUR_MAIL#gmail.com
auth_password=YOUR_MAIL_PASS
force_sender=YOUR_MAIL#gmail.com
php.ini
extension=php_openssl.dll
I have application where I want to send mails to clients but i am getting error from server.
Controller code for sending mail
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.lmsweb.in';
$config['smtp_port'] = '25';
$config['smtp_user'] = 'support#lmsweb.in';
$config['smtp_pass'] = '****';
// $config['charset'] = 'iso-8859-1';
$this->email->initialize($config);
$this->email->from('support#lmsweb', 'LMS');
$this->email->to($this->input->post('txtvisitoremail'));
$this->email->subject('Client Login Details');
$this->email->subject('Congratulation');
$this->email->send();
echo $this->email->print_debugger();
redirect('telecaller/telecaller/update');
Same code works when using on localhost but on server I get this error.
After changing protocol to mail I get:
Message: mail() [function.mail]: SMTP server response: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
Filename: libraries/Email.php
Line Number: 1539
I then created a new test mail account, test#lmsweb.in and tried sending mail to it and it worked. So its clear that using mail protocol I cannot send mail to other like Gmail.
I used this code to send mail from server:
function sendEmail($data, $templateName) {
//echo "<pre>";
//print_r($data);
$this->load->library('email');
$this->email->set_mailtype('html');
$this->email->from($data['from_address']);
$this->email->to($data['to_address']);
//$this->email->cc($data['cc_address']);
$this->email->subject($data['subject']);
$body = '--------'
$this->email->message($body);
$this->email->send();
$this->email->clear();
//redirect('web_welcome/member_signup');
}
In this line you don't have valid address $this->email->from('support#lmsweb', 'LMS'); You are lacking .in: support#lmsweb .in
Anyway you can use the PHP Mailer class for sending emails from codeigniter. I've done it by putting the files class.phpmailer.php and class.smtp.php in third party folder and then creating custom library for mail sending:
class Mail_sender
{
protected $ci;
var $mail;
public function __construct()
{
include (APPPATH. 'third_party/class.phpmailer.php');
$this->ci =& get_instance();
$this->mail = new PHPMailer ();
$this->init();
}
public function init(){
$this->mail->IsSMTP ();
$this->mail->IsHTML(true);
$this->mail->Mailer = 'smtp';
$this->mail->SMTPAuth = false;
$this->mail->Host = 'smtp.t-home.mk'; // "ssl://smtp.gmail.com" didn't worked
$this->mail->Port = 25;
$this->mail->SMTPSecure = '';
$this->mail->SMTPAuth = true;
$this->mail->Username = 'user#example.com'; // SMTP username
$this->mail->Password = 'secret'; //SMTP password
$this->mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.
}
public function setUsername($username){
$this->mail->Username = $username;
}
public function setFrom($email, $sender_name) {
$this->mail->From = $email;
$this->mail->FromName = $sender_name;
}
public function setAddress($to){
$this->mail->addAddress ($to);
}
public function setReplyTo($replyTo)
{
$this->mail->AddReplyTo($replyTo);
}
public function setSubject($subject){
$this->mail->Subject = $subject;
return $this->mail->Subject;
}
public function setBody($messageBody){
$this->mail->Body = $messageBody;
}
public function sendMail(){
return $this->mail->Send();
}
}
And used a controller for sending mail:
$nameSurname=$this->input->post('name');
$email = $this->input->post('email');
$subject = $this->input->post('subject');
$temp_message = $this->input->post('message');
$message = 'FROM: '.$nameSurname.'('.$email.')<br><br>'.$temp_message;
$array = array(
'ime' => $nameSurname,
'mail' => $email,
'sub' => $subject,
'msg' => $message
);
$this->load->library('mail_sender');
$this->mail_sender->setFrom('[from address]','[title]');
$this->mail_sender->setSubject($subject);
$this->mail_sender->setAddress("[receiving address]");
$this->mail_sender->setBody($message);
$this->mail_sender->sendMail();
Hope it helps.
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'something#mail.com', // change it to yours
'smtp_pass' => '*******', // change it to yours
'wordwrap' => TRUE,
'crlf' => "\r\n",
'newline' => "\r\n" );
it is worked for me after adding last two lines ''crlf' => "\r\n",
'newline' => "\r\n"'
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();