I have clients who send email messages to a specific e-mail address, and i have a script that will automatically update the MySQL table with that data from that e-mail and will publish it to a web page.
Images inserted into the emails cannot be seen on a web browser, because it was inserted into that email So the image reference in the source code has to be a hyperlink reference to the image and not cid:image001@1CCC701.5A896430 My issue is there may be one or more images inserted in that email to be published to the site, so how would i be able to differentiate and upload them to a specified FTP server folder and also get the uploaded file as use the hyperlink reference as a string to be replaced with the current as seen below.
In the body source, the image reference is:
<img width=183 height=183 id="Picture_x0020_1" src="cid:image001.png@01CCC701.5A896430">
How could i extract the image in the body of the e-mail, convert to a normal image type, upload it to the FTP server, and use the file on the FTP server as the image reference in the e-mail using str_replace ? Below is the code as i imagine it in my head. I've created it based off of researching functions that are supposed to do the job, but Could somebody help me find a solution to this?
<?php
//Specifies e-mail server inbox and connects to it
$mailbox = '{server.example.com/imap/novalidate-cert}INBOX';
$mbox = imap_open($mailbox, 'user', 'pass'); // log in to mail server
//Sets up FTP connection
$ftp_server = "ftp.example.com";
$ftp_user = "user";
$ftp_pass = "pass";
//Login with username and password
$conn = ftp_connect($ftp_server);
$login = ftp_login($conn, $ftp_user, $ftp_pass);
// the body of the e-mail body with its original formatting quoted-printable characters to an 8-bit character set
$body = imap_qprint(imap_fetchbody($mbox, $no, "1.2"));
//CID image names will be dynamic so i must use REGEX
$fileto = "/src="cid:(.*).png@(.*)/"";
#$fileto = "/src="cid:(.*).bmp(.*)/"";
#$fileto = "/src="cid:(.*).png(.*)/"";
#$fileto = "/src="cid:(.*).gif(.*)/"";
$target_f = "public_html/images/";
if(strpos($body, $fileto) {
$remote_f = ftp_nb_put($conn, $target_f, $fileto, FTP_ASCII);
} or die "Could not convert image!";
$new_img = str_replace($fileto, $remote_f, $body);
?>