Home > Article > Backend Development > How does ECMall support the configuration of SSL connection to the mail server_PHP tutorial
First of all, the main reason is that the phpmailer version used by ecmall is too low and does not support encrypted connections.
Then, you have to make certain adjustments to the corresponding code.
1. Cover phpmailer
Please download from the attachment:
http://files.cnblogs.com/x3d/ecmall_phpmailer_lib.zip
2. Modify lib
Involves two libs: mail.lib.php, mail_quequ.lib.php
In the constructors of these two classes, add a parameter to pass. Such as Mailer
<span>function</span> __construct(<span>$from</span>, <span>$email</span>, <span>$protocol</span>, <span>$host</span> = '', <span>$port</span> = '', <span>$user</span> = '', <span>$pass</span> = '', <span>$SMTPSecure</span> = <span>false</span><span>)//增加<span>$SMTPSecure</span> { </span><span>$this</span>->Mailer(<span>$from</span>, <span>$email</span>, <span>$protocol</span>, <span>$host</span>, <span>$port</span>, <span>$user</span>, <span>$pass</span>, <span>$SMTPSecure</span><span>); } </span><span>function</span> Mailer(<span>$from</span>, <span>$email</span>, <span>$protocol</span>, <span>$host</span> = '', <span>$port</span> = '', <span>$user</span> = '', <span>$pass</span> = '', <span>$SMTPSecure</span> = <span>false</span><span>) </span>....
The same applies to MailQueue.
3. Encapsulate calling functions
global.lib.php is about 300 lines
Add a line in function &get_mailer():
<span>$secure</span> = Conf::get('email_ssl'<span>);//增加这一行 </span><span>$mailer</span> = <span>new</span> Mailer(<span>$sender</span>, <span>$from</span>, <span>$protocol</span>, <span>$host</span>, <span>$port</span>, <span>$username</span>, <span>$password</span>, <span>$secure</span>);//同时传递参数
4. Adjust the background email setting interface and add related setting items
Backend template: setting.email_setting.html Add a configuration item
<span><</span><span>tr</span><span>></span> <span><</span><span>th </span><span>class</span><span>="paddingT15"</span><span>></span><span> 邮件服务器加密方式:</span><span></</span><span>th</span><span>></span> <span><</span><span>td </span><span>class</span><span>="paddingT15 wordSpacing5"</span><span>></span><span> {html_radios name="email_ssl" options=$email_ssl checked=$setting.email_ssl} </span><span><</span><span>label </span><span>class</span><span>="field_notice"</span><span>></span>此功能要求您的php必须支持OpenSSL模块, 如果您要使用此功能,请联系您的空间商确认支持此模块<span></</span><span>label</span><span>></span> <span></</span><span>td</span><span>></span> <span></</span><span>tr</span><span>></span>
At the same time, modify the parameter delivery of the email test
<span><</span><span>script </span><span>type</span><span>="text/javascript"</span><span>></span><span> $(</span><span>function</span><span>(){ $(</span><span>'</span><span>#send_test_email</span><span>'</span><span>).click(send_test_email); }); </span><span>function</span><span> send_test_email(){ </span><span>var</span><span> email_type </span><span>=</span><span> $(</span><span>'</span><span>input[name="email_type"]:checked</span><span>'</span><span>).val(); </span><span>var</span><span> email_ssl </span><span>=</span><span> $(</span><span>'</span><span>input[name="email_ssl"]:checked</span><span>'</span><span>).val();//增加这一行 $.ajax({ type:</span><span>"</span><span>POST</span><span>"</span><span>, url:</span><span>"</span><span>index.php</span><span>"</span><span>, data:</span><span>'</span><span>app=setting&act=send_test_email&email_type=</span><span>'</span><span>+</span><span>email_type</span><span>+</span><span>'</span><span>&email_host=</span><span>'</span><span>+</span><span>$(</span><span>"</span><span>#email_host</span><span>"</span><span>).val()</span><span>+</span><span>'</span><span>&email_port=</span><span>'</span><span>+</span><span>$(</span><span>"</span><span>#email_port</span><span>"</span><span>).val()</span><span>+</span><span>'</span><span>&email_addr=</span><span>'</span><span>+</span><span>$(</span><span>"</span><span>#email_addr</span><span>"</span><span>).val()</span><span>+</span><span>'</span><span>&email_id=</span><span>'</span><span>+</span><span>$(</span><span>"</span><span>#email_id</span><span>"</span><span>).val()</span><span>+</span><span>'</span><span>&email_pass=</span><span>'</span><span>+</span><span>$(</span><span>"</span><span>#email_pass</span><span>"</span><span>).val()</span><span>+</span><span>'</span><span>&email_test=</span><span>'</span><span>+</span><span>$(</span><span>"</span><span>#email_test</span><span>"</span><span>).val()</span><span>+</span><span>'</span><span>&email_ssl=</span><span>'</span><span>+</span><span>email_ssl, dataType:</span><span>"</span><span>json</span><span>"</span><span>, success:</span><span>function</span><span>(data){ </span><span>if</span><span>(data.done){ alert(data.msg); } </span><span>else</span><span>{ alert(data.msg); } }, error: </span><span>function</span><span>(){alert(</span><span>'</span><span>{$lang.mail_send_failure}</span><span>'</span><span>);} }); } </span><span></</span><span>script</span><span>></span>
Then you also need to modify setting.app.php
<span>/*</span><span>* * EMAIL 设置 * * @author Hyber * @return void </span><span>*/</span> <span>function</span><span> email_setting() { </span><span>$model_setting</span> = &af('settings'<span>); </span><span>$setting</span> = <span>$model_setting</span>->getAll(); <span>//</span><span>载入系统设置数据</span> <span>if</span> (!<span>IS_POST) { </span><span>$this</span>->assign('setting', <span>$setting</span><span>); </span><span>$this</span>->assign('mail_type', <span>array</span><span>( MAIL_PROTOCOL_SMTP </span>=> Lang::get('smtp'),<span> MAIL_PROTOCOL_LOCAL </span>=> Lang::get('email'),<span> ));<br /> <span>//增加 </span></span><span>$this</span>->assign('email_ssl', <span>array</span><span>( </span>0 => Lang::get('no'), 1 => 'SSL', 2 => 'TLS',<span> )); </span><span>$this</span>->display('setting.email_setting.html'<span>); } </span><span>else</span><span> { </span><span>$data</span>['email_type'] = <span>$_POST</span>['email_type'<span>]; </span><span>$data</span>['email_host'] = <span>$_POST</span>['email_host'<span>]; </span><span>$data</span>['email_ssl'] = <span>$_POST</span>['email_ssl'<span>];<span>//增加 </span></span><span>$data</span>['email_port'] = <span>$_POST</span>['email_port'<span>]; </span><span>$data</span>['email_addr'] = <span>$_POST</span>['email_addr'<span>]; </span><span>$data</span>['email_id'] = <span>$_POST</span>['email_id'<span>]; </span><span>$data</span>['email_pass'] = <span>$_POST</span>['email_pass'<span>]; </span><span>$data</span>['email_test'] = <span>$_POST</span>['email_test'<span>]; </span><span>$model_setting</span>->setAll(<span>$data</span><span>); </span><span>$this</span>->show_message('edit_email_setting_successed'<span>); } }</span>
And test email method.
<span>function</span><span> send_test_email() { </span><span>if</span><span> (IS_POST) { </span><span>$email_from</span> = Conf::get('site_name'<span>); </span><span>$email_type</span> = <span>$_POST</span>['email_type'<span>]; </span><span>$email_host</span> = <span>$_POST</span>['email_host'<span>]; </span><span>$email_ssl</span> = <span>$_POST</span>['email_ssl'<span>];<span>//增加 </span></span><span>$email_port</span> = <span>$_POST</span>['email_port'<span>]; </span><span>$email_addr</span> = <span>$_POST</span>['email_addr'<span>]; </span><span>$email_id</span> = <span>$_POST</span>['email_id'<span>]; </span><span>$email_pass</span> = <span>$_POST</span>['email_pass'<span>]; </span><span>$email_test</span> = <span>$_POST</span>['email_test'<span>]; </span><span>$email_subject</span> = Lang::get('email_subjuect'<span>); </span><span>$email_content</span> = Lang::get('email_content'<span>); </span><span>/*</span><span> 使用mailer类 </span><span>*/</span><span> import(</span>'mailer.lib'<span>); </span><span>$mailer</span> = <span>new</span> Mailer(<span>$email_from</span>, <span>$email_addr</span>, <span>$email_type</span>, <span>$email_host</span>, <span>$email_port</span>, <span>$email_id</span>, <span>$email_pass</span>, <span>$email_ssl</span><span>);<span>//增加 </span></span><span>$mail_result</span> = <span>$mailer</span>->send(<span>$email_test</span>, <span>$email_subject</span>, <span>$email_content</span>, CHARSET, 1<span>); </span><span>if</span> (<span>$mail_result</span><span>) { </span><span>$this</span>->json_result('', 'mail_send_succeed'<span>); } </span><span>else</span><span> { </span><span>$this</span>->json_error('mail_send_failure', <span>implode</span>("\n", <span>$mailer</span>-><span>errors)); } } </span><span>else</span><span> { </span><span>$this</span>->show_warning('Hacking Attempt'<span>); } }</span>
The tls method has not been tested.