“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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  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));
      }
    }
*/
  }

Leave a Reply

Your email address will not be published. Required fields are marked *

*