Php 使用CodeIgniter通过Gmail发送电子邮件时出现问题?
我正试图通过我的Gmail帐户在codeigniter中发送一封电子邮件。我当前的代码如下所示:Php 使用CodeIgniter通过Gmail发送电子邮件时出现问题?,php,email,codeigniter,gmail,Php,Email,Codeigniter,Gmail,我正试图通过我的Gmail帐户在codeigniter中发送一封电子邮件。我当前的代码如下所示: $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.googlemail.com', 'smtp_port' => 465, 'smtp_user' => 'me@gmail.com', 'smtp_pass' => 'my_gmail_pa
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'me@gmail.com',
'smtp_pass' => 'my_gmail_password',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('me@gmail.com', 'Me');
$this->email->to("me@gmail.com");
$this->email->subject('A test email from CodeIgniter using Gmail');
$this->email->message("A test email from CodeIgniter using Gmail");
$this->email->send();
但是,这给了我以下错误:
Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.googlemail.com:465 (Connection timed out)
Filename: libraries/Email.php
Message: fwrite(): supplied argument is not a valid stream resource
Message: fgets(): supplied argument is not a valid stream resource
如果您能帮助解决此问题,我们将不胜感激!谢谢
注意:我可以向您保证,我的Gmail电子邮件和密码是正确的错误意味着您无法连接您输入的SMTP地址 您应该使用:smtp.gmail.com 请查看以下链接以供参考:
您可以下载一个由“肖恩·麦库尔”编写的漂亮的小火花软件包,该软件包使用“斯威夫特·梅勒”。 查看他的站点以获取安装说明 下面是我使用干式方法发送电子邮件的配置(需要PHP5.3.0!) 配置 |--mailer[dot]php
$config['swift_email_server'] = 'ssl://smtp.googlemail.com';
$config['swift_email_port'] = 465;
$config['swift_email_username'] = '######';
$config['swift_email_password'] = '######';
图书馆
|--我的电子邮件
控制器
|--我的控制器
class MY_Controller extends MX_Controller{
protected $user;
protected $swiftMailer = NULL;
public function __construct(){
parent::__construct();
$this->user = ($this->session->userdata('uid'))
? Member::find($this->session->userdata('uid')) : array();
$this->setup_profile();
$this->form_validation->set_error_delimiters('<p class="form-error">', '</p>');
}
public function setup_profile(){
$sections = array(
'config' => FALSE,
'http_headers' => FALSE,
'queries' => FALSE,
'memory_usage' => FALSE
);
$this->output->set_profiler_sections($sections);
if(ENVIRONMENT === 'development'){
$this->output->enable_profiler(TRUE);
}
}
protected function prep_email($data, $template){
$message =
Swift_Message::newInstance('Type your heading here...')
->setFrom($data['email_address'])
->setTo(array($this->config->item('primary_email'), $data['email_address']))
->setBody( $this->email->send_mail($data, $template))
->setContentType('text/html');
return ( $this->swiftMailer->send($message) );
}
}
希望这有帮助 也许这与你的网络配置有关,我只是一字不差地复制了这段代码,它工作得很好……我记得Gmail和CodeIgniter也有类似的问题。不幸的是,我没能解决它+1.如果您自己找到了问题的解决方案,请发布一个答案。您的服务器上的端口465是打开的?不幸的是,这没有什么区别,谢谢您的帮助。所以,请澄清一下,
'smtp\u host'=>ssl://smtp.gmail.com“
不起作用?
class MY_Controller extends MX_Controller{
protected $user;
protected $swiftMailer = NULL;
public function __construct(){
parent::__construct();
$this->user = ($this->session->userdata('uid'))
? Member::find($this->session->userdata('uid')) : array();
$this->setup_profile();
$this->form_validation->set_error_delimiters('<p class="form-error">', '</p>');
}
public function setup_profile(){
$sections = array(
'config' => FALSE,
'http_headers' => FALSE,
'queries' => FALSE,
'memory_usage' => FALSE
);
$this->output->set_profiler_sections($sections);
if(ENVIRONMENT === 'development'){
$this->output->enable_profiler(TRUE);
}
}
protected function prep_email($data, $template){
$message =
Swift_Message::newInstance('Type your heading here...')
->setFrom($data['email_address'])
->setTo(array($this->config->item('primary_email'), $data['email_address']))
->setBody( $this->email->send_mail($data, $template))
->setContentType('text/html');
return ( $this->swiftMailer->send($message) );
}
}
$this->form_validation->CI =& $this;
$this->form_validation->set_rules($config);
if($this->form_validation->run($this)){
$service = Service::find($this->input->post('service'));
if((int)$this->input->post('num') > 1){
$potential = ( $this->input->post('num') * $service->price );
}else{
$potential = $this->input->post('num');
}
$insert = array(
'service_id' => $this->input->post('service'),
'num' => $this->input->post('num'),
'request_date' => $this->input->post('request_date'),
'note' => serialize( $this->input->post('note') ),
'potential_income' => $potential,
'member_id' => $this->user->id,
'status' => 'pending'
);
$emaildata = array(
'service' => $service->slug,
'num' => $insert['num'],
'request_date' => $insert['request_date'],
'note' => $insert['note'],
'potential_income' => $potential,
'email_address' => $this->user->email_address,
'status' => 'pending',
'fullname' => $this->user->firstname . ' ' . $this->user->surname,
'message_sent' => date('M d h:iA'),
'site_link' => ''
);
if(Meeting::create($insert)){
if(parent::prep_email($emaildata, 'request_meeting[dot]html')){
$this->session->set_flashdata('success' ,'Request has been sent! You will recieve an email shortly from Paul');
//redirect('/');
}else{
$this->session->set_flashdata('error', 'Email service seems to be down at the moment');
//redirect('/');
}
}else{
$this->session->set_flashdata('error', 'Unable to request meeting at this time');
//redirect('/');
}
}else{
$this->meetings_form();
}