/    Sign up×
Community /Pin to ProfileBookmark

difficulty with PHPMailer, it takes no values I give it

I want to create an application for sending mails using PHPMailer by placing it on my website

server (using SMTP),
but I can not get object of class phpmailer to change its properties of:

to_name
to_email
ContentType

from default (i.e. null).
Why are the properties keeping default values from the PHPMailer class, and not accepting my

once, though I check values i get from DB and they are good. ContentType is set in a config file.

Here is part of var_dump ($mailer); object of my extended class:
object(MyMailer)#2 (56) { [“priority”]=> int(3) [“to_name”]=> NULL [“to_email”]=> NULL …
[“ContentType”]=> string(10) “text/plain”…

also I get value of [“Port”]=> int(26) but [“SMTP_PORT”]=> int(25).
I need to use smtp port 26.

And here is the extended class of phpmailer

require_once ‘./PHPMailer/class.phpmailer.php’;
require_once ‘mailer-config.php’;

class MyMailer extends PHPMailer
{
var $priority = 3;
var $to_name = null;
var $to_email = null;
var $From = null;
var $FromName = null;
var $Sender = null;
var $ContentType = null;

function MyMailer()
{
global $eSlanje;

// Comes from mailer-config.php $eSlanje array

if($eSlanje[‘smtp_mode’] == ‘enabled’)
{
$this->Host = $eSlanje[‘smtp_host’];
$this->Port = $eSlanje[‘smtp_port’];
if($eSlanje[‘smtp_username’] != ”)
{
$this->SMTPAuth = true;
$this->Username = $eSlanje[‘smtp_username’];
$this->Password = $eSlanje[‘smtp_password’];
}
$this->Mailer = “smtp”;
}
if(!$this->From)
{
$this->From = $eSlanje[‘from_email’];
}
if(!$this->FromName)
{
$this->FromName = $eSlanje[‘from_name’];
}
if(!$this->Sender)
{
$this->Sender = $eSlanje[‘from_email’];
}
$this->Priority = $this->priority;
//echo “<br>eSlanje[content_type]”;
//var_dump ($eSlanje[‘content_type’]);
$this->ContentType = $eSlanje[‘content_type’];
}

}

and here is the cofig file:

mailer-config.php

<?php
$eSlanje[‘from_email’] = “[email protected]“;
$eSlanje[‘from_name’] = “Joe Blog”;
$eSlanje[‘title’] = “hi there”;
$eSlanje[‘content_type’] =”text/html”;

$eSlanje[‘smtp_username’] = null;
$eSlanje[‘smtp_mode’] = “enabled”;
$eSlanje[‘smtp_port’] = 26;
$eSlanje[‘smtp_host’] = “mail.mysite.com”;

Here is where I insert values of email and name into the object:
<?php session_start();
include (“password.php”);
require_once ‘class_lib/MyMailer.php’;
require “class_lib/MySQL_Database.php”;
require “get_email_template.php”;

$db = new MySQL_Database ();

$id = isset($_GET[‘id’]) ? $_GET[‘id’] : false;
$sql = “select email,ime from “.TABLE.” where id = $id”;
$query = mysql_query ($sql);
$toClient = mysql_fetch_array($query);

$to = isset($toClient[’email’]) ? $toClient[’email’] : false;

var_dump($to); // here I get a valid email address

$to_name = isset($toClient[‘ime’]) ? $toClient[‘ime’] : false;

//var_dump($to_name); // here I get a valid preson’s name

$mailer = new MyMailer();
$mailer->AddAddress($to, $to_name);
$mailer->Body = get_htmlBody();
$mailer->AltBody = get_txtBody();
if(!$mailer->Send())
{
echo “<br> Mailer Error: ” . $mailer->ErrorInfo.”&nbsp”;
}
else {
echo “<br>message not sent &nbsp”;
}
$mailer->ClearAddresses();

var_dump($mailer); // this is where the object trace comes from

?>

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 13.2011 — I might approach it this way:

MailConfig.php:
[code=php]
class MailConfig
{
public $from_email = "[email protected]";
public $from_name = "Joe Blog";
public $title = "hi there";
public $content_type ="text/html";
public $smtp_username = null;
public $smtp_mode = "enabled";
public $smtp_port = 26;
public $smtp_host = "mail.mysite.com";
public $smtp_password = null;
}
[/code]

MyMailer.php:
[code=php]
require_once 'path/to/PHPMailer/class.phpmailer.php';
class MyMailer extends PHPMailer
{
public function __construct(MailConfig $mc, $exceptions = false)
{
parent::__construct($exceptions);
$this->From = $mc->from_email;
$this->FromName = $mc->from_name;
$this->Subject = $mc->title;
$this->ContentType = $mc->content_type;
if($mc->smtp_mode == "enabled") {
$this->IsSMTP();
$this->Host = $mc->smtp_host;
if(!empty($mc->smtp_port)) {
$this->Port = $mc->smtp_port;
}
if(!empty($mc->smtp_username)) {
$this->Username = $mc->smtp_username;
if(!empty($mc->smtp_password)) {
$this->SMTPAuth = true;
$this->Password = $mc->smtp_password;
}
}
}
}
}
[/code]

Test script:
[code=php]
require_once 'path/to/MailConfig.php';
require_once 'path/to/MyMailer.php';

$to = "[email protected]";
$toName = "Joe Blow";
$test = new MyMailer(new MailConfig());
$test->AddAddress($to, $toName);
echo "<pre>".print_r($test, true)."</pre>";
[/code]
Copy linkTweet thisAlerts:
@RanZoauthorSep 13.2011 — Thank you NogDog, you have pointed me to the right direction.

I have used $this->ClearAddress(), to clear the email adderess, so that is why it was not showing

in the trace of the object. Comenting it out and it showed.

I could see that using print_r as it gives better visibility than var_dump.

Also introduction of the MailerConfig class in a __construct was neat.

But even with your code content type is showing this [ContentType] => text/plain.



I am also trying to mail to a list using this:

$row = mysql_fetch_object($sql);

echo "<br> row = ";

var_dump($row);

while($row = mysql_fetch_object($sql))

{

// Send the emails in this loop.

$member_name = $row->ime;

echo "<br>member_name = ";

var_dump($member_name);

$to_email = $row->email;
echo "<br>to_email = ";
var_dump ($to_email);

if($row->MailType == 'html')
{
$mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $htmlBody);
$mailer->IsHTML(true);
$mailer->AltBody = str_replace('{MEMBER_NAME}', $member_name, $textBody);
$mailer->IsHTML(true);
}
else
{
$mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $textBody);;
$mailer->isHTML(false);
}
echo "<pre>".print_r($mailer, true)."</pre>";
$mailer->Send();
//$mailer->ClearAddresses();
//$mailer->ClearAttachments();
$mailer->IsHTML(false);
echo "Mail sent to: ".$member_name."<br />";
}


But trace for $mailer object is showing neither the body nor the email nor name to send to.

I have found this in a tutorial for PHPMailer, it made no sense to me, tried it anyway,

but with no joy.

Many Thanks.
Copy linkTweet thisAlerts:
@NogDogSep 13.2011 — IIRC, PHPMailer is pretty good at automatically setting the correct content type based on what content you add. E.g.: if you use the MsgHTML() method to add your HTML text as the main body, it will automatically configure things to send the email as multi-part MIME with the HTML section marked as text/html (or whatever the correct value is). That's the beauty of it: you don't have to worry about that level of detail.
Copy linkTweet thisAlerts:
@RanZoauthorSep 13.2011 — That is fine.

Do you know by any chance why the $mailer object in the example above does not show any emails, names of persons message was sent to nor the message body, when using print_r function?

Much Obliged.
Copy linkTweet thisAlerts:
@NogDogSep 13.2011 — First: you can help us to help you by using [noparse][code=php]...[/code][/noparse] tags around your code snippets, which will make them much easier to read.

If you are going to do a mysql_fetch_*() to get debug info, and then do it again in your while() loop, before starting the loop, you'll want to do a mysql_data_seek() to row 0 (zero) in order to reset the result row pointer back to the first result.

Also, I don't see you setting the "To:" address anywhere? Assuming you want to use the AddAddress() method, it might be something like this (untested):
[code=php]
$row = mysql_fetch_object($sql);
echo "<br> row = ";
var_dump($row);
mysql_data_seek($sql, 0); // reset back to first record

while ($row = mysql_fetch_object($sql)) {
// Send the emails in this loop.
$member_name = $row->ime; // is "ime" the correct property name?
echo "<br>member_name = ";
var_dump($member_name);
$to_email = $row->email;
echo "<br>to_email = ";
$mailer->AddAddress($to_email, $member_name); // You need to add the TO address
var_dump($to_email);
if ($row->MailType == 'html') {
$mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $htmlBody);
$mailer->IsHTML(true);
$mailer->AltBody = str_replace('{MEMBER_NAME}', $member_name, $textBody);
$mailer->IsHTML(true);
} else {
$mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $textBody);;
$mailer->isHTML(false);
}
echo "<pre>" . print_r($mailer, true) . "</pre>";
$mailer->Send();
$mailer->IsHTML(false); // this seems unnecessary?
echo "Mail sent to: " . $member_name . "<br />";
$mailer->ClearAddresses(); // you still want to do this, just do it after the debug
$mailer->ClearAttachments(); // ditto, in case you ever do add attachments
}
[/code]
Copy linkTweet thisAlerts:
@RanZoauthorSep 14.2011 — Thank you so much top dog genius, everithing is good now.

One final question if I may, object $mailer is instanciated before the while loop.

Yet in the trace, I can see there are several objects (depending on the quiery), each with its email, name of person, and message body.

I do not understand it. We instanciated only one object (outside of the loop).
Copy linkTweet thisAlerts:
@RanZoauthorSep 14.2011 — I have got ahead of myself when I said everithing works.

Well trace of the mailer object has looked right on localhost, but when I placed it my website it

sent no emails.

Here is how it looks:

SMTP Error: Could not authenticate.

Mailer Error: SMTP Error: Could not authenticate.

MyMailer Object

(

Priority] => 3

[CharSet] => iso-8859-1

[ContentType] => multipart/alternative

[Encoding] => 8bit

[ErrorInfo] => SMTP Error: Could not authenticate.

[From] => [email][email protected][/email]

[FromName] => Joe Blogs

[Sender] =>

[Subject] => Hi there ...

[WordWrap] => 0

[Mailer] => smtp

[Sendmail] => /usr/sbin/sendmail

[PluginDir] =>

[ConfirmReadingTo] =>

[Hostname] =>

[MessageID] =>

[Host] => mail.mysite.com

[Port] => 26

[Helo] =>

[SMTPSecure] =>

[SMTPAuth] => 1

[Username] => my-smtp-username (this is same as my email / [email][email protected][/email])

[Password] => my-smtp-password (pass for the cpanel)


as well as I can tell it looks all right, except for the error message offcourse.



here is the config file for mailer:
[code=php]
<?php
class MailerConfig
{
public $from_email = "[email protected]";
public $from_name = "my name";
public $title = "hi there";
public $content_type ="text/html";
public $smtp_username = "my-smtp-username";
public $smtp_mode = "enabled";
public $smtp_port = 26;
public $smtp_host = "mail.nivodesign.com";
public $smtp_password = "my-smtp-password";
}
[/code]

and the mailer extend class
[code=php]
<?php
require_once './PHPMailer/class.phpmailer.php';
require_once 'MailerConfig.php';

class myMailer extends PHPMailer
{
public function __construct(MailerConfig $mc, $exceptions = false)
{
parent::__construct($exceptions);
$this->From = $mc->from_email;
$this->FromName = $mc->from_name;
$this->Subject = $mc->title;
$this->ContentType = $mc->content_type;
if($mc->smtp_mode == "enabled") {
$this->IsSMTP();
$this->Host = $mc->smtp_host;
if(!empty($mc->smtp_port)) {
$this->Port = $mc->smtp_port;
}
if(!empty($mc->smtp_username)) {
$this->Username = $mc->smtp_username;
if(!empty($mc->smtp_password)) {
$this->SMTPAuth = true;
$this->Password = $mc->smtp_password;
}
}
//$this->SmtpConnect(); this just errors out no SMTP class at class.PHPMailer.php on line
//774
}
}
}
[/code]
Copy linkTweet thisAlerts:
@NogDogSep 14.2011 — Make sure the SMTP password is correct: it is your email password for the provided email address you are using for that SMTP mail server, which may not be the same as your cpanel/login password.
Copy linkTweet thisAlerts:
@RanZoauthorSep 14.2011 — I have been using the same password to connect using Outlook. So I beleve it is the same one, not sure how to check?
Copy linkTweet thisAlerts:
@NogDogSep 14.2011 — I have been using the same password to connect using Outlook. So I beleve it is the same one, not sure how to check?[/QUOTE]

That should be okay, then. Maybe try setting SMTPDebug to 2, to see if you can get more info? (See http://phpmailer.worxware.com/index.php?pg=exampleasmtp for an example of using exceptions and try/catch.)

Also, I see in your sample data here that SMTPSecure is not set. Maybe it needs to be for your particular SMTP configuration?
×

Success!

Help @RanZo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...