« class-phpmailer out of memory using GuiForm » avec WordPress

Ayant un WordPress sur 1and1 j’ai été confronté à un « out of memory » sur le fichier class-phpmailer.php à cause d’un appel AJAX d’un formulaire créé avec GuiForm. Je ne peux pas faire grand chose côté serveur à cause de l’hébergement partagé… Donc j’ai fouillé comment contourner le problème.

Il va s’agir de modifier le fichier guiform/classes/GuiForm/Module/Ajax.php du plugin.

On va remplacer la fonction mailer() afin d’utiliser la fonction wp_mail fournie par WordPress. La fonction mailer() devient :

	public function mailer($type = 'mail', $init = array()){
		
		global $wpdb, $guiform;
		
		$subject = "";
		$MsgHTML = "";
    $headers = "";
    $attachments = "";
		$sendTo = array_map('trim', explode(',', $init['to']));
				
		$sendCc = array_map('trim', explode(',', $init['cc']));
		$sendBcc = array_map('trim', explode(',', $init['bcc']));
		$sendReplyTo = array_map('trim', explode(',', $init['reply-to']));
		
		/* Make sure the PHPMailer class has been instantiated 
		// (Re)create it, if it's gone missing
		if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
			require_once ABSPATH . WPINC . '/class-phpmailer.php';
			require_once ABSPATH . WPINC . '/class-smtp.php';
			$phpmailer = new PHPMailer(true);
			$phpmailer->clearAllRecipients();
		  $phpmailer->SMTPAuth = true;
		}*/
		
		if($type == 'test-mail'){
			$data = $wpdb->get_row($wpdb->prepare("SELECT name, value FROM $wpdb->guiform_options WHERE id = %d", $this->_id));
		  $sendTo = array($data->name);
		  $row = unserialize($data->value);
		  $row = array_map('trim', $row);
			$html = "<strong>". __('Greetings!', GuiForm_Plugin::NAME) ."</strong><br /><br />";
			$html .=  __("This is a test message.", GuiForm_Plugin::NAME) ."<br /><br />";
			$MsgHTML = self::emailTpl($html);
			/*$phpmailer->SetFrom("noreply@guiform.com", GuiForm_Plugin::PACKAGE);
			$phpmailer->Subject = __('Test Message', GuiForm_Plugin::NAME);*/
      $headers .= 'From: noreply@guiform.com' . "\r\n";
      $subject = __('Test Message', GuiForm_Plugin::NAME);
		}
		else if($type == 'activation-mail'){
			$data = $wpdb->get_row($wpdb->prepare("SELECT name, value FROM $wpdb->guiform_options WHERE id = %d", $this->_id));
		  $row = unserialize($data->value);
		  $row = array_map('trim', $row);
		  $mv_code = md5(time());
			$row['key'] = $mv_code;
			$guiform->updateOption($data->name, $row, 'mail', $this->_id);
			//$phpmailer->Subject = __("Email Verification", GuiForm_Plugin::NAME);
      $subject = __("Email Verification", GuiForm_Plugin::NAME);
			$sendTo = array($data->name);
			$vlink = get_site_url() ."/?". $guiform->getOption('permalink')->value['value'] .'='. $this->_id ."&mv-code=$mv_code";
		
			$html = "Hello ".$row['name'].",<br /><br />";
			$html .= __("To enable this email address from sending emails with your forms we must first verify by clicking the link below:", GuiForm_Plugin::NAME) ."<br /><br />";
			$html .= __("Verification Link: ", GuiForm_Plugin::NAME) ."<a target=\"_blank\" href=\"$vlink\">". __("click here!", GuiForm_Plugin::NAME) ."</a><br /><br />";
			$MsgHTML = self::emailTpl($html);
			//$phpmailer->SetFrom("noreply@guiform.com", "GuiForm");
      $headers .= 'From: noreply@guiform.com' . "\r\n";
		}
		else if($type == 'mail'){	
			
			$init['message'] = str_replace("\\r\\n", "<br />", $init['message']);
			$init['message'] = stripcslashes($init['message']);
			
			//Do not remove &nbsp and <br />.
			$MsgHTML = $init['message'] ."   <br />";
			
			//$phpmailer->SetFrom($init['from'], "");
			//$phpmailer->Subject = $init['subject'];
      $headers .= 'Reply-To: '.$init['from'] . "\r\n";
      $headers .= 'From: '.$init['from'] . "\r\n";
      $subject = $init['subject'];
			
			if(sizeof($init['attachment'])){
				foreach($init['attachment'] as $file){
					//$phpmailer->AddAttachment(self::getAttachmentPath($file['url']), $file['name']);
          $attachments = self::getAttachmentPath($file['url']);
				}
			}
			
			if(sizeof($sendReplyTo)){
				foreach($sendReplyTo as $replyTo){
					if(is_email($replyTo)){
						//$phpmailer->AddReplyTo($replyTo);
            $headers .= 'Reply-To: '.$replyTo. "\r\n";
					}
				}
			}
			
			
			if(sizeof($sendCc)){
				foreach($sendCc as $mailCc){
					if(is_email($mailCc)){
				  	//$phpmailer->AddCC($mailCc);
            $headers .= 'Cc: '.$mailCc."\r\n";
					}
				}
			}
			
			if(sizeof($sendBcc)){
				foreach($sendBcc as $mailBcc){
					if(is_email($mailBcc)){
				  	//$phpmailer->AddCC($mailBcc);
            $headers .= 'Bcc: '.$mailBcc."\r\n";
					}
				}
			}
		}
		
		/*$phpmailer->Body = html_entity_decode($MsgHTML);
		$phpmailer->AltBody = strip_tags($MsgHTML);
		$phpmailer->IsHTML(true);
		$phpmailer->CharSet = "UTF-8";*/
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-Type: text/html; charset=UTF-8'."\r\n";
    $body = html_entity_decode($MsgHTML);
		wp_mail( $sendTo, $subject, $body, $headers, $attachments);
/*
		foreach($sendTo as $mailTo){
			if($phpmailer->validateAddress($mailTo)){
				$phpmailer->AddAddress($mailTo);
			}
		}
		
		$smtpSettings = $guiform->getOption($this->form, false, 'smtp')->value;
		
		if($smtpSettings->smtp_enable){
			$row['protocol'] = 'smtp';
			$row['smtp_host'] = $smtpSettings->smtp_host;
			$row['smtp_port'] = $smtpSettings->smtp_port;
		}
		
		$phpmailer->Mailer = $row['protocol'];
		
		if($row['protocol'] == 'smtp'){
			$phpmailer->IsSMTP();
			$phpmailer->SMTPSecure = $row['smtp_encryption'];
			$phpmailer->Host       = $row['smtp_host'];
			$phpmailer->Port       = $row['smtp_port'];
		}
		
		if(filter_var($row['smtp_auth'], FILTER_VALIDATE_BOOLEAN)) {
			$phpmailer->SMTPAuth = true;
			$phpmailer->Username = trim($row['smtp_username']);
			$phpmailer->Password = trim($row['smtp_password']);
		}
		
    if(!$phpmailer->send()) {
			die( __("Mailer Error: ", GuiForm_Plugin::NAME) . $phpmailer->ErrorInfo);
		} else {
			$phpmailer->clearAllRecipients();
		  $phpmailer->clearAttachments();
			if($type !== 'mail'){
				die( __("Message sent! Please check your email for message.", GuiForm_Plugin::NAME));
			}
		}
*/
	}

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*