Send email with attachment by SendMail()

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
actarus99
User
Posts: 38

Send email with attachment by SendMail()

Post by actarus99 »

Hi
I can't get any attachment from my server event Row_Updated: email worked as expected but when trying to attach an uploaded document email gone out with no attachment.

function Row_Updated($rsold, &$rsnew)
{

if (CurrentUserLevel() <> "11111110") {
$fromEmail = "noreply@tsdsdsdsdd.it";
$toEmail = "info@zxc.it";
$ccEmail = "";
$bccEmail = "";
$subject = "Update";

$mailContent = "New content";
$format = "html";
$charset = "";
$arAttachments[] = array("filename" => $rsnew["ata_document"], "content" => '');

$result =  SendEmail($fromEmail, $toEmail, $ccEmail, $bccEmail, $subject, $mailContent, $format, $charset, $smtpSecure = "", $arAttachments = [], $arImages = [], $arProperties = NULL);
}
}

arbei
User
Posts: 9787

Post by arbei »

Note that your $rsnew["ata_document"] is only the file name, you need to specify the path so that it can be found, or put the files in the global upload folder.


actarus99
User
Posts: 38

Post by actarus99 »

Tnx
the document is already in the global upload folder.

But the strange thing is when I make
var_dump($rsnew["ata_document"]);
the result is NULL.

Other fields are correctly displayed.

When inspecting the db via phpmyadmin i can see the name of the document in the field ata_document.

I don't understand


arbei
User
Posts: 9787

Post by arbei »

If the file was uploaded previously, there is no $rsnew["ata_document"], you should use $rsold["ata_document"] instead.


actarus99
User
Posts: 38

Post by actarus99 »

var_dump($rsold["ata_document"]);
is ok and show the name of the document

When I try:

$fileName = "https://www.xnmdfsf.it/madvv2/files/".$rsold["ata_document"]; 
var_dump($fileName);
die;

I get the correct path of my document BUT still no attachment in my email...

$arAttachments[] = array("filename" => $fileName, "content" => '');
$result =  SendEmail($fromEmail, $toEmail, $ccEmail, $bccEmail, $subject, $mailContent, $format, $charset, $smtpSecure = "", $arAttachments = [], $arImages = [], $arProperties = NULL);
}

arbei
User
Posts: 9787

Post by arbei »

You should not call the function with '$smtpSecure = "", $arAttachments = [], $arImages = [], $arProperties = NULL' which reset the arguments.


actarus99
User
Posts: 38

Post by actarus99 »

Right,
This code sends me the email with an attachment BUT when I try to open it: "File non in PDF format or corrupted"

$arAttachments[] = array("filename" => $fileName, "content" => 'application/pdf');
$result =  SendEmail($fromEmail, $toEmail, $ccEmail, $bccEmail, $subject, $mailContent, $format, $charset, $smtpSecure, $arAttachments);

arbei
User
Posts: 9787

Post by arbei »

The "content" is file content as string, not content type. If you use physical file (not content as string), you should remove the content setting and provide absolute path as file name.


actarus99
User
Posts: 38

Post by actarus99 »

Still nothing...
I use a physical file,
the value of $fileName is the correct absolute path of the file, I can verify it:

$fileName = "https://www.xnmdfsf.it/madvv2/files/".$rsold["ata_document"]; 
var_dump($fileName);
die;

I tried:

$arAttachments[] = array("filename" => $fileName);
$arAttachments[] = array("filename" => $fileName, "content" => '');
$arAttachments[] = array("filename" => $fileName, "content" => 'application/pdf'); (this is the only case wich the attachment is sent but unreadable)
$result =  SendEmail($fromEmail, $toEmail, $ccEmail, $bccEmail, $subject, $mailContent, $format, $charset, $smtpSecure, $arAttachments);

arbei
User
Posts: 9787

Post by arbei »

  1. Again, "content" is not content type. If you want to use content, you can use get the file as string by file_get_contents(), then the "filename" must the file name only, not absolute path.
  2. If you use "filename" without "content", the "filename" is the absolute physical file path on your server, not URL. (PHPMailer also uses file_get_contents() to load file, file_get_contents() can also open URL, but if the file is on your own server, there is no need to load by URL.)

actarus99
User
Posts: 38

Post by actarus99 »

Thank You for your patience,
I managed to send the email with the attachment exactly as you taught me.

Here's the code I inserted in Row_Inserted event:

if (CurrentUserLevel() <> "11111110") {
$fromEmail = "noreply@tsdsdsdsdd.it";

$fileName = $_SERVER['DOCUMENT_ROOT']."/madvv/files/".$rsnew["ata_document"]; 
// var_dump($fileName);
// die;

$toEmail = "info@zxc.it";
$ccEmail = "";
$bccEmail = "";
$subject = "New contact";
$mailContent = "New content";
$format = "html";
$charset = "";

$arAttachments[] = array("filename" => $fileName, "content"=> '');

$result =  SendEmail($fromEmail, $toEmail, $ccEmail, $bccEmail, $subject, $mailContent, $format, $charset, $smtpSecure, $arAttachments);

}

Post Reply