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.
Related
I have PHP Send Email using SMTP.
$from = "Info <donotreply#test.com>";
$subject = "Calibration will be expiring!";
$body = 'Hello';
$to = "david#test.com, dono#test.com";
$cc = "rena#test.com";
$bcc = ""; //sometimes BCC can be empty
$host = 'smtp.test.com';
$port = '587';
$username = 'donotreply#test.com';
$password = 'dontreply?';
$headers = array(
'Port' => $port,
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8',
'Cc' => $cc
);
$recipients = $to.", ".$bcc.", ".$cc;
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($recipients, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
I tried to run above code and only working if $bcc is not empty.
Error when $bcc is empty
Failed to add recipient: #localhost [SMTP: Invalid response code received from server (code: 501, response: 5.1.3 Invalid address [HKXPR02MB0695.apcprd02.prod.outlook.com])]
Is there any way how to fix it or need some modification there?
If $bcc variable is empty then your concatenated string contains ,, which is syntactically incorrect. You need to add some logic there to make ensure this is not happening.
Also I doubt you set BCC correctly anyway. It should not be just another recipient like you have now.
I've got the solution below:
$bcc = $dEmailListBCC['EMAIL_ADDRESS'];
if($bcc == "")
{
$a = "";
}
else
{
$a = ",".$bcc;
}
First, check the $bcc variable is empty or not.
and then the recipients to be like this:
$recipients = $to." ".$a.", ".$cc;
I have a script I run to send emails with attachments.
On my old server it worked perfectly, But since moving a few days back. The script no longer works. As soon as it is supposed to send a mail, it bombs out.
I did find this error in the error log file,How can I locate the problem?
[08-Jul-2015 12:39:17 UTC] PHP Fatal error: Call to undefined function mime_content_type() in /home/username/public_html/editor/cronjob.php on line 79
My code more or less looks like the below:
function sendmailwithattachment($from,$to,$subject,$body,$host,$username,$password,$body_id,$port) {
$query = "SELECT * FROM `email_attachments` where `email_id`='$body_id';";
if($result = mysqli_query($this->link, $query)){
if($result->num_rows!=0){
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$crlf = "\n";
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($body);
while ($row = mysqli_fetch_array($result,1)) {
$attachment=$row['attachment'];
$mime->addAttachment($attachment, mime_content_type($attachment));
}
$body = $mime->get();
$headers = $mime->headers($headers);
} else {
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject,'Content-type' => 'text/html;charset=iso-8859-1');
$body = $body;
}
}
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail=$smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo "<p>Message not sent:".$mail->getMessage()."</p>";
return $mail->getMessage();
} else {
//echo "<p>Message successfully sent!</p>";
return 'sent';
}
}
It looks like mime_content_type() is deprecated anyway, try replacing it with Fileinfo functions as they mention in the docs. It might solve your problem in the process (though hard to say, because we don't know what your code looks like).
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
I have this really weird issue that am troubled with and do not know how to fix it!
Sample code i used here Wroks and sends me the email correctly!
<?php
require_once "Mail.php";
$from = "Web Master <contact#sample.com>";
$to = "bob <sample#sample.com>";
$subject = "Test email using PHP SMTP\r\n\r\n";
$body = "This is a test email message";
$host = "mail.emailsrvr.com";
$username = "sample#sample.com";
$password = "11111";
$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>");
}
?>
But when i put this code into a email password reminder php code it stops working!
i have put the code at the very front or at the place where u click to send button
to trigger, nothing works and i have no idea why!
the funny part is i had it working if i slowly copy in parts of the code and it WORKS
for about the first one or two email and it stops working after...
the code that comes before the reminder.php before the email script is below
<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
if(get_magic_quotes_runtime())
{
set_magic_quotes_runtime(false);
}
include("include/dbcommon.php");
$cEmailField = "email";
$reminded=false;
$strSearchBy="username";
include('libs/Smarty.class.php');
$smarty = new Smarty();
$strUsername="";
$strEmail="";
$strMessage="";
$conn=db_connect();
// Before Process event
if(function_exists("BeforeProcessRemindPwd"))
BeforeProcessRemindPwd($conn);
Please ! if you have any idea on the solution to this?
mail("sample#sample.com","Test email","test body here");
check if this works?
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.