SMTP Sendmail With PEAR not using formating from database - php

I am using PEAR to send an email with smtp authentication.
Currently i store the email with formatting in a database and then send it out from there. The email is sending perfectly. But the email body is not being formatted. instead it is taking whatever is in the database and using it directly as plain text not HTML formatted text.
Is there anyway to resolve this issue?
function sendmail($from,$to,$subject,$body,$host,$username,$password){
$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, $body);
if (PEAR::isError($mail)) {
echo "<p>" . $mail->getMessage() . "</p>";
} else {
echo "<p>Message successfully sent!</p>";
}
}
right now $body = <p> Lets make this an epic test mail. and see what happens?</p> so it should remove the tags and just place it in a email.
But instead i get the email with the text being <p> Lets make this an epic test mail. and see what happens?</p>

if you want HTML email then you need to add the header:
'Content-type' => 'text/html;charset=iso-8859-1'
your charset may vary

Related

PHP Not sent automatic email on remote server

I wrote codes that should send auto email using PEAR. every thing is OK on my local MAC OS system but not working on windows based remote server. here is my codes:
<?php require_once('../config/connection.php');
include_once("Mail.php");
$id=$_GET['id'];
if(isset($_POST['update-visa-status-btn'])){
$status=$_POST['status'];
mysqli_query($con,"UPDATE `visa` SET `status`='$status' WHERE `id`='$id'");
$visa=mysqli_query($con,"SELECT * FROM `visa` WHERE `id`='$id'");
$row_visa=mysqli_fetch_assoc($visa);
$user_id=$row_visa['userid'];
$user_info=mysqli_query($con,"SELECT * FROM `user` WHERE `id`='$user_id'");
$row_user_info=mysqli_fetch_assoc($user_info);
$From = "...";
$To="...";
$Subject = "...";
$Message = '
<html>
<head>
</head>
<body>
<!-- SOME HTML CODES THAT MAKES HTML EMAIL BODY -->
</body>
</html>';
$Host = "...";
$Username = "...";
$Password = "...";
$content_type="text/html";
$Headers = array ('From' => $From, 'To' => $To, 'Subject' => $Subject, 'content-type' => $content_type);
$SMTP = Mail::factory('smtp', array ('host' => $Host, 'auth' => true,
'username' => $Username, 'password' => $Password));
$mail = $SMTP->send($To, $Headers, $Message);
if (PEAR::isError($mail)){
echo($mail->getMessage());
} else {
echo("Email Message sent!");
}}
?>
On remote server none of messages success or error not shown & no email sent but on local, every thing works like a charm.Thank you for your help.
First please escape the GET and POST variables:
$escaped_string = mysqli_real_escape_string($connection, $string)
Secondly: For Email sending purpose use PHPMailer a free repository to send emails. You can get this from github: https://github.com/PHPMailer/PHPMailer There you will also find the Process you need to follow to get your emails sent.
I'm using this PHPMailer for ages now. It is easy, robust and hardly fails on me.

PHP Html Email using Pear HTML Table and Pear Email

I am facing quite strange issue, I've php script which sends out an email to the list of users containing HTML table(with the data) and CSV attachment of the same data. The problem is when that script to send out more then 10 emails, in some of the emails we get
--=_f3be233a9b22e88524e1539166c49be0 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=UTF-8
in email body and HTML table lost the formatting as well, however CSV data is absolutely fine in that email.
Basically the purpose of the script is to pick the data according to user subscribe information(database-table) and send it to the subscribe user early morning. Every night the database tables are updated with the new data, which is picked up by that PHP script and send in an email. Further that PHP script trigger by Windows Schedule task.
Below is the code of the application.
if($ix>0)
{
while(odbc_fetch_row($subRes2))
{
$s_email=odbc_result($subRes2, 1);
//echo $s_email;
$from = "Sender <alerts#xxx.com>";
$to = "$s_email";
$subject = "Information_DB:" . $arrayE . " Restored on: " . "-";
$host = "xxxinternal.xxxx.com";
$port = "25";
//disabled the username and password because currently sending unauthenticated email.
// $username = ""; //<> give errors
//$password = "";
//add the email headers 29082013 1008 - SMS
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8');
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => false));
//'username' => $username,
//'password' => $password));
$subject = "Results from query";
$crlf = "\n";
$mime_params = array(
'text_encoding' => '7bit',
'text_charset' => 'UTF-8',
'html_charset' => 'UTF-8',
'head_charset' => 'UTF-8'
);
// Creating the Mime message
$mime = new Mail_mime($crlf);
// Setting the body of the email
//$mime->setTXTBody($body);
$mime->setHTMLBody($body);
$file='./wamp/www/' . $csv_filename;
$mime->addAttachment($csv_filename,'application/octet-stream');
$body = $mime->get($mime_params);
$headers = $mime->headers($headers);
// Sending the email
// $mail =& Mail::factory('mail');
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
// echo "<script>window.close()</script>";
}
else {
echo("<p>Message successfully sent!</p>");
}
}}}
else
{
$from = "Sender <alert#xxx.com>";
$to = "$s_email";
$subject = "Information_DB:" . $arrayE . " Restored on: " . "-";
//$host = "ssl://smtp.gmail.com";
$host = "xxxinternal.xxxx.com";
$port = "25";
//disabled the username and password because currently sending unauthenticated email.
// $username = ""; //<> give errors
//$password = "";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8');
?>
Unfortunately when we tested I could't seen this issue. But when its went UAT lots of user get this issue and it seems to be consistent with the database. For e.g. if user A is subscribe with Spain and Russian data, then consistently they experience the issue with Russian Data email (contains the mentioned headers in the email) however Spain data email is fine. In the live its probably sending around 100's of email daily.
I will appreciate if any of you please point me out what is going wrong as I am setting up the UTF-8 character set in an email mime setup but still getting this issue.
Regards
Shoaib
Most probably you are sending emails too fast that may occur in the result of spam/unwantd email use sleep(4) to create the 4 sec delay in next `mail.
The problem was with mime type, which require to code again. Basically the message I was sending was not encoded with UTF-8 which I did. This time I user Mail_mimePart instead of Mail_mime and it works treat.
That example was very very helpful
---ahttp://tiger-fish.com/comment/reply/133---
and some of the parameter explanation for Mail_mimePart can be found on below link
ahttp://pear.php.net/manual/en/package.mail.mail-mimepart.mail-mimepart.php---
How to attach the attachment using Mail_mimePart.
ahttp://pear.php.net/manual/en/package.mail.mail-mimepart.addsubpart.php---
Hope it helps.

PHP: PEAR email sends only on second pass

I am using pear to send email using the code below, on the first time through I get this error every time:
"Failed to set sender: aa#bb.com [SMTP: Invalid response code received
from server (code: -1, response: )]"
If I debug and set the execution point back to 'Re-run from here' after the send, it works fine - almost like something is not initialised properly first time round - anyone have any ideas on this?
require_once "Mail.php";
function SendEMail() {
$from = "Sender Sender <sender#example.com>";
$to = "AA BB <aa#bb.com>";
$subject = "Test";
$body = "This is a test";
$host = "myhost";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
//Re-run from here
$smtp =& Mail::factory('smtp',
array ('host' => $host,
'auth' => false,
'username' => '',
'password' => ''));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$s = $mail->getMessage();
}
}
Use wireshark to find out which response you really get from the server. Maybe the server has activated greylisting, so that you can't send a mail the first time.

pear mail function bcc won't send

I copied a code for PEAR mail from a website, and input my data. It works. It sends mail, however, I want to use bcc to send to a lot of people and keep their addresses anonymous, and it will send to the $to recipients, but not the $bcc.
The code:
<?php
$message = "yay email!";
require_once("Mail.php");
$from = 'myaddress#mysite.com ';
$to = "anadress#gmail.com";
$bcc = "thepeopleimemailing#yaddayadda.com";
$subject = " test";
$body = $message;
$host = "smtp.mysite.com";
$username = "myusername";
$password = "mypassword";
$headers = array ('From' => $from,
'To' => $to,
'Cc' => $cc,
'Bcc' => $bcc,
'Subject' => $subject
);
$recipients = $to;
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
'port' => '25'
)
);
$mail = $smtp->send($recipients, $headers, $body);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
}
else {
echo("Message successfully sent!");
}
?>
P.s. I read on anther forum that I shouldn't put the headers in an array? I'm having trouble grasping the concept of the headers. What do they do and how should I organize them? I just want a to, from, subject, and bcc.
Thanks!
To elaborate on Chaky31's answer to send a Bcc use the following, note that we do NOT specify any Bcc information in the header:
//All other variables should be self explanatory!
//The main recipient
$to = "test#test.com";
//Bcc recipients
$bcc = "bcc#test.com";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
//We append the bcc addresses as comma seperated values to the send method
$mail = $smtp->send($to . "," . $bcc, $headers, $body);
For those who are looking to the solution to adding cc and bcc in PEAR php mail. Here is the simple solution and the abbreviated explanation why.
ANSWER: Everyone who want to receive the mail must be added to the $recipients field. If they are not in this field, they will not get the mail. Everything you want to be visible, add to the header. Therefore, since bcc is BLIND carbon copy, do NOT add it to the header.
WHY?: The recipient field dictates where the mail goes, the headers dictate what is displayed. If you do not add cc to the header, then you can make them blind too. Whichever tickles your fancy. Any questions, check the link ripa added above! Great explanation!
use $headers['Cc'] = 'cc#example.com, bb#example.com, dd#ex.com';
see the link below for pear mail
Sending multiple CC's and BCCs with PHP PEAR MAIL
or can get help from
http://phpmailer.worxware.com/index.php?pg=exampledb -- it is not pear mail. but it works very fine. I have used this and it is very easy to integrate.

Looping email code

I've been using the code below for years to send email from my site, but the problem is that it sends the mail many times sometimes.
Example: if I send a message to my users, some may get it 5 times, etc. even when I send to one person only, he may get it 3 times.
require_once "Mail.php";
$from = "xxx Support <noreply#xxxx.com>";
$to = "$name <$email>";
$subject = "xxxxx";
$body = "xxx";
$host = "mail.xxx.com";
$username = "noreply+xxx.com";
$password = "xzcsd=sfd?hdssc";
$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, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}`
I think you may need to complicate your solution a bit to add tracking. For instance add a new table in your DB that has a record for each email sent.
The table structure may be something like
email_id (PK)
email_source (FK to table including emails)
user_id (FK to users table)
Then you can send the email to those users who did not receive this version of the email.
This can help you to off-load your SMTP, or send the email in bulks distributed over the day. As well this can help you to track the users who open the email, or interacted with the email.
Hope that helps.

Categories