Reputation:
I was updating few non PHP related pages for the client which send form input over to PHP. However without making any changes to the code client reported to not getting the e-mails. Instead he said it only sends emails to one of the emails instead of all 3. Can some one pin point whats wrong with the code?
$email_to = "[email protected], [email protected], [email protected]";
$email_subject = "VIP Access"." [".date("Y-m-d @ h:m:s A")."]";
$first_name = $_POST["objFirstName"];
$last_name = $_POST["objLastName"];
$phone = $_POST["objPhone"];
$email_from = $_POST["objEmail"];
$full_name = $first_name." ".$last_name;
$message = "";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$message .= "<html><body>\n";
$message .= "<table rules='all' border='1' style='border-color:#000;' cellpadding='10' width='100%'>\n";
$message .= "<tr style='background: #eee;'><td width='20%'><strong>Full Name:</strong> </td><td width='70%'>".clean_string($full_name)."</td></tr>\n";
$message .= "<tr style='background: #eee;'><td width='20%'><strong>Email:</strong> </td><td width='70%'>".clean_string($email_from)."</td></tr>\n";
$message .= "<tr style='background: #eee;'><td width='20%'><strong>Phone:</strong> </td><td width='70%'><a href='tel:".clean_string($phone)."'>".clean_string($phone)."</a></td></tr>\n";
$message .= "</table>\n";
$message .= "<img src='http://example.com/images/logo-trans.png' width='120' height='130' alt='Estate Brothers' style='text-align:center;'/>\n";
$message .= "</body></html>\n";
// create email headers
$headers = "From: ".$email_from." via example.com\r\n"."Reply-To: ".$email_from."\r\n"."X-Mailer: PHP/".phpversion()."via example.com";
$headers .= 'To: Example <[email protected]>, User <[email protected]>, User two <[email protected]>' . "\r\n";
$headers .= 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$mail_feed = mail($email_to, $email_subject, $message, $headers);
Upvotes: 2
Views: 205
Reputation: 893
Acquite swiftmailer here -> http://swiftmailer.org/
require_once 'swiftmailer/lib/swift_required.php';
function new_mail($subject, $content)
{
// Create the message
$message = Swift_Message::newInstance();
// Give the message a subject
$message->setSubject($subject);
// Set the From address with an associative array
$message->setFrom(array('[email protected]' => 'Sender')));
// Set the To addresses with an associative array
$message->setTo(array('[email protected]' => 'Example'));
// Give it a body
$message->setBody($content);
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
}
new_mail('Subject', $messagecontentgoeshere);
If you want you can replace the TO and FROM parts with variables as well. So you can reuse the whole function everywhere on the site.
Upvotes: 1
Reputation: 2707
Try these things:
Remove all spaces in the email_to variable (I know the RFC states spaces are allowed but some people ran into issues and removing the spaces fixes it for them):
$email_to = '[email protected],[email protected],[email protected]';
Insure you escape all your $_POST
variable to avoid injections.
You can do the TO using the headers as opposed to using mail to (notice concatenation fix like the other answers spoke about):
$email_to = "[email protected]";
....
$headers = "From: ".$email_from." via example.com\r\n"."Reply-To: ".$email_from."\r\n"."X-Mailer: PHP/".phpversion()."via example.com\r\n";
$headers .= 'To: [email protected], [email protected]'."\r\n";
$headers .= 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
Another solution would be to send the email multiple times (insure you have the other fixes in for the headers including the missing \r\n for x-mailer):
$email_to = array('[email protected]', '[email protected]', '[email protected]');
....
$mail_feed = true;
foreach ($email_to as $email) {
$mail_feed = $mail_feed && mail($email, $email_subject, $message, $headers);
}
Upvotes: 0