问题
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();
回答1:
$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());
}
}
回答2:
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
回答3:
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.
回答4:
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.
回答5:
use path helper
$this->load->helper('path');
$path = set_realpath('./images/');
on email line
$this->email->attach($path . $your_file);
回答6:
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!";
}
回答7:
<?php
class Email extends CI_Controller
{
public Function index();
{
$config = Array(
'protocol' => 'smtp',
'smpt_host' => 'ssl://googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'example@gmail.com',
'smtp_pass' => 'yourpass'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('example@gmail.com');
$this->email->to('example@gmail.com');
$this->email->subject('This is a test email sending');
$this->email->message('This is some message, you can type your own');
if($this->email->send()
{
echo "Your email has been sent";
}else{
show_error($this->email->print_debugger());
}
}
?>
回答8:
This will help you in sending an email with attach file
private function sendEmail()
{
//Load email library
$this->load->library('email');
$this->load->helper('path');
$path = set_realpath('./uploads/');
//SMTP & mail configuration
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'yourEmail@gmail.com',
'smtp_pass' => 'yourPassword',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->to('yourEmail@gmail.com');
$this->email->from($_POST['email'],$_POST['name']);
$this->email->subject($_POST['subject']);
$this->email->message($_POST['message']);
foreach ($_FILES as $key => $file)
{
if ($file['error'] == 0)
{
$this->email->attach($file['tmp_name'], '', $file['name']);
}
}
$this->email->send();
}
来源:https://stackoverflow.com/questions/25416585/codeigniter-send-email-with-attach-file