{"id":1542,"date":"2015-10-16T11:13:43","date_gmt":"2015-10-16T09:13:43","guid":{"rendered":"http:\/\/blog.kodono.info\/wordpress\/?p=1542"},"modified":"2015-10-16T11:13:43","modified_gmt":"2015-10-16T09:13:43","slug":"class-phpmailer-out-of-memory-using-guiform-avec-wordpress","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2015\/10\/16\/class-phpmailer-out-of-memory-using-guiform-avec-wordpress\/","title":{"rendered":"&#8220;class-phpmailer out of memory using GuiForm&#8221; avec WordPress"},"content":{"rendered":"<p>Ayant un WordPress sur 1and1 j&#8217;ai \u00e9t\u00e9 confront\u00e9 \u00e0 un &#8220;out of memory&#8221; sur le fichier <code>class-phpmailer.php<\/code> \u00e0 cause d&#8217;un appel AJAX d&#8217;un formulaire cr\u00e9\u00e9 avec GuiForm. Je ne peux pas faire grand chose c\u00f4t\u00e9 serveur \u00e0 cause de l&#8217;h\u00e9bergement partag\u00e9&#8230; Donc j&#8217;ai fouill\u00e9 comment contourner le probl\u00e8me.<\/p>\n<p>Il va s&#8217;agir de modifier le fichier <code>guiform\/classes\/GuiForm\/Module\/Ajax.php<\/code> du plugin.<\/p>\n<p>On va remplacer la fonction <code>mailer()<\/code> afin d&#8217;utiliser la fonction <code>wp_mail<\/code> fournie par WordPress. La fonction <code>mailer()<\/code> devient :<\/p>\n<pre class=\"brush:php\">\r\n\tpublic function mailer($type = 'mail', $init = array()){\r\n\t\t\r\n\t\tglobal $wpdb, $guiform;\r\n\t\t\r\n\t\t$subject = \"\";\r\n\t\t$MsgHTML = \"\";\r\n    $headers = \"\";\r\n    $attachments = \"\";\r\n\t\t$sendTo = array_map('trim', explode(',', $init['to']));\r\n\t\t\t\t\r\n\t\t$sendCc = array_map('trim', explode(',', $init['cc']));\r\n\t\t$sendBcc = array_map('trim', explode(',', $init['bcc']));\r\n\t\t$sendReplyTo = array_map('trim', explode(',', $init['reply-to']));\r\n\t\t\r\n\t\t\/* Make sure the PHPMailer class has been instantiated \r\n\t\t\/\/ (Re)create it, if it's gone missing\r\n\t\tif ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {\r\n\t\t\trequire_once ABSPATH . WPINC . '\/class-phpmailer.php';\r\n\t\t\trequire_once ABSPATH . WPINC . '\/class-smtp.php';\r\n\t\t\t$phpmailer = new PHPMailer(true);\r\n\t\t\t$phpmailer->clearAllRecipients();\r\n\t\t  $phpmailer->SMTPAuth = true;\r\n\t\t}*\/\r\n\t\t\r\n\t\tif($type == 'test-mail'){\r\n\t\t\t$data = $wpdb->get_row($wpdb->prepare(\"SELECT name, value FROM $wpdb->guiform_options WHERE id = %d\", $this->_id));\r\n\t\t  $sendTo = array($data->name);\r\n\t\t  $row = unserialize($data->value);\r\n\t\t  $row = array_map('trim', $row);\r\n\t\t\t$html = \"&lt;strong>\". __('Greetings!', GuiForm_Plugin::NAME) .\"&lt;\/strong>&lt;br \/>&lt;br \/>\";\r\n\t\t\t$html .=  __(\"This is a test message.\", GuiForm_Plugin::NAME) .\"&lt;br \/>&lt;br \/>\";\r\n\t\t\t$MsgHTML = self::emailTpl($html);\r\n\t\t\t\/*$phpmailer->SetFrom(\"noreply@guiform.com\", GuiForm_Plugin::PACKAGE);\r\n\t\t\t$phpmailer->Subject = __('Test Message', GuiForm_Plugin::NAME);*\/\r\n      $headers .= 'From: noreply@guiform.com' . \"\\r\\n\";\r\n      $subject = __('Test Message', GuiForm_Plugin::NAME);\r\n\t\t}\r\n\t\telse if($type == 'activation-mail'){\r\n\t\t\t$data = $wpdb->get_row($wpdb->prepare(\"SELECT name, value FROM $wpdb->guiform_options WHERE id = %d\", $this->_id));\r\n\t\t  $row = unserialize($data->value);\r\n\t\t  $row = array_map('trim', $row);\r\n\t\t  $mv_code = md5(time());\r\n\t\t\t$row['key'] = $mv_code;\r\n\t\t\t$guiform->updateOption($data->name, $row, 'mail', $this->_id);\r\n\t\t\t\/\/$phpmailer->Subject = __(\"Email Verification\", GuiForm_Plugin::NAME);\r\n      $subject = __(\"Email Verification\", GuiForm_Plugin::NAME);\r\n\t\t\t$sendTo = array($data->name);\r\n\t\t\t$vlink = get_site_url() .\"\/?\". $guiform->getOption('permalink')->value['value'] .'='. $this->_id .\"&#038;mv-code=$mv_code\";\r\n\t\t\r\n\t\t\t$html = \"Hello \".$row['name'].\",&lt;br \/>&lt;br \/>\";\r\n\t\t\t$html .= __(\"To enable this email address from sending emails with your forms we must first verify by clicking the link below:\", GuiForm_Plugin::NAME) .\"&lt;br \/>&lt;br \/>\";\r\n\t\t\t$html .= __(\"Verification Link: \", GuiForm_Plugin::NAME) .\"&lt;a target=\\\"_blank\\\" href=\\\"$vlink\\\">\". __(\"click here!\", GuiForm_Plugin::NAME) .\"&lt;\/a>&lt;br \/>&lt;br \/>\";\r\n\t\t\t$MsgHTML = self::emailTpl($html);\r\n\t\t\t\/\/$phpmailer->SetFrom(\"noreply@guiform.com\", \"GuiForm\");\r\n      $headers .= 'From: noreply@guiform.com' . \"\\r\\n\";\r\n\t\t}\r\n\t\telse if($type == 'mail'){\t\r\n\t\t\t\r\n\t\t\t$init['message'] = str_replace(\"\\\\r\\\\n\", \"&lt;br \/>\", $init['message']);\r\n\t\t\t$init['message'] = stripcslashes($init['message']);\r\n\t\t\t\r\n\t\t\t\/\/Do not remove &#038;nbsp and &lt;br \/>.\r\n\t\t\t$MsgHTML = $init['message'] .\" &nbsp; &lt;br \/>\";\r\n\t\t\t\r\n\t\t\t\/\/$phpmailer->SetFrom($init['from'], \"\");\r\n\t\t\t\/\/$phpmailer->Subject = $init['subject'];\r\n      $headers .= 'Reply-To: '.$init['from'] . \"\\r\\n\";\r\n      $headers .= 'From: '.$init['from'] . \"\\r\\n\";\r\n      $subject = $init['subject'];\r\n\t\t\t\r\n\t\t\tif(sizeof($init['attachment'])){\r\n\t\t\t\tforeach($init['attachment'] as $file){\r\n\t\t\t\t\t\/\/$phpmailer->AddAttachment(self::getAttachmentPath($file['url']), $file['name']);\r\n          $attachments = self::getAttachmentPath($file['url']);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif(sizeof($sendReplyTo)){\r\n\t\t\t\tforeach($sendReplyTo as $replyTo){\r\n\t\t\t\t\tif(is_email($replyTo)){\r\n\t\t\t\t\t\t\/\/$phpmailer->AddReplyTo($replyTo);\r\n            $headers .= 'Reply-To: '.$replyTo. \"\\r\\n\";\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\r\n\t\t\tif(sizeof($sendCc)){\r\n\t\t\t\tforeach($sendCc as $mailCc){\r\n\t\t\t\t\tif(is_email($mailCc)){\r\n\t\t\t\t  \t\/\/$phpmailer->AddCC($mailCc);\r\n            $headers .= 'Cc: '.$mailCc.\"\\r\\n\";\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif(sizeof($sendBcc)){\r\n\t\t\t\tforeach($sendBcc as $mailBcc){\r\n\t\t\t\t\tif(is_email($mailBcc)){\r\n\t\t\t\t  \t\/\/$phpmailer->AddCC($mailBcc);\r\n            $headers .= 'Bcc: '.$mailBcc.\"\\r\\n\";\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t\/*$phpmailer->Body = html_entity_decode($MsgHTML);\r\n\t\t$phpmailer->AltBody = strip_tags($MsgHTML);\r\n\t\t$phpmailer->IsHTML(true);\r\n\t\t$phpmailer->CharSet = \"UTF-8\";*\/\r\n    $headers .= 'MIME-Version: 1.0' . \"\\r\\n\";\r\n    $headers .= 'Content-Type: text\/html; charset=UTF-8'.\"\\r\\n\";\r\n    $body = html_entity_decode($MsgHTML);\r\n\t\twp_mail( $sendTo, $subject, $body, $headers, $attachments);\r\n\/*\r\n\t\tforeach($sendTo as $mailTo){\r\n\t\t\tif($phpmailer->validateAddress($mailTo)){\r\n\t\t\t\t$phpmailer->AddAddress($mailTo);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t$smtpSettings = $guiform->getOption($this->form, false, 'smtp')->value;\r\n\t\t\r\n\t\tif($smtpSettings->smtp_enable){\r\n\t\t\t$row['protocol'] = 'smtp';\r\n\t\t\t$row['smtp_host'] = $smtpSettings->smtp_host;\r\n\t\t\t$row['smtp_port'] = $smtpSettings->smtp_port;\r\n\t\t}\r\n\t\t\r\n\t\t$phpmailer->Mailer = $row['protocol'];\r\n\t\t\r\n\t\tif($row['protocol'] == 'smtp'){\r\n\t\t\t$phpmailer->IsSMTP();\r\n\t\t\t$phpmailer->SMTPSecure = $row['smtp_encryption'];\r\n\t\t\t$phpmailer->Host       = $row['smtp_host'];\r\n\t\t\t$phpmailer->Port       = $row['smtp_port'];\r\n\t\t}\r\n\t\t\r\n\t\tif(filter_var($row['smtp_auth'], FILTER_VALIDATE_BOOLEAN)) {\r\n\t\t\t$phpmailer->SMTPAuth = true;\r\n\t\t\t$phpmailer->Username = trim($row['smtp_username']);\r\n\t\t\t$phpmailer->Password = trim($row['smtp_password']);\r\n\t\t}\r\n\t\t\r\n    if(!$phpmailer->send()) {\r\n\t\t\tdie( __(\"Mailer Error: \", GuiForm_Plugin::NAME) . $phpmailer->ErrorInfo);\r\n\t\t} else {\r\n\t\t\t$phpmailer->clearAllRecipients();\r\n\t\t  $phpmailer->clearAttachments();\r\n\t\t\tif($type !== 'mail'){\r\n\t\t\t\tdie( __(\"Message sent! Please check your email for message.\", GuiForm_Plugin::NAME));\r\n\t\t\t}\r\n\t\t}\r\n*\/\r\n\t}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ayant un WordPress sur 1and1 j&#8217;ai \u00e9t\u00e9 confront\u00e9 \u00e0 un &#8220;out of memory&#8221; sur le fichier class-phpmailer.php \u00e0 cause d&#8217;un appel AJAX d&#8217;un formulaire cr\u00e9\u00e9 avec GuiForm. Je ne peux pas faire grand chose c\u00f4t\u00e9 serveur \u00e0 cause de l&#8217;h\u00e9bergement partag\u00e9&#8230; Donc j&#8217;ai fouill\u00e9 comment contourner le probl\u00e8me. Il va s&#8217;agir de modifier le fichier [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[23,1,13,33],"tags":[152,25,158,82],"class_list":["post-1542","post","type-post","status-publish","format-standard","hentry","category-debug","category-divers","category-niveau-intermediaire","category-programmation","tag-niveau-intermediaire","tag-php","tag-programmation","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1542"}],"version-history":[{"count":4,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1542\/revisions"}],"predecessor-version":[{"id":1546,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1542\/revisions\/1546"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}