'How to send email via php from that database?

Hello!

Anyone have any idea how to send emails from this query to people who match?

I have a code: DBFIDDLE LINK

And I would like to send an email via the button to all people, but with specific parameters. I can send an email one at a time, but in this case I mean sending emails based on matches. What I mean:

First Email:

Email From: MyEmail  
Email to:   [email protected]  
Body: 
Hello TOM W. 
You are matched with: 
KATE B. Her email: [email protected]

Second Email

Email From: MyEmail  
Email to:   [email protected]   
Body: 
Hello MARK K. 
You are matched with: 
KATE B. Her email: [email protected]
ALEX S. Her email: [email protected]

Third email

Email From: MyEmail 
Email to:   [email protected]  
Body: 
Hello KATE B. 
You are matched with: 
TOM W.  His email: [email protected]
MARK K. His email: [email protected]

Fourth Email

Email From: MyEmail 
Email to:   [email protected]
Body: 
Hello ALEX S. 
You are matched with: 
MARK K. His email: [email protected]


Solution 1:[1]

First; create an email template to store on your server. You will use this to insert new content into for each notification.

Call your list of notifications and define the parameters for the email. This can be done one at a time or by looping through an array. In this example, I'm going to just set one set of variables and process the notification.

Format your notification by replacing placeholders with the notification variables.

Now send your notification.

// Notification Variables
$to['name'] = 'TOM W';  
$to['email'] = '[email protected]';
$subject = 'You got a new match!';
$body = 'Hello TOM W.<br>You are matched with:<br>KATE B. Her email: [email protected]';

// Process Notification
emailSend($to, $subject, $body);

// This function formats the attributes required to send an email
function emailSend($to_address, $subject, $body){
  
  // To send HTML mail, the Content-type header must be set
  $headers[] = 'MIME-Version: 1.0';
  $headers[] = 'Content-type: text/html; charset=iso-8859-1';
  
  // Additional headers
  $headers[] = 'To: ' . $to_address['name'] . '<' . $to_address['email'] . '>';
  $headers[] = 'From: My Name <[email protected]>';
  $headers[] = 'Cc: Mike <[email protected]>';
  $headers[] = 'Reply-To: My Name <[email protected]>';
  
  // Load notification template and replace placeholder variables in the html file with the notification variables
  $message = emailFormatMessage($subject, $subject, $body);
  
  // Send Notification
  mail($to, $subject, $message, implode("\r\n", $headers));
}

// This function loads the static HTML file and replaces variables such as {body} with the $body variable we passed to the function.
function emailFormatMessage($title, $preheader, $body){
  $template = file_get_contents(ASSETDIR . '/templates/generic/notification.1.1.html');
  $message = str_replace('%title%', $title, $template);
  $message = str_replace('%preheader%', $preheader, $message);
  $message = str_replace('%body%', $body, $message);
  
  return $message;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1