php Codeigniter 发送带有附件的电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25416585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter send email with attach file
提问by mgphyo zaw
I am trying to send email on codeigniter with attach file.
我正在尝试使用附件文件在 codeigniter 上发送电子邮件。
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'] = "[email protected]";
$config['smtp_pass'] = "test";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";
$ci->email->initialize($config);
$ci->email->from('[email protected]', 'Test Email');
$list = array('[email protected]');
$ci->email->to($list);
$this->email->reply_to('[email protected]', '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();
回答by Nitheesh K P
$this->email->attach()
$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:
使您能够发送附件。将文件路径/名称放在第一个参数中。注意:使用文件路径,而不是 URL。对于多个附件,多次使用该功能。例如:
public function setemail()
{
$email="[email protected]";
$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' => '[email protected]',
'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('[email protected]');
$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());
}
}
回答by Ahmed Al Bermawy
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);
$attched_file= $_SERVER["DOCUMENT_ROOT"]."/uploads/".$file_name;
$this->email->attach($attched_file);
And it works fine with me
它对我来说很好用
回答by Torsten Arendrup
With Codeigniter 3.1.0 I had same problem. Seems that there is missing a "\r\n":
使用 Codeigniter 3.1.0 我遇到了同样的问题。似乎缺少一个“\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
我将 system/libraries/Email 中的第 725 行从
'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.
它对我有用,但不是完美的解决方案。
回答by dangermark
Try putting the full path in $ci->email->attach();
尝试将完整路径放入 $ci->email->attach();
On windows this would like something like
在 Windows 上,这会类似于
$ci->email->attach('d:/www/website/test/myfile.pdf');
This method has worked well for me in the past.
这种方法过去对我很有效。
回答by topimiring
use path helper
使用路径助手
$this->load->helper('path');
$path = set_realpath('./images/');
on email line
在电子邮件线上
$this->email->attach($path . $your_file);
回答by Amit Joshi
here i am using phpmailer to send mail
here full code is mention below
在这里我使用 phpmailer 在
这里发送邮件完整的代码在下面提到
$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 = "[email protected]"; // SMTP account username
$mail->Password = "PasswordComesHere";// SMTP account password
$mail->SetFrom('[email protected]', 'From Name Here');
$mail->AddReplyTo("[email protected]", "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 ='[email protected]';
$mail->AddAddress($address, "John Doe");
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
回答by Saddam Khan
<?php
class Email extends CI_Controller
{
public Function index();
{
$config = Array(
'protocol' => 'smtp',
'smpt_host' => 'ssl://googlemail.com',
'smtp_port' => 465,
'smtp_user' => '[email protected]',
'smtp_pass' => 'yourpass'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('[email protected]');
$this->email->to('[email protected]');
$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());
}
}
?>