send email with attachment in CodeIgniter - codeigniter

Pdf attachment through email in CodeIgniter Mysource code here
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('test#gmail.com');
$this->email->to($useremail);
$this->email->cc('');
$this->email->subject('pdf');
$this->email->message($message);
$this->email->attach('public_html/invoicepdf/171.pdf');
$mailsucc = $this->email->send();
I tried with this but didnt work
$this->email->attach('public_html/invoicepdf/171.pdf');
And i also replace path with URL.

You can send attach file using
$this->email->attach($atch);
method in codeigniter. in this below code i'm sending mail using SMTP with
Attached File.
its Working perfectly.
You only need to specify base_url to define attachment file path
Controller
$email_config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'yourmail#gmail.com', // change it to yours
'smtp_pass' => 'mypasswords', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
// Defined Attachment file using variable
$atch=base_url().'html/images/logo.png';
$this->load->library('email', $email_config);
$this->email->set_newline("\r\n");
$this->email->from('test#gmail.com', 'Rudra'); // email From
$this->email->to('mailtosend#gmail.com'); // eMail to send
$this->email->subject('Mail with Attachment'); // Subject
$this->email->message("This is mail with Attachment file using CodeIgniter."); // Message
$this->email->attach($atch); // Attachment file defined using variable
$maill=$this->email->send(); // send mail
if($maill>0)
{
echo 'Email sent.'; // success
}
else
{
show_error($this->email->print_debugger());
}

Related

Why the email body is showing html source code when using CodeIgniter email library?

I am using the CodeIgniter email library to send emails with file attachments. However, everything working fine except the email body is showing the Html source code in the mailbox. Please help to sort out this problem.
Below is the Controller page
welcome.php
----------------------------
$this->load->library('upload');
$config = array(
'protocol' => 'sendmail',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxxxxxxxxx',
'smtp_pass' => 'xxxxxxxxxx',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE,
'priority' => '1'
);
$data['message']=$message;
// Upload file
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from($sendermail, $sendername);
$this->email->to($receivermail);
$this->email->subject($subject);
$this->email->message($this->load->view('email',$data,true));
$filename=null;
if (!empty($_FILES['attachment']['name']))
{
$files = $_FILES['attachment'];
$config_data['upload_path'] = 'uploads/';
$config_data['allowed_types'] = 'jpg|jpeg|png';
$_FILES['attachment']['name'] = time().'_'.$files['name'];
$filename=$_FILES['attachment']['name'];
$_FILES['attachment']['type'] = $files['type'];
$_FILES['attachment']['tmp_name'] = $files['tmp_name'];
$_FILES['attachment']['error'] = $files['error'];
$_FILES['attachment']['size'] = $files['size'];
$this->upload->initialize($config_data);
if ($this->upload->do_upload('attachment'))
{
$upload_data = $this->upload->data();
$this->email->attach($upload_data['full_path']);
}
}
// Upload file
$this->email->send();
enter code here
Below is the View page
email.php
--------------------
<html>
<head>
</head>
<body>
<?php echo $message;?>
</body>
</html>
you need to configure the content as HTML
$this->email->set_mailtype("html");
OR
$this->email->set_header('Content-Type', 'text/html');

i cant send mail codeigniter

I can't send mail from codeigniter to yandex mail when i using this code:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.yandex.com',
'smtp_port' => 465,
'smtp_user' => 'blablabla#yandex.com',
'smtp_pass' => 'blablabla',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'newline' => "\r\n"
);
$this->load->library('email');
$this->email->initialize($config);
$msg = $this->messages($f2, $f3);
$this->email->to($f1);
$this->email->subject('Here is Subject');
$this->email->message($msg);
if($this->email->send()){
echo "Masuk!";
}else{
echo "Gagal!";
}
echo $this->email->print_debugger();
And the result always says:
Gagal!Cannot send mail with no "From" header.
Cannot send mail with no "From" header.
You simply need to add a From address to your email, by calling
$this->email->from('your#example.com', 'Your Name');
or
$this->email->from('your#example.com');
Depending on the sending and receiving provider, that might get you in trouble though if you try and send the email under another From address, than the one you authenticated against the SMTP server with - it might get classified as spam then (or even rejected by the SMTP server right away) - so in this case here you should use blablabla#yandex.com as From.
you have to set a name and email as a sender.
for example if you set different email and name for sender it shows in inbox mail (Gmail for example) as like below image

Codeigniter 3: send account activation email fails

I am working on a Register and Login application with CodeIgniter 3 and Twitter Bootstrap.
When a user registers, an email should be send to the address he/she provided, with an account confirmation link. The problem is that the confirmation email does not send.
In the Usermodel I have:
public function activationEmail($first_name='', $last_name='', $email='', $verification_key='')
{
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.code-love.tk',
'smtp_port' => 465,
'smtp_user' => 'razvan#code-love.tk',
'smtp_pass' => '******',
'smtp_crypto' => 'ssl',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$messg = 'Wellcome, '. $first_name . ' ' . $last_name . '! Click the <strong>confirmation link</strong> to confirm your account.';
$this->email->initialize($config);
$this->email->set_newline('\r\n');
$this->email->from('mail.code-love.tk','Razvan Zamfir');
$this->email->to($email);
$this->email->subject('Account activation');
$this->email->message($messg);
if (!$this->email->send()) {
show_error($this->email->print_debugger());
}
else {
echo 'Your e-mail has been sent!';
}
}
In my Signup controller I have this code:
public function signup() {
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
$this->form_validation->set_rules('cpassword', 'Confirm password', 'required|matches[password]');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run()) {
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$email = $this->input->post('email');
$password = $this->input->post('password');
$verification_key = md5($email);
$date_created = date('Y-m-d H:i:s');
$date_updated = date('Y-m-d H:i:s');
$active = 0;
// Load user model
$this->load->model('Usermodel');
// If email does not already exist in the database
// signup new user
if (!$this->Usermodel->email_exists($email)) {
if ($this->Usermodel->user_register($first_name, $last_name, $email, $password, $verification_key, $date_created, $date_updated, $active) && $this->Usermodel->activationEmail($first_name, $last_name, $email, $verification_key)) {
$this->session->set_flashdata("signup_success", "Your account has just bean created. You must confirm it before you can sign in. We have send you a confirmation email at $email for this purpose.");
} else {
// unless sigup does not fail for whatever reason
$this->session->set_flashdata("signup_failure", "We ware unable to create your account.");
}
redirect('signup');
} else {
// If email is already in the database
// urge user to sign in (redirect to signup page too)
$this->session->set_flashdata("email_exists", "The email address $email already exists in the database. Please signin.");
redirect('signin');
}
} else {
$this->load->view('signup');
}
}
The sign up does happen, with the error below, and the verification email is not send. I am not doing this from a local XAMPP/WAMP/MAMP server, but from a "live" one, you can signup yourself.
Severity: Warning
Message: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Filename: libraries/Email.php
Line Number: 1950
Backtrace:
File: /home/roxoqdat/code-love.tk/ciauth/application/models/Usermodel.php
Line: 52
Function: send
File: /home/roxoqdat/code-love.tk/ciauth/application/controllers/Signup.php
Line: 42
Function: activationEmail
File: /home/roxoqdat/code-love.tk/ciauth/index.php
Line: 292
Function: require_once
What am I doing wrong?
Check if you have the required SMTP service.
Try with google SMTP service.
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('mygmail#gmail.com', 'myname');
$this->email->to('target#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$result = $this->email->send();
Next try with:
$config = array('mailtype' => 'html');
Remove the rest from $config = array.
First of all, after setting flashdata, I don't see you redirecting the user to where they can see it...
Also, send the mail first, then you set flash data as An Activation Link Has Been Sent To Your Email, Please Check Your Email To Activate Your Account!
So,
User Registers,
An email is sent to them only if it doesn't exist already,
Set flashdata for any errors that may arise... Eg:
Assign a variable to the email sending function:
$send_email = $this->activationEmail('params');
In your activationEmail function, make sure there is a simple control structure like this:
$success=$this->email->send();
if($success){ return true; } else{ return false; }
Now in your controller, compare to see if mail is actually sent:
if($send_email == true){ $this->session->flashdata('success','Your success message');
// use the redirect() helper function to the function that loads your view
} else { //set failed flashdata and redirect to where you want it seen like above}
You then will know where the problem is.
Let the program flow in your mind first, then implement it. Try that first and get back to me right after.
Hope it somehow helps
Debug whether your parameters are passing or not (specially last_name). Try to write the model method like this to avoid showing errors
public function activationEmail($first_name='', $last_name='', $email='', $verification_key='')
Try adding header to your email (You are setting mailtype twice, might be an issue).
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');

Where are the SMTP settings in CodeIgniter?

I am looking at someone else's code and need to find the SMTP details. He is using CodeIgniter however, I can not figure out where the SMTP config is being set.
For example, he is sending mail like this:
function sendMail{
$this->load->library('email');
$this->email->set_mailtype("html");
$this->email->from($this->config->item('from_email'), $this->config->item('from_name'));
$this->email->to(test#example.com);
$this->email->subject('Subject is here');
$message = "Hello";
$this->email->message($message);
$this->email->send();
return true;
}
I can not see where the config files are set. He does have the from_email item and from_name item configured in a custom config file, but that file contains only these two lines.
The default config.php does not contain anything smtp related..
Any ideas where could I find it?
Thanks!
From Codeigniter forum,
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'xxx',
'smtp_pass' => 'xxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// Set to, from, message, etc.
$result = $this->email->send();
Check this link
If you look in the config folder there might be a file called email.php into which default config items can be set. So they might be being set there.
These are called automatically when the email class is loaded, but can be overidden by setting them in the controller and initialising the class as described in the docs.
https://www.codeigniter.com/user_guide/libraries/email.html

Sending mail with SwiftMailer in Laravel 4

I am receiving a the following error:
Cannot send message without a recipient
This is while trying to send a email using swiftmailer. My code works in localhost and has all the parameters needed from sender to receiver but it throws an error saying it cannot send without recipient.
Here is my code:
public function email()
{
Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message){
$message = Swift_Message::newInstance();
$email = $_POST['email']; $name = $_POST['name']; $subject = $_POST['subject']; $msg = $_POST['msg'];
$message = Swift_Message::newInstance()
->setFrom(array($email => $name))
->setTo(array('name#gmail.com' => 'Name'))
->setSubject($subject)
->setBody($msg);
$transport = Swift_MailTransport::newInstance('smtp.gmail.com', 465, 'ssl');
//$transport->setLocalDomain('[127.0.0.1]');
$mailer = Swift_Mailer::newInstance($transport);
//Send the message
$result = $mailer->send($message);
if($result){
var_dump('worked');
}else{
var_dump('Did not send mail');
}
}
}
You can do this without adding your the SMTP information in your Mail::send() implementation.
Assuming you have not already, head over to app/config/mail.php and edit the following to suit your needs:
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your_username',
'password' => 'your_password',
Then your code should be as simple as:
public function email()
{
Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message)
{
$message->from( Input::get('email'), Input::get('name') );
$message->to('name#gmail.com', 'Name')->subject( Input::get('subject') );
});
}
So, hopefully the issue is one of configuration. Do you have other environments setup that might be over-riding the app/config/mail.php configuration settings in your server where it's not working?
Please make sure you have configured all the necessary configuration correctly on
/app/config/mail.php. Ensure all the configuration is correct for the environment where the email is not working correctly.
If you want to use your Gmail account as an SMTP server, set the following in app/config/mail.php:
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your-email#gmail.com',
'password' => 'your-password',
When switching to online server, you want to protect this file or switch provider so as not to expose your gmail credentials. Port 587 is for mailgun not gmail.

Resources