Jump to content

Recommended Posts

Hello all,

 

I am looking at the best way to implement an email system.  I have a class that can email the person, but I have a lot of different messages that I might have to send.  I wanted to know if my line of thought was efficient. 

Would it be a good idea to have a class that had all the possible email messages in it, and I would send it a key word to retrieve the proper message. Then if I had to email 100-300 people, would I create the class so that each instance of the class would send 1 email to a person, then destroy the class create a new one, etc...  Or would it be better to send the class the list of people the Email should be sent to, and then have the looping occur within the class? 

 

Any ideas or suggestions would be great. 

 

Thanks All

 

Link to comment
https://forums.phpfreaks.com/topic/200353-an-email-system/
Share on other sites

It would be best if your class resembles the e-mail. Then add the people you wish to e-mail to the "to:" field (comma separated) Something like:

 

class Email {
  private $to, $subject, $message, $headers, $parameters;
  
  public function __construct($from, $subject, $message) {
    $this->subject = $subject;
    $this->message = $message;
    $this->addHeader('From', $from);
    $this->addHeader('Return-Path', $from);
    $this->addHeader('Reply-To', $from);
  }
  
  public function addTo($to) {
    $this->to[] = $to;
  }
  
  public function addHeader($name, $value) {
    $this->headers[] = "$name: $value";
  }
  
  public function send() {
    $to = implode(',', $this->to);
    $headers = implode("\r\n", $this->headers);
    $message = wordwrap($this->message, 70);
    $message = str_replace("\n.", "\n..", $message);
    return mail($to, $this->subject, $message, $headers, $this->parameters);
  }
}

 

Use as:

 

$mail = new Mail('youremail@somewhere.com', 'Hello World', 'Lorem Ipsum dolor sit amet consectuer adipiscing elit');
$to = file('emails.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
foreach ($to as $email) $mail->addTo($email);
if ($mail->send()) {
  //send
}

Link to comment
https://forums.phpfreaks.com/topic/200353-an-email-system/#findComment-1051456
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.