0

我一直在努力使用下面的代码从表单向我的 gmail 发送电子邮件。但我放弃了。请看下面的代码:

                            require_once('../php/Mail.php');
                            require_once('../php/Mail/RFC822.php');
                            //function to send email
                            function send_email($to,$from,$subject,$body,$is_body_html=false) {
                                if(! valid_email($to))
                                {
                                    throw new Exception('This email address in invalid: '.htmlspecialchars($to));
                                }
                                if(! valid_email($from))
                                {
                                    throw new Exception('This email address in invalid: '.htmlspecialchars($from));
                                }
                                //set up an array which can hold the SMTP server detail
                                $smtp=array();
                                $smtp['host']='ssl://smtp.gmail.com';
                                $smtp['port']=465;
                                $smtp['auth']=true;
                                $smtp['username']='my_username@gmail.com';
                                $smtp['password']='my_pass';

                                //create the mailer object using which can connect to your SMTP server
                                $mailer=Mail::factory('smtp',$smtp);
                                //check the returned value when creating the mailer object
                                if(PEAR::isError($mailer))
                                    {
                                    throw new Exception('Could not create the mailer object.');
                                    }
                                //as the send method of the mailer object accepts the reciepients and headers as an array, create 
                                //and set up the arrays

                                $recipients=array();
                                $recipients['to']=$to;

                                $headers=array();
                                $headers['from']=$from;
                                $headers['to']=$to;
                                $headers['subject']=$subject;
                                //check if the content is set to be html
                                if($is_body_html)
                                    {
                                    $headers['Content-type']='text/html';
                                    }
                               //send the email
                               $result=$mailer->send($recipients,$headers,$body);
                               //check the returned value when sending the email
                                if(PEAR::isError($result))
                                    {
                                    throw new Exception('There was an error while trying to send the email: '.htmlspecialchars($result));
                                    }
                            } //end of send_email function

我得到的错误信息如下

错误:异常“异常”,消息“尝试发送电子邮件时出错:无法连接到 ssl://smtp.gmail.com:465 [SMTP:无法连接套接字:连接超时(代码:- 1,响应:)]' in /home/biwucr/public_html/functions/mailer_function.php:49 堆栈跟踪:#0 /home/biwucr/public_html/send-quote.php(62): send_email('yibeltalisme@gm. ..', 'Yibeltal 酸...', false) #1 {main}

我不明白为什么。

我必须联系我的寄宿公司吗?

4

2 回答 2

1

Gmail 的 SMTP 服务器地址是SMTP.GOOGLEMAIL.COM而不是 SMTP.GMAIL.COM。

因此您的设置应该是:

// ...
$smtp=array();
$smtp['host']='ssl://smtp.googlemail.com';
// ...
于 2012-06-04T09:45:52.933 回答
1

您可能想尝试从 php.ini 文件中取消注释这一行:

extension=php_openssl.dll
于 2012-08-29T05:38:23.283 回答