[SOLVED] Trouble sending emails with images and attachments

F.schettino

Italian
Please, help me, even if it is PHP not Fabrik and forgive me if they are stupid questions: I tried to solve searching on line, but I didn't find nothing useful.

The context is:
PHP form plugin
Process script: End of form submission
in which I coded to send an email which parameters (Body, Subject, ... ) are stored in a db table (by an other form).

I use:
$mailer = JFactory::getMailer();
$mailer->setSender($mittente_email);
$mailer->setSubject($oggetto);
$mailer->setBody($messaggio);
$mailer->isHTML();
$mailer->addRecipient($Destinatari_email_A);
$mailer->addCC($Destinatari_email_Cc);
$mailer->addBCC($Destinatari_email_Ccn);
$mailer->addAttachment($Allegati);

The mail is sent and it is correct, but I have 2 trouble:

1- The image in body isn't shown in the email: it is a blank image (with the text that says that there isn't the file in the path).
JCE Editor wrote:
<p><a><img src="images/sub/myfile.png" alt="logo 150x150" style="border: 0px;" title="logo" /></a></p>
but echo of body shows the link
http://www.mysite.com/sub/index.php/images/sub/myfile.png


2- The attachments are not sent.
I tried using
JURI::root()
$Allegati is​

and
JPATH_ROOT​
array(1) { [0]=> string(129) "/home/mhd-01/www.mysite.com/htdocs/sub/myfile.pdf"}

???
 
2. use JURI::root() and then $Allegati[0]

1. make sure your image is included with the absolute path. JCE has an option to insert with absolute or relative pathes (you can define a JCE profile to use absolute pathes)
 
Hi, troester.
I hope you will help me.

1
I put a lot of "echo" and "var_dump" statements in PHP to see what happens and I tried to write by hand HTML.
If in Body there is
<p><a><img src="images/sub/myfile.png" alt="logo 150x150" style="border: 0px;" title="logo" /></a></p>
echo $messaggio; shows
http://www.mysite.com/sub/index.php/images/sub/myfile.png
It would be right if it was http://www.mysite.com/sub/images/sub/myfile.png (without index.php/).
I tried also to insert
$messaggio = str_replace('index.php/', '', $messaggio);
before $mailer->setBody($messaggio);, but it didn't change; it seems me that index.php/ is added after str_replace.

2
Solved
$Allegati[] = JPATH_ROOT . "/" . $filepath;​
where $filepath is the value stored by a fileupload element, so images/sub/myfile.png

I didn't woks because it was $Allegati[] = JPATH_ROOT . $filepath; (without "/" ).

If you want, you can get information accessing to the site; I wrote all you need in "My sites".
 
I think JPATH_SITE (or JPATH_ROOT, I forget which) is what you need, which should be the full path to your J! folder. You don't want JURI, as that's giving you just the URI, eg the web address part, but for pointing to the attachment you need the full server path.

Code:
$mailer->addAttachment(JPATH_ROOT . DIRECTORY_SEPARATOR . $Allegati);

-- hugh
 
Hi, Hugh.

Probably you didn't see that I solved issue 2 about attachments.

Now my problem is only 1, about images in Body of email.
I am sure that you can help me.
 
To have the image (or any link) correctly in the mail it must be included with the complete URL (the mail doesn't know about any http://yoursite...)
So
if you are using a body text created via JCE you can force JCE to include links and images with absolute pathes (default is relative ones)
or
you can do string replace in your php and add JURI::root() to your img source

In any case the body must be
...
<img src="http://www.mysite.com/sub/images/sub/myfile.png" alt="logo 150x150" style="border: 0px;" title="logo" /></a>
...
before sending the mail.
 
As Troester says, any links you put in emails have to be full URL's. Joomla's editors (Tiny MCE, JCE, etc) by default do not do this - even if you specify a complete URL in the text, when you save it, they will remove the http://your.site/ part, leaving just the relative part, lie /images/foo.jpg. Obviously this doesn't work for emails, as the email is being read on a different server.

You can configure your editor to use full URL's, but it depends which editor you use. For the default TinyMCE, go to the J! plugin manager, edit the TinyMCE plugin, and towards the bottom of the list of settings you'll find "URL's: relative/absolute". You want "absolute". I can't remember if you can set up different profiles in TinyMCE.

In JCE, you can create per-user profiles, and set absolute for just one user.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top