I have a simple php registration form that captures a name and email address. After registering, I have a confirmation email set to go to the registrant and one to go to the admin.
I've had an issue where the user only registered once, but some issue caused the email to be sent 50+ times to both the registrant and admin. It's only happened a few times, but I'm trying to prevent it from happening in the future.
I'm thinking of a time limit on running the script, but wondering the best way to handle this. Below is a snippet of the mailing portion of the script.
2 notes, in case they are of use:
I'm using PEAR Mailer
My mail is being sent via MailGun
$headers = array ('From' => $from,
'To' => $to,
'Bcc' => $bcc,
'Reply-to' => $email,
'Subject' => $subject,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1');
$headers_admin_notification = array ('From' => $from_admin_notification,
'To' => $to_admin_notification,
'Subject' => $subject_admin_notification,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1');
// credentials for SMTP Authentication included from config file
$smtp = Mail::factory('smtp', array ('host' => 'ssl://smtp.mailgun.org',
'auth' => true,
'port' => 465,
'username' => 'XXXXX',
'password' => 'XXXXX'));
$mail = $smtp->send($to, $headers, $body);
$mail_admin_notification = $smtp->send($to_admin_notification, $headers_admin_notification, $body_admin_notification);
if (PEAR::isError($mail) || PEAR::isError($mail_admin_notification)) {
echo("<p>" . $mail->getMessage() . "</p>");
echo("<p>" . $mail_admin_notification->getMessage() . "</p>");
}
else {
header("Location: LINK-TO-MY-CONFIRMATION-PAGE?fname=$fname&session=$readable_date_and_time");
}
Thanks!
Related
I'm using PEAR's mail library via SMTP on my server and whilst I can get emails to generate, adding CC's doesn't seem to work. Basically the CC recipients never get their email even though the main recipient of the same email does.
My basic setup is as follows, all the recipient variables ($to,$cc,$bcc) are string variables containing either a single recipient email addresses or comma separated email addresses.
$headers = array (
'From' => $from,
'To' => $to,
'Cc' => $cc,
'Bcc' => $bcc,
'Subject' => $subject,
'Reply-To' => $from,
'X-Mailer' => 'PHP/' . phpversion(),
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=ISO-8859-1'
);
$smtp = Mail::factory('smtp', array (
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password
));
$result = $smtp->send($to, $headers, $message);
I've read that sending BCCs is more complex, so lets stick to the CCs... why aren't they being received ? Is there anything obvious that I'm doing wrong here ?
In order to send an email to CC or BCC with SMTP, you must list all email addresses both under recipients to the send() function and in the CC key within the header.
$to = "john#example.com";
$cc = "doe#example.com";
$recipients = $to . ", " . $cc;
$headers["From"] = "john#example.com";
$headers["To"] = $to;
$headers["Subject"] = "Hello World!";
$headers["Cc"] = "doe#example.com";
$headers["Reply-To"] = "john#example.com";
$send = $mail->send($recipients, $headers, $body);
I have the following problem. I have an online application form in HTML posting to a php file. The php file should send the contents via email. It works perfectly except when the client's name posting has special characters such as ΓΌ etc. It does not give any errors on form submission it just does not send the email.
This is my code:
$host = "192.168.10.14";
$username = "exxxx#xxx.com";
$password = "xxxxx";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = new Mail_mime(array("text_charset" => "utf-8",
"html_charset" => "utf-8",
"eol" => "\n"));
foreach ($headers as $name => $value){
$headers[$name] = $mail->encodeHeader($name, $value, "utf-8",
"quoted-printable");
}
// also encode to value
$to = $mail->encodeHeader("to", $to, "utf-8", "quoted-printable");
// fetch message
$msgDone = $mail->get();
// let Mail_Mime finish the headers (adds e.g. MIME info)
$headers_done = $mail->headers($headers);
// send the email
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail2)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
header("Refresh: 0;url=OK.html");
}
It works perfectly unless there are special characters. When there are special characters it does not send the email at all without any warnings.
Thanks
Have you tried cleaning your input before allowing it to be sent?
Would you allow an apostrophe to be entered in to an SQL statement without escaping it? Also check your mail logs on the server - that should provide a better hint to the underlying issue.
Thanks,
//P
I am using default PHP mail() function to send emails as below . I have two email servers. How can I set my code to use these two mail server? I am running my PHP code on Linux.
mail($currEmail, $HOT_EMAIL_SUBJECT, $body, 'From: '.$pollsConfig_senderEmail);
Use PEAR::Mail, you can specify a SMTP server with it.
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$obj = Mail::factory ('smtp',
array ('host' => $host,
'port' => $port));
$obj->send ($to, $headers, $body);
I am trying to edit PHP code that sends an email using PEAR. The below code has worked on the company server, but doesn't seem to work on my computer when I am using localhost.
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
//Send the Actual Mail
$smtp = Mail::factory('smtp',
array ( 'host' => $host,
'auth' => false,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail))
{
echo($mail->getMessage());
}
else
{
echo("<p>Message successfully sent!<p>");
}
The items in $headers are defined earlier, don't worry.
The error returned by echo($mail->getMessage()); is as follows:
Failed to connect to localhost:25 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
I'm on a machine running Ubuntu. Not sure if any other information is needed.
Recipients named in the BCC/CC (in the headers) are not received.
I've found a couple of posts with similar questions, no answers...
The code is below, the question is: "Have any of you had similar problems?"
require_once "Mail.php";
$host = "mail.mailserver.com";
$username = "notification#yourhost.com";
$password = "getyourownpassword";
$headers = array ('From' => "User Name <$username>",
'To' => $to_,
'Cc' => 'Patty <patty#gmail.com>',
'Subject' => $subj_,
'Content-type' => 'text/html');
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to_, $headers, $mail_msg);
It looks like you're using the PEAR mail module.
if you read here You'll see a discussion about the headers passed to the pear module only specifies how the message looks, not to who actually gets it. If you add a CC header, that person will show up as being CC'd but to actually receive it he needs to be added to the recipients array. For BCC, you add them to recpients array, but don't show them in the header.