hello friends, I can't believe how much time I have lost on this. I need some eyes on this. basically, I have an HTML page that send a list of emails through AJAX, who the sends the email.
This works like butter but when you get the email, the headers are all messed up and I can't seem to find the right formula. I did a search here and saw different options but none have fixed mine.
Here's my class
<?php
class Emailer
{
private $db;
private $user_id;
private $mt4_id;
private $to;
private $from;
private $from_email;
private $subject;
private $message;
private $headers;
#Constructor
function __construct() {
}
#add database link
public function add_db_resource ($resource) {
$this->db = $resource;
}
public function set_to ($P) {
$tmp = "";
foreach ($P as $email) {
$tmp .= $email.",";
}
$str_array = substr($tmp, 0, -1);
$this->to = explode(',',$str_array);
}
public function get_email_address() {
return $this->to;
}
public function set_from($inviter, $email) {
$this->from_email = $email;
$this->from = $inviter." <".$email.">";
}
public function set_subject($challenge_title) {
$this->subject = "I'm inviting you to " .$challenge_title. " with me";
}
public function set_message($body) {
$this->message = $body;
}
public function set_headers() {
//$this->headers = 'From: '.$this->from_email. '\r\n Reply-To: '.$this->from_email. '\r\n Content-type: text/html';
$this->headers = "Content-type: text/html" . "\n" . "From: " . $this->from_email . "\n" . "Reply-To: " . $this->from_email;
}
#send emails out
public function send() {
foreach ($this->to as $email) {
mail($email, $this->subject, $this->message, $this->headers, '-f '.$this->from);
}
return true;
}
}
The AJAX script looks like this:
#set email properties
$emailer->set_headers();
$emailer->set_to($_POST);
$emailer->set_from($user->getUserMeta('first_name')." ".$user->getUserMeta('last_name'), $user->getUserMeta('email'));
$emailer->set_subject($ch->getTitle());
#setup html
$body = '<html>...</html>';
$emailer->set_message($body);
#good to send
if (!$emailer->send()) {
echo "0-Failed";
exit;
}
So the email is sent, but the From is empty. I have tried -f xxx@xxx.xxx, -f My Name <xxx@xxx.com>, I have tried it without it, I have tried hardcoding the from field... nothing.
I have tried others things, like changing the order the header string and sometimes it shows the FROM email address but the body is no longer HTML.
I am losing it.
Help.