PHP email form not sending information - php

I've inherited an email form from a former vendor and after we've moved it to it's new server it started to malfunction. What it's supposed to do is send a verification email to the user who filled it out (does this), then send an email to warranty#mydomain.com and bcc: another address (this it no longer does). Tried everything but I'm not super savvy with PHP, mostly a front-end coder, can anyone look at my code and tell me why this isn't sending properly?
PHP code:
<?php
header("location: http://www.anatomicglobal.com/warranty/regthanks.html");
$posting = array(
'Name' => $_POST['Name'],
'Email' => $_POST['Email'],
'Phone' => $_POST['Phone'],
'Address' => $_POST['Address'],
'City' => $_POST['City'],
'State' => $_POST['State'],
'Province' => $_POST['Province'],
'Zip' => $_POST['Zip'],
'Product' => $_POST['check'][0],
'Size' => $_POST['size'][0],
'Mattress Model Name' => $_POST['MattressModeName'],
'Mattress Model Number' => $_POST['MattressModeNo'],
'Serial Number' => $_POST['SerialNumber'],
'Store Name' => $_POST['StoreName'],
'Purchase Month'=> $_POST['Month'],
'Purchase Day' => $_POST['Day'],
'Purchase Year' => $_POST['Year']
);
$decide = $_POST['decide'];
$subject = 'Eco Memory Foam - Warranty Registration';
$to = 'warranty#anatomicglobal.com';
$to = $posting['Email'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'To: Anatomic Global <warranty#anatomicglobal.com>' . "\r\n";
$headers .= 'From: ecomemoryfoam.com <warranty#anatomicglobal.com>' . "\r\n";
$headers .= 'Reply-To: ' . $posting['Email'] . "\r\n";
$headers .= 'bcc: Hollyce Weber <hweber#anatomicglobal.com>' . "\r\n";
$message = '<html>';
$message .= '<head><title>Eco Memory Foam - Warranty Registration</title></head><body>';
$message .= '<h1>Eco Memory Foam - Warranty Registration</h1>';
$message .= '<p><strong>Warranty Registration</strong> submission successful, please keep for your records.</p>
';
$message .= '<p>Below is the submitted information at: <strong>' . strftime("%B %d %Y - %H:%M:%S", time()) . '</strong></p>';
$message .= '<dl>';
foreach ($posting as $field => $value) {
$message .= '<dt>';
$message .= '<dd><b>' . $field . '</b>: ' . $value . '</dd>';
$message .= '</dt>';
};
$message .= '<dt>Decide to Purchase This Product?</dt>';
$message .= '<dd>Customer Selected:<ul>';
foreach ($decide as $field => $value) {
$message .= '<li>' . $value . '</li>';
};
$message .= '</ul></dd>';
$message .= '</dl>';
/**$message .= '<p>You can reply to the submitter by replying to this email (if they gave you a valid email address).</p></body></html>';**/
mail($to, $subject, $message, $headers);
a few hours later....
Thanks for the help guys, but it's still not sending anything, the new code looks like this:
<?php
$posting = array(
'Name' => $_POST['Name'],
'Email' => $_POST['Email'],
'Phone' => $_POST['Phone'],
'Address' => $_POST['Address'],
'City' => $_POST['City'],
'State' => $_POST['State'],
'Province' => $_POST['Province'],
'Zip' => $_POST['Zip'],
'Product' => $_POST['check'][0],
'Size' => $_POST['size'][0],
'Mattress Model Name' => $_POST['MattressModeName'],
'Mattress Model Number' => $_POST['MattressModeNo'],
'Serial Number' => $_POST['SerialNumber'],
'Store Name' => $_POST['StoreName'],
'Purchase Month'=> $_POST['Month'],
'Purchase Day' => $_POST['Day'],
'Purchase Year' => $_POST['Year']
);
$decide = $_POST['decide'];
$subject = 'Eco Memory Foam - Warranty Registration';
$to = "warranty#anatomicglobal.com, {$posting['email']}";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'To: Anatomic Global <warranty#anatomicglobal.com>' . "\r\n";
$headers .= 'From: ecomemoryfoam.com <warranty#anatomicglobal.com>' . "\r\n";
$headers .= 'Reply-To: ' . $posting['Email'] . "\r\n";
$headers .= 'bcc: Hollyce Weber <hweber#anatomicglobal.com>' . "\r\n";
$message = '<html>';
$message .= '<head><title>Eco Memory Foam - Warranty Registration</title></head><body>';
$message .= '<h1>Eco Memory Foam - Warranty Registration</h1>';
$message .= '<p><strong>Warranty Registration</strong> submission successful, please keep for your records.</p>
';
$message .= '<p>Below is the submitted information at: <strong>' . strftime("%B %d %Y - %H:%M:%S", time()) . '</strong></p>';
$message .= '<dl>';
foreach ($posting as $field => $value) {
$message .= '<dt>';
$message .= '<dd><b>' . $field . '</b>: ' . $value . '</dd>';
$message .= '</dt>';
};
$message .= '<dt>Decide to Purchase This Product?</dt>';
$message .= '<dd>Customer Selected:<ul>';
foreach ($decide as $field => $value) {
$message .= '<li>' . $value . '</li>';
};
$message .= '</ul></dd>';
$message .= '</dl>';
/**$message .= '<p>You can reply to the submitter by replying to this email (if they gave you a valid email address).</p></body></html>';**/
mail($to, $subject, $message, $headers);
header("location: http://www.anatomicglobal.com/warranty/regthanks.html");

put
header("location: http://www.anatomicglobal.com/warranty/regthanks.html");
at the end...

In your code, you have it so it is sending the email to only one address. Here you're building your to adress:
$to = 'warranty#domain.com';
$to = $posting['Email'];
If you notice, you're setting the address to yours then overwriting it with the posted email. Then you go on to email it to only the one address
mail($to, $subject, $message, $headers);
You need to build the addresses as such:
$to = "warranty#domain.com, {$posting['email']}";
this will have the following value:
"warranty#domain.com, other#domain.com"

Related

Google Api Gmail - strange emails

I am using this function for send emails.
function sendMessage($to, $subject, $messageBody, $Cc)
{
$service = getGoogleGmailService();
$message = new \Google_Service_Gmail_Message();
$rawMessageString = "From: 'My name' <me>\r\n";
$rawMessageString .= "To: <" . $to . ">\r\n";
if (!empty($Cc)) {
$rawMessageString .= "Cc: <" . $Cc . ">\r\n";
}
$rawMessageString .= 'Subject: =?utf-8?B?' . base64_encode($subject) . "?=\r\n";
$rawMessageString .= "MIME-Version: 1.0\r\n";
$rawMessageString .= "Content-Type: text/html; charset=utf-8\r\n";
$rawMessageString .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
$rawMessageString .= $messageBody . "\r\n";
$rawMessage = strtr(base64_encode($rawMessageString), array('+' => '-', '/' => '_'));
$message->setRaw($rawMessage);
try {
$message = $service->users_messages->send('me', $message);
return $message->getId();
} catch (Exception $e) {
return $e->getMessage();
}
}
In the first photo it is how the email should be structured, in fact everything arrives ok. Every so often, however, I receive a strange email, as in the second photo, as if it were still encoded or something else.
What could it be ?

Mail function in PHP does not allow me to send email to multiple recipients

I am trying to send emails to a team and some invited individuals to indicate their assigned cooperation. However, only the first recipient (the team) in the $to seems to be receiving an email. The invited individuals do not receive any emails. Can anyone tell what I did wrong?
<?php
/* Template Name: send emails */
include_once ('important.php');
function get_emails($campus) {
global $mydb;
$individuals = array();
$individuals = $mydb->get_results("SELECT * FROM individuals WHERE campus = '$campus'");
return array_map(function($e) {
return $e->individual_email; }, $individuals);
}
$team_email = 'teamA#gmail.com';
$campus = 'NY';
$individuals = get_emails($campus);
$to = $team_email. ', ' .implode(', ', $individuals);
$subject = 'Team invitation from ' . $team_email. '!';
$message = 'Hello everyone, you are now working together';
send_email($message, $subject, $to);
function send_email($message, $subject, $to, $from='admin#gmail.com', $from_name='Admin Team Creator') { $message = '';
$message .= '<html>';
$message .= '<body>';
$message .= $msg;
$message .= '-<br/>';
$message .= '<font face="tahoma, sans-serif" color="#0eafd6"><b>The Admin Team Creator</b></font><br/>';
$message .= '</body>'; $message .= '</html>';
$headers = ''; $headers .= 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-Type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: ' . $from . ' <' . $from_name . '>' . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion();
return mail($to, $subject, $message, $headers, '-f ' . $from); }
?>

bootstrap mail form $from<$email>

I refer to this page.
https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form
i want mailheader "name"
but this php code is
<?php
// configure
$from = 'Demo contact form <demo#domain.com>';
$sendTo = 'Demo contact form <demo#domain.com>';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// let's do the sending
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
so i tried
$sendTo = 'Demo contact form '; => my mail.
$from = 'Demo contact form '; => $name <'$email'>
but the result is "nobody"
So i tried again.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
=>
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from<$email>,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
But it failed...
what should i do?
i tried
From: $FromName and
$headers = "From: $from_user \r\n". and
$headers .= 'From: "'. $from . '" <' . $Email . '>' . "\r\n"; etc...
This worked for me
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$from = "$name<$email_address>";

PHP mailgun mailer issue

New at PHP and trying to integrate mailgun to send to multiple forms. Below is the code for my PHP script which is using mailgun.
When sending the email using a simple mailgun script i am able to receive email but when i attach it to the forms i don't get any email.
require 'vendor/autoload.php';
use Mailgun\Mailgun;
else if($_GET['method'] == 'send_form'){
$to = 'myemail#email.com';
$message = 'Name : '.$_POST['name'].'<br />';
$message .= 'Mobile : '.$_POST['mobile'].'<br />';
$message .= 'Email : '.$_POST['email'].'<br />';
$from = $name.' <'.$from.'>';
$name = $_POST['name'];
$subject = 'Email Subj for form 1';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: '.$name.' <'.$from.'>' . "\r\n";
send_mail($to,$from,$subject,$message);
}
else if($_GET['method'] == 'send_form_two'){
$to = 'myemail#email.com';
$message = 'Name : '.$_POST['name'].'<br />';
$message .= 'Mobile : '.$_POST['mobile'].'<br />';
$message .= 'Email : '.$_POST['email'].'<br />';
$from = $name.' <'.$from.'>';
$name = $_POST['name'];
$subject = 'Email subject for form 2';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: '.$name.' <'.$from.'>' . "\r\n";
send_mail($to,$from,$subject,$message);
}
function send_mail($to, $from, $subject,$content)
{
$mgClient = new Mailgun('mailgunkey-here');
$domain = "mg.domain.com";
$result = $mgClient->sendMessage($domain, array(
'from' => $from,
'to' => $to,
'subject' => $subject,
'text' => $content
));
}
Seems like my code does not want 'from' => $from,
changing the from to static email resulted to sent mails.

PHP HTML mail is not working as I wanted

I have created reset password page, where used enters hes email, and PHP sends him back a the reset key. Mail works, but its going as plain text in my gmail account. I wanted it to go in HTML.
$subject = "Your password reset for {$config['site_name']}";
$message = "<html><body>";
$message .= "<p>Someone on" . $config['site_domain'] . "tried to reset your password.</p>";
$message .= "<p>Please click below link, if you want to reset your password.</p>";
$message .= "<p><a href='" . $config['site_url'] . "/forgot_password.php?key=" . $key . "'>" . $config['site_url'] . "/forgot_password.php?key=" . $key . "</a></p>";
$message .= "<p>Thank you,<br>The Admin - " . $config['site_url'] . " </p>";
$message .= "</body></html>";
// Create email headers
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
// Additional headers
//$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= "From: " . $config['site_name'] . " <noreply#" . $config['site_domain'] . "> \r\n";
$headers .= "X-Sender: <noreply#" . $config['site_domain'] . "> \r\n";
$headers .= "Reply-To: <noreply#" . $config['site_domain'] . "> \r\n";
mail($input['email'],$subject,$message,$headers);
//update pw_reset field into DATABASE
$stmt = $mysqli->prepare("UPDATE members SET pw_reset = ? WHERE email = ?");
$stmt->bind_param("ss", $key, $input['email']);
$stmt->execute();
$stmt->close();
You should structure your headers like this:
$headers = 'From: You <you#example.com>' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Notice that the From is before the MIME and Content and only Content ends with "\r\n", the other are just "\n".
Source (saganwebdesign)
Try this function. returns true on success
function sendMail($email, $subject, $message)
{
$supportEmail = 'support#abc.com';
$from = 'Test Application';
$msg = $message;
$from = str_replace(' ', '-', $from);
$frm = $from.' <'.$supportEmail.'>';
preg_match("<(.*)#(.*\..*)>", $frm, $match);
///////////////////Headers/////////////////
$hdr='';
$hdr.='MIME-Version: 1.0'."\n";
$hdr.='content-type: text/html; charset=iso-8859-1'."\n";
$hdr.="From: {$frm}\n";
$hdr.="Reply-To: {$frm}\n";
$hdr.="Message-ID: <".time()."#{$match[2]}>\n";
$hdr.='X-Mailer: PHP v'.phpversion();
$x=#mail($email, $subject, $msg, $hdr);
if($x==0)
{
$email=str_replace('#','\#', $email);
$hdr=str_replace('#','\#',$hdr);
$x=#mail($email, $subject, $msg, $hdr);
}
return $x;
}

Categories