Page 1 of 1

Send email with attachment by SendMail()

Posted: Tue Sep 10, 2024 12:57 am
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);
}
}

Re: Send email with attachment by SendMail()

Posted: Tue Sep 10, 2024 9:00 am
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.


Re: Send email with attachment by SendMail()

Posted: Wed Sep 11, 2024 5:07 pm
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


Re: Send email with attachment by SendMail()

Posted: Wed Sep 11, 2024 5:13 pm
by arbei

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


Re: Send email with attachment by SendMail()

Posted: Wed Sep 11, 2024 5:39 pm
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);
}

Re: Send email with attachment by SendMail()

Posted: Wed Sep 11, 2024 6:29 pm
by arbei

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


Re: Send email with attachment by SendMail()

Posted: Wed Sep 11, 2024 9:14 pm
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);

Re: Send email with attachment by SendMail()

Posted: Wed Sep 11, 2024 10:52 pm
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.


Re: Send email with attachment by SendMail()

Posted: Thu Sep 12, 2024 12:58 am
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);

Re: Send email with attachment by SendMail()

Posted: Thu Sep 12, 2024 8:39 am
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.)

Re: Send email with attachment by SendMail()

Posted: Thu Sep 12, 2024 7:04 pm
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);

}