Pear PHP Mail sending CC doesn't seem to work - php

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);

Related

Set time or sending limit for PHP mailing script

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!

PHP Mail adding CC

I have this PHP Mail Function:
if(!function_exists("sendemail"))
{
function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto)
{
if(filter_var($email_to, FILTER_VALIDATE_EMAIL))
{
require_once "/usr/local/lib/php/Mail.php";
$from = $email_from;
$to = $email_to;
$subject = $email_subject;
$body = $email_body;
$host = "mail.domain.co.uk";
$username = "sending#domain.co.uk";
$password = "*******";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-type' => 'text/html');
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body, $cc);
}
}
}
I have added in the ability to CC Email addresses in:
if(!function_exists("sendemail"))
{
function sendemail($email_to,$email_from,$email_subject,$email_body,$email_replyto,$cc)
{
if(filter_var($email_to, FILTER_VALIDATE_EMAIL))
{
require_once "/usr/local/lib/php/Mail.php";
$from = $email_from;
$to = $email_to;
$subject = $email_subject;
$body = $email_body;
$host = "mail.domain.co.uk";
$username = "sending#domain.co.uk";
$password = "*******";
$headers = array ('From' => $from,
'To' => $to,
'Cc' => $cc,
'Subject' => $subject,
'Content-type' => 'text/html');
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body, $cc);
}
}
}
but it only seems to add the CC'd email address(es) into the header and not send the email. it only sends the email to the address in the $email_to variable
Any ideas how i can get the CC working?
I'm assuming you are using the Pear class Mail, If not disregard.
According to this (http://pear.php.net/manual/en/package.mail.mail.send.php#2073) you need to have the email address you want to cc to in the receipients and the headers. I personally have never used this code so I can't attest to whether it works or not.
Here is another link saying the same thing: http://raamdev.com/2008/adding-cc-recipients-with-pear-mail/
Pulled from link:
$to = 'to#example.com';
$cc = 'cc#example.com';
$recipients = $to.", ".$cc;
$headers['From'] = 'from#example.com';
$headers['To'] = $to;
$headers['Subject'] = 'Test message';
$headers['Cc'] = 'cc#example.com';
$headers['Reply-To'] = 'from#example.com';
$send = $mail->send($recipients, $headers, $body);
Also just in case you ever need it according to the link to BCC you simply add an email address to the $recipients and don't add it to the $headers.

Pear PHP Mailer Emails Sender and Recipient

I'm using Pear PHP Mailer to send an email through SMTP to a number of recipients, but for each one, it sends an additional email to the sender's email address as well.
This is my code: Any help is appreciated.
...
$from = "$from_email_name <$from_email_addr>";
$html_body = "$html";
$crlf = "\r\n";
$hdrs = array(
'From' => $from,
'Subject' => $subject
);
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text_body);
$mime->setHTMLBody($html_body);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail = Mail::factory('mail',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail->send($to_email_addr, $hdrs, $body);
....
Also, on the email it is sending to my sender address, its saying the correct recipient name and sender information, but its like the sender is getting a copy of each one it sends out.
It could be a "service" of your mail server provider to automatically put mails sent via the SMTP server in your IMAP/POP mail box. Ask him if that's the case.

How to use own SMTP servers PHP mail

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);

Issue with html content-type header when sending an php smtp mail through PEAR

I can send emails perfectly but they are showing on plain text, I have been trying a lot of solutions to display it as HTML trying to put content-type but when I check the headers from the email sent MIME and Content-type headers are missing. What I am doing wrong?
Thank you!
$email = "example#test.com";
$subject = "example subject\r\n";
$msg_body = "example body with some html";
$host = "mail.examplehost.com";
$username = "exampleemail#test.com";
$password = "examplepass";
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$headers = array ('From' => "exampledomain.com",
'To' => $to,
'Subject' => $subject
'MIME-Version' => "1.0",
'Content-type' => "text/html; charset=iso-8859-1"
);
$mail = $smtp->send($to,$headers,$msg_body);
In the headers array, you seem to be missing a comma after $subject

Categories