簡體   English   中英

如何使用pear包將表單中的電子郵件發送到我的Gmail

[英]How to send email from a form to my gmail using pear package

我一直在努力使用下面的代碼從表單發送電子郵件到我的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,回復:))'在/home/biwucr/public_html/functions/mailer_function.php:49堆棧追蹤:#0 /home/biwucr/public_html/send-quote.php(62):send_email('yibeltalisme @ gm。 ..','Yibeltal酸...',假的)#1 {main}

我不明白為什么。

我必須聯系我的主辦公司嗎?

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

因此,您的設置應為:

// ...
$smtp=array();
$smtp['host']='ssl://smtp.googlemail.com';
// ...

您可能想嘗試從php.ini文件中取消注釋掉這一行:

extension=php_openssl.dll

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM