1

I have a function for email, please find the code below:

function sendEmail($userName, $password, $sendTo, $sendToName, $subject, $body, $mailSent){

        $mail             = new PHPMailer();
        // $body             = eregi_replace("[\]",'',$body);

        $mail->IsSMTP(); // telling the class to use SMTP
        --------
        --------
        --------

        $mail->SetFrom($userName, 'Rajendra Arora');

        $mail->AddReplyTo($userName,"Rajendra Arora");

        $mail->Subject    = $subject;

        $mail->Body    = $body; // optional, comment out and test
    }

This email function has an attachment with long file size but whenever this email function is called by using sendEmail();. It is executed everytime and takes much time as it has to go through the whole execution program with primarily upload.

Is there any way to execute this sendEmail()function only for once time. So that we could call it simply once and saves time from preventing to upload?

Ramesh
  • 7
  • 1
  • Memoization is valid only for function that have no side effects. The function you're talking about probably sends mail, but it's unclear from description. – polkovnikov.ph Jun 17 '17 at 11:41
  • What I mean to say, can we execute this whole `sendEmail()` function only for once time. so that whenever next time we call `sendEmail()` anywhere, It has not to go through the whole `sendEmail()` function and simply it implies with already execution with upload file changes. – Ramesh Jun 17 '17 at 11:45
  • File upload takes time because SMTP needs to upload the file every time you send a letter. Everything else takes almost no time. – polkovnikov.ph Jun 17 '17 at 11:47
  • Exactly, Thanks anyway .. @jens108 has already pointed me some suggestion. :) – Ramesh Jun 17 '17 at 11:49
  • Beware that all recipients will know emails of each other then. – polkovnikov.ph Jun 17 '17 at 11:51
  • Really? ??? Is there any other way suggest for this job process? – Ramesh Jun 17 '17 at 11:57
  • a) Make the size of attachments smaller; b) Send emails in several parallel threads, better in a language that supports parallel/asynchronous programming unlike PHP. – polkovnikov.ph Jun 17 '17 at 12:01
  • Possible duplicate of [Send email Asynchronously via PhpMailer](https://stackoverflow.com/questions/32743260/send-email-asynchronously-via-phpmailer) – terales Jun 17 '17 at 13:18

1 Answers1

0

If your emails have the same contents, you can send the same mail to multiple users: you just need to call the AddAddress method once for every recipient. Like so:

$mail->AddAddress('person1@domain.com', 'Person One');
$mail->AddAddress('person2@domain.com', 'Person Two');
influjensbahr
  • 3,978
  • 1
  • 12
  • 12