im trying to use joomla mailer to send emails but it doesent work, any suggestions? what am i missing? i've searched around the web and SO, but it didnt help.
the only error i get is 500, and i cant understand why...
this is the actual code:
<?php
//framework joomla
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__) . "/../"));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
$mainframe =& JFactory::getApplication('site');
//get vars
$session =& JFactory::getSession();
$num1 = $session->get('variable1');
$num2 = $session->get('variable2');
$val= $session->get('variable3');
$uq= $session->get('unique');
$sendto= $session->get('mail');
//mail
$mailer =& JFactory::getMailer();
$mailer->setSender('some1#domain.com');
$recipient = array($sendto, 'some1else#domain.com');
$mailer->addRecipient($recipient);
$body = '<h2>sometext</h2>'
. '<div>sometext</div>'
. '<div> blabla' echo $num1 'blabla </div>'
. '<div> texttext' echo $num2 'texet </div>'
. '<div> texttext' echo $val 'text </div>';
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
$mailer->AddEmbeddedImage("$uq".".gif", "image_0", "", "base64", "image/gif");
$mailer->addAttachment("$uq".".gif");
//send
$send =& $mailer->Send();
if ( $send !== true ) {
//Elimina .gif
$mask = "*.gif";
array_map( "unlink", glob( $mask ) );
unset($mailer);
echo 'error: ' . $send->message;
} else {
unset($mailer);
//Elimina .gif
$mask = "*.gif";
array_map( "unlink", glob( $mask ) );
echo 'done';
}
?>
forgot to add specs about the platform:
PHP Version 5.3.22
Joomla! Version 2.5.9
if you need any other info just ask.
update:
i tried another joomla! version, a simplified version of the code without variables and with a single email, plain text emails... nothing seems to work.
internal server error (500) with a blank error log... (wierd, but the installation it's inside a subdirectory with many other installation, and my webhosting allow me to see only the "root" log, so i think this is the problem of the blank error log...)
Not quite sure whether this is the root of your problem. But $mail->setSender() accepts an array. Example : $mail->setSender(array('SENDER EMAIL', 'SENDER NAME'));
AND
$mail->addRecipient() accepts a string OR an array. Use an array if you are sending the email to multiple recipients and the string when you are sending the email to only 1 recipient.
Hope this helps
ok, solved!
this might help someone else so, here is how:
$config =& JFactory::getConfig();
this line grabs the email configuration of Joomla, without this Jmail will not send emails.
//body email
$body .= "<h2>Texttext</h2>" . "\n";
$body .= "<div>TexttextTexttextTexttext" . $blockh . " Texttext " . $blockd . "\n";
$body .= "<div> TexttextTexttext <span> " . $num1 . " </span> Texttext </div>" . "\n";
$body .= "<div>TexttextTexttextTexttext <span> " . $val . " </span> Texttext </div>";
previus concatenation of the body was not working, it seems that double quotes are needed to work...
$mailer->isHTML();
the "true" inside is not necessary since true is the default value..
In answer to KentaS comment of
previus concatenation of the body was not working, it seems that double quotes are needed to work...
It has nothing to do with single or double quotes.
In your original post you had:
$body = '<h2>sometext</h2>'
. '<div>sometext</div>'
. '<div> blabla' echo $num1 'blabla </div>'
. '<div> texttext' echo $num2 'texet </div>'
. '<div> texttext' echo $val 'text </div>';
the part:
'<div> blabla' echo $num1 'blabla </div>'
causes a syntax error (string, followed, without concatenation, by a call to "echo", followed, without concatenation, by a string...!
Somthing which works with single quotes:
$body = '<h2>sometext</h2>'
. '<div>sometext</div>'
. '<div> blabla' . $num1 . 'blabla </div>'
. '<div> texttext' . $num2 . 'texet </div>'
. '<div> texttext' . $val . 'text </div>';
And with double quotes you can do even shorter:
$body = "<h2>sometext</h2>"
. "<div>sometext</div>"
. "<div> blabla $num1 blabla </div>"
. "<div> texttext $num2 texet </div>"
. "<div> texttext $val text </div>";
Cheers,
Dom
Related
I am testing this sample of php code and I wonder why isn't giving the same result:
Head Title
. $theTitle .
instead of:
Head Title
Head Title
$theTitle = "Head Title";
define( "TEMPLATE", "<div class=\"well\">{{theTitle}}</div>");
$Template = str_replace("{{", '" . $', TEMPLATE);
$Template = str_replace("}}", ' . "', $Template);
echo $theTitle;
echo $Template;
How to achieve it?
Regards,
Marco
Use str_replace and replace "{{theTitle}}" with $theTitle.
$Template = str_replace("{{theTitle}}", $theTitle, TEMPLATE);
I have a simple echo statement that's supposed to display a link on the web page, but all it's doing is showing exactly "<a href=website.com>Link</a>" (without the quotes). To me, this should work with no problem. I thought maybe it's because the HTML for the website is in one file while the PHP is in another file.
foreach ($output as $output)
{
echo 'DATE: ' . $output['date'] . "\n";
echo 'TO: ' . $output['to'] . "\n";
echo 'FROM: ' . $output['from'] . "\n";
echo 'SUBJECT: ' . $output['subject'] . "\n";
echo "<a href=website.com>Link</a>\n\n";
}
Your loop isn't a valid one, you have $output as $output.
I also suggest printing $output before looping through it, to see if it even contains what you think it should.
In your 'a' tag, the actual URL needs to be in quotes
<a href='http://www.website.com'>
Im trying to embed an image within my message body but it ends up as an attachment
$mailer->Subject = APP_NAME . " - " . $name . " send you and Ad : " . $row['name'];
$mailer->IsHTML(true);
$mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDimes.png');
//footer
$footer = "Regards<br/><br/>";
$footer .= '<table style="width: 95%">';
$footer .= '<tr>';
$footer .= '<td>';
$footer .= "<strong><span style='font-size: 15px'>NamDimes Team</span></strong><br/>
NamDimes<br/>
Contact Number: " . APP_CONTACT . "<br/>
Email: " . APP_EMAIL . "<br/>
Website: " . APP_WEBSITE . "<br/>";
$footer .= '</td>';
$footer .= '<td style="text-align:right">';
$footer .= '<img src=\"cid:logoimg\" />';
$footer .= '</td>';
$footer .= '</tr>';
$footer .= '</table>';
$mailer->Body = $body . $footer;
$mailer->AltBody="This is text only alternative body.";
$mailer->AddAttachment('../' . $row['image_path'], $row['name'] . ".jpg");
i have set everything else, including the addresses, the mail gets send out, logo image that I want embed in the body gets attached as an attachment, anyone know why?
Don't use $mailer->AddEmbeddedImage, but directly add
<img src="http://.../images/namDiams.png" /> instead.
The mail length should be lighter... And it works.
EDIT
I don't know if it will help you but there is a little mistake here :
$mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDimes.png');
Should be
$mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDiames.png');//the last param the second 'a' was missing...
Another topic here
I can confirm that user2189925's answer does work. However, I use the absolute path since the location of the calling script is more likely to change than the location of the image.
e.g.
<img src="C:\folder\images\namDiames.png" />
Faced the same problem, then I decided to replace the following
<img src="img/example.jpg"
with
<img src= "https://mysitename.com/img/example.jpg">
and it worked.
just give path of your image to the mail body eg: (img src="../images/cat.jpeg) it will definately work
Here's the branch of code I've isolated...
if ( !is_search()
&& (get_option('option1')
&& !(is_page()
|| get_option('option2')
|| get_option('option3')
|| in_category('excludeme', $post )
)
)
)
I've inserted...
<?php print "Hi, Mom!\n"; exit; ?>
above and below this line to isolate the cause of the crash
Try breaking out your code into chunks to further isolate the problem:
This is obviously a WordPress template, and even though you may think the problem is not in the core WordPress code, you may need to put debugging output inside the core functions to find out exactly where the problem is happening. In other words, you need to go inside these functions calls to find out what is causing the problem - you might find a solution to your problem at that point.
<?php
echo '<pre>';
echo PHP_EOL . 'is_search' . PHP_EOL;
var_dump( is_search() );
echo PHP_EOL . 'get option 1' . PHP_EOL;
var_dump( get_option('option1') );
echo PHP_EOL . 'is_page' . PHP_EOL;
var_dump( is_page() );
echo PHP_EOL . 'get option 2' . PHP_EOL;
var_dump( get_option('option2') );
echo PHP_EOL . 'get option 3' . PHP_EOL;
var_dump( get_option('option3') );
echo PHP_EOL . 'in category' . PHP_EOL;
var_dump( in_category('excludeme', $post ) );
How do I retrieve the email address from an email with imap_open?
If the sender name is known I get the sender name instead of the email address if I use the 'from' parameter.
Code: http://gist.github.com/514207
$header = imap_headerinfo($imap_conn, $msgnum);
$fromaddr = $header->from[0]->mailbox . "#" . $header->from[0]->host;
I battled with this as well but the following works:
// Get email address
$header = imap_header($imap, $result); // get first mails header
echo '<p>Name: ' . $header->fromaddress . '<p>';
echo '<p>Email: ' . $header->senderaddress . '<p>';
I had used imap_fetch_overview() but the imap_header() gave me all the information I needed.
Worst case, you can parse the headers yourself with something like:
<?php
$headers=imap_fetchheader($imap, $msgid);
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $headers, $matches);
?>
$matches will contain 3 arrays:
$matches[0] are the full-lines (such as "To: user#user.com\r\n")
$matches[1] will be the header (such as "To")
$matches[2] will be the value (user#user.com)
Got this from: http://www.php.net/manual/en/function.imap-fetchheader.php#82339
Had same issue as you....had to piece it together, don't know why it's such gonzoware.
Untested example here:
$mbox = imap_open(....)
$MN=$MC->Nmsgs;
$overview=imap_fetch_overview($mbox,"1:$MN",0);
$size=sizeof($overview);
for($i=$size-1;$i>=0;$i--){
$val=$overview[$i];
$msg=$val->msgno;
$header = imap_headerinfo ( $mbox, $msg);
echo '<p>Name / Email Address: ' . $header->from[0]->personal ." ".
$header->from[0]->mailbox ."#". $header->from[0]->host. '<p></br>';
}
imap_close($mbox);
imap_fetch_overview could be what you're looking for: http://www.php.net/manual/en/function.imap-fetch-overview.php
An example of use can be found here: http://davidwalsh.name/gmail-php-imap, specifically
echo $overview[0]->from;
This function is simple, but has limitations. A more exhaustive version is in imap_headerinfo ( http://www.php.net/manual/en/function.imap-headerinfo.php ) which can return detailed arrays of all header data.
Had trouble until I spotted that the $header is an array of stdClass Objects. The following 2 lines worked:
$header=imap_fetch_overview($imap,$countClients,FT_UID);
$strAddress_Sender=$header[0]->from;
Full working code with an online example
Extract email addresses list from inbox using PHP and IMAP
inbox-using-php-and-imap
I think all you need is just to copy the script.
I am publishing two core functions of the code here as well (thanks to Eineki's comment)
function getAddressText(&$emailList, &$nameList, $addressObject) {
$emailList = '';
$nameList = '';
foreach ($addressObject as $object) {
$emailList .= ';';
if (isset($object->personal)) {
$emailList .= $object->personal;
}
$nameList .= ';';
if (isset($object->mailbox) && isset($object->host)) {
$nameList .= $object->mailbox . "#" . $object->host;
}
}
$emailList = ltrim($emailList, ';');
$nameList = ltrim($nameList, ';');
}
function processMessage($mbox, $messageNumber) {
echo $messageNumber;
// get imap_fetch header and put single lines into array
$header = imap_rfc822_parse_headers(imap_fetchheader($mbox, $messageNumber));
$fromEmailList = '';
$fromNameList = '';
if (isset($header->from)) {
getAddressText($fromEmailList, $fromNameList, $header->from);
}
$toEmailList = '';
$toNameList = '';
if (isset($header->to)) {
getAddressText($toEmailList, $toNameList, $header->to);
}
$body = imap_fetchbody($mbox, $messageNumber, 1);
$bodyEmailList = implode(';', extractEmail($body));
print_r(
',' . $fromEmailList . ',' . $fromNameList
. ',' . $toEmailList . ',' . $toNameList
. ',' . $bodyEmailList . "\n"
);
}