PHP Mail adding CC - php

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.

Related

PHP Pear Mail HTML Version

I'm able to send emails with text only, but none of the scripts are working to send HTML. I have authentication off, and google apps is authenticating based on the server IP
$message = 'Blah blah blah';
require_once "Mail.php";
$from = "mycompany <service#mycompany.com>";
$to = $fName.$lName." <".$email.">";
$subject = "Hello from mycompany!";
$body =
'Dear '.$fName.',
Thank you for your interest in mycompany. We have received your inquiry and will contact you within 24 hours.
Thanks,
Timothy Elliott - Owner/CEO mycompany';
$host = "tls://smtp-relay.gmail.com";
$port = "465";
$username = "";
$password = "";
$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => false,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("There was an error, please try again.");
}
This code works just fine, but I'm not able to send it as an HTML message.
It's easy to do with with PEAR Mail - you just need to use PEAR Mail_Mime for encoding the HTML portion of your outgoing emails.
Here's an example of how.
Expanding your code for this, it should look something like:
<?php
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "mycompany <service#mycompany.com>";
$to = $fName.$lName." <".$email.">";
$subject = "Hello from mycompany!";
$html = <<< HTML
<b>Dear $fName</b>,
<p>
Thank you for your interest in mycompany. We have received your inquiry and will contact you within 24 hours.
</p>
Thanks,<br/>
<i>Timothy Elliott - Owner/CEO mycompany</i>
HTML;
$host = "tls://smtp-relay.gmail.com";
$port = "465";
$username = "";
$password = "";
$headers = [
'From' => $from,
'To' => $to,
'Subject' => $subject
];
$crlf = "\n";
$mime = new Mail_mime(['eol' => $crlf]);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
[
'host' => $host,
'port' => $port,
'auth' => false,
'username' => $username,
'password' => $password
]
);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("There was an error, please try again.");
}

Pear/Mail Send array in body.

I have two problems, When I use the PEAR/Mail to send a form to my email, the email is received but it only allows me to send 3 variables. That said, I'm trying to send an array of value, but my script only prints the word "Array" on the message body. Here is my code:
require_once "Mail.php";
$from = $_POST['email'];
$to = "xxxxxxxx";
$subject = $_POST['Evento'];
$name = $_POST['Nombre'];
$body = $_POST['mensaje'];
$host = "xxxxxxx";
$username = "xxxxxxx";
$password = "xxxxxxx";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $name, $body);
This code connects and sends the email fine, but only displays up to the third variable ($name) if I switch the display to $body. So I thought, lets make an array and send all the message in one variable. So I made this:
require_once "Mail.php";
$from = $_POST['email'];
$to = "xxxxxxx";
$subject = $_POST['Evento'];
$name = $_POST['Nombre'];
$body = $_POST['mensaje'];
$host = "xxxxxxx";
$username = "xxxxxxx";
$password = "xxxxxxx";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$message=array($_POST[Nombre],$_POST[Evento],$_POST[fecha],$_POST[telefono],$_POST[mensaje]);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $message);
This one displays "(Array)" in the body.
I need some advise or route to follow.
$smtp->send take a string for the body argument, not an array (you can see that here: http://pear.php.net/manual/en/package.mail.mail.send.php). You should use PHP implode first to create a string from your array and then pass that as the argument e.g.
$str = implode("\n", $message);
$mail = $smtp->send($to, $headers, $str);

Sending mail via pear mail

Sorry for my English.
I’m trying to sent e-mai via Mail pear packet:
require_once "Mail.php";
$from = '<frommail#gmail.com>';
$to = '<tomail#vacant.lv>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'frommail#gmail.com',
'password' => 'pass'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
//email addresses are changed
But in result I have error:
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: Permission denied (code: -1, response: )]
Openssl is enabled.
Thanks you.
At some stage you might want to send an HTML email, so you would also need to use the Mail/mime package that is provided by PEAR. I've included that as well.
<?php
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "<noreply#example.com>";
$to = "fred#example.com"; // the email address
$host = "smtp.gmail.com";
$port = "587";
// use the first example username if sending from gmail, as full email address
// is required
$username = "fred.flintstone#gmail.com";
$username = "fred";
$password = "secure";
$headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
$mailbody = "<html><body>...</body></html>";
$mime = new Mail_mime();
$mime->setHTMLBody($mailbody);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory(
'smtp',array (
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
'port' => $port
)
);
// send email
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo "<b><Center>Succesfully sent email to</b>$to</center>";
}

Saving data to online text document

I have an issue which is i want to save the data to a online shared text document on google drive.
but it's not working.
There is No error , it's just not saving anything into the document and i've already gived the document the full access by public.
////// email code
require_once "Mail.php";
$from = "Babylovenappies <****#gmail.com>";
$to = "nassim#*****.com";
$recipients = $to;
$subject = $subject;
$body = $message;
$host = "ssl://smtp.googlemail.com";
$port = "465";
$username = "****#gmail.com";
$password = "****";
$headers = array ('From' => $from,
'To' => $to,
'Bcc' => "admin#***.com.au",
'Subject' => $subject,
'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));
$mail = $smtp->send($recipients, $headers, $body);
$fh = fopen('https://docs.google.com/document/d/1dS_MhqGnLkb22mwU6OKfxoiFcX0izRTvjjf8eJ7igiE/edit?usp=sharing', "a");
fwrite($fh, $to);
fclose($fh);
Thanks In Advance
You definitely should use API
Look at this:
Files: update

Date not in mail

I have the following script to send email using SMTP authentication. Everything works fine but date is not shown in my inbox for this mail. It is marked as '?'. Can anybody help me?
require_once "Mail.php";
$from = "test#example";
$to = $mailto;
$subject = $subject;
$body = $msg;
$host = "test.com";
$username = "uname";
$password = "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);
if (PEAR::isError($mail)) {
//echo("<p>" . $mail->getMessage() . "</p>");
return false;
} else {
//echo("<p>Message successfully sent!</p>");
return true;
}
The from,host,username and password values shown here are only test values for security.
Is it any problem with mail headers?
solved it? this worked for me, i just added in the headers array:
'Date' => date('r', time()),

Categories