slawotrend Posted April 29, 2019 Share Posted April 29, 2019 (edited) Hello All, I am strugling with embending standard smtp code within the one I have. As I am using PHP ocassionaly I have problem with this. My code is to use mail function to send email through an form with email server installed on host system where the form and server are together. I want this code to adapt to sth that can login to google account and use google external smtp protocol to send the content of the form defined in $headers The problem is I am using self generated captcha that validates if the email is sent and all fields are filled (currently under tests what should be filled and not filled) and it works fine This is my code that works on a local host server: //Set up the email headers $headers = "From: $from_sender <$from_email>\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "Message-ID: <".time().rand(1,1000)."@".$_SERVER['SERVER_NAME'].">". "\r\n"; //Set up the email content $email_message = '<html><body>'; $email_message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; $email_message .= "<tr style='background: #eee;'><td><strong>Request</strong></td></tr>"; $email_message .= "<tr style='background: #fff;'><td></td><td>" . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Requested By:</strong> </td><td>" . strip_tags($_POST['query']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Assigned To:</strong> </td><td>" . strip_tags($_POST['assigned']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Contact Email:</strong> </td><td>" . strip_tags($_POST['senderemail']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>New Part Number:</strong> </td><td>" . strip_tags($_POST['partno']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Item Description:</strong> </td><td>" . strip_tags($_POST['item']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Expected Annual Volume:</strong> </td><td>" . strip_tags($_POST['volume']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 1:</strong> </td><td>" . strip_tags($_POST['sup1']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec1']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 2:</strong> </td><td>" . strip_tags($_POST['sup2']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec2']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 3:</strong> </td><td>" . strip_tags($_POST['sup3']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec3']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 4:</strong> </td><td>" . strip_tags($_POST['sup4']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec4']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Email Subject:</strong> </td><td>" . strip_tags($_POST['subject']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Message:</strong> </td><td>" . strip_tags($_POST['message']) . "</td></tr>"; $email_message .= "</body></html>"; // Required Fileds Validation if($req_by == "") { $submission_status = '<div class="vpb_info" align="left">Please enter your full name.</div>'; } elseif($from_email == "") { $submission_status = '<div class="vpb_info" align="left">Please enter your email address.</div>'; } elseif(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $from_email)) { $submission_status = '<div class="vpb_info" align="left">Please enter a valid email address.</div>'; } elseif($email_subject == "") { $submission_status = '<div class="vpb_info" align="left">Please enter the subject.</div>'; } elseif($part_no == "") { $submission_status = '<div class="vpb_info" align="left">Please enter the part number.</div>'; } elseif($email_message == "") { $submission_status = '<div class="vpb_info" align="left">Please enter your message.</div>'; } elseif($security_code == "") { $submission_status = '<div class="vpb_info" align="left">Please enter the security code.</div>'; } elseif(!isset($_SESSION['vpb_captcha_code'])) { $submission_status = '<div class="vpb_info" align="left">Sorry, missing session. Please refresh and try again.</div>'; } else { if(empty($_SESSION['vpb_captcha_code']) || strcasecmp($_SESSION['vpb_captcha_code'], $_POST['vpb_captcha_code']) != 0) { //Note: the captcha code is compared case insensitively. If you want case sensitive match, update the check above to strcmp() $submission_status = '<div class="info" align="left">Please enter the correct code.</div>'; } else { $mailer = mail($to_mail, $email_subject, $email_message, $headers); if ($mailer) { //Displays the success message when email message is sent $submission_status = "<div align='left' class='success'>The message has been sent successfully!</div>"; } else { //Displays an error message when email sending fails $submission_status = "<div align='left' class='info'>Sorry, your email could not be delievered. <br>Please try again or contact website admin.</div>"; } } } } ?> And would like sth like this in place of $mailer , but my way is not working The code I thought would be embeded into $mailer $mail->isSMTP(); $mail->Host = 'smtp.googlemail.com'; //gmail SMTP server $mail->SMTPAuth = true; $mail->Username = 'GMAIL_USERNAME'; //username $mail->Password = 'GMAIL_PASSWORD'; //password $mail->SMTPSecure = 'ssl'; $mail->Port = 465; //SMTP port Any idea how to adapt it to my code are welcomed and appreciated, Thanks Edited April 29, 2019 by slawotrend Quote Link to comment https://forums.phpfreaks.com/topic/308652-help-with-embedding-google-smtp-mail-function/ Share on other sites More sharing options...
chhorn Posted April 29, 2019 Share Posted April 29, 2019 Your post lacks relevant information Quote but my way is not working what should this mean to anyone? What error messages did you get? What is the actual and the expected behavior? Your code is incomplete, you did not say which return values you got or what you tested against. 1 Quote Link to comment https://forums.phpfreaks.com/topic/308652-help-with-embedding-google-smtp-mail-function/#findComment-1566358 Share on other sites More sharing options...
slawotrend Posted April 29, 2019 Author Share Posted April 29, 2019 6 hours ago, chhorn said: Your post lacks relevant information what should this mean to anyone? What error messages did you get? What is the actual and the expected behavior? Your code is incomplete, you did not say which return values you got or what you tested against. ok. Let's make it straigh what I am trying to achieve For now I am using this which wshould make my code complete <?php /******************************************************************************************************************** * Main PHP code. *********************************************************************************************************************/ session_start(); ob_start(); //ini_set('error_reporting', E_NONE); if(isset($_POST["submitted"]) && $_POST["submitted"] == 1) { //Read POST request params into global vars $to_mail = trim(strip_tags($_POST['sup1'])); // Function to use supplier email on the form $to_mail .= ','. trim(strip_tags($_POST['sup2'])); $to_mail .= ','. trim(strip_tags($_POST['sup3'])); $to_mail .= ','. trim(strip_tags($_POST['sup4'])); $quote_rec1 = ','. trim(strip_tags($_POST['rec1'])); $quote_rec2 = ','. trim(strip_tags($_POST['rec2'])); $quote_rec3 = ','. trim(strip_tags($_POST['rec3'])); $quote_rec4 = ','. trim(strip_tags($_POST['rec4'])); $req_by = trim(strip_tags($_POST['query'])); $assigned_to = trim(strip_tags($_POST['assigned'])); $from_email = trim(strip_tags($_POST['senderemail'])); $part_no = trim(strip_tags($_POST['partno'])); $item_name = trim(strip_tags($_POST['item'])); $volume_exp = trim(strip_tags($_POST['volume'])); $email_subject = trim(strip_tags($_POST['subject'])); $email_message = nl2br(trim(strip_tags($_POST['message']))); $security_code = trim(strip_tags($_POST['captcha_code'])); //Set up the email headers - the rest code above And instead of this $mailer = mail($to_mail, $email_subject, $email_message, $headers); Want to create sth like: $mailer = some function here to use google mail login for the code below to use phpMailer by embending the code below with mine so all can work with PHPMailer $mail->isSMTP(); $mail->Host = 'smtp.googlemail.com'; //gmail SMTP server $mail->SMTPAuth = true; $mail->Username = 'GMAIL_USERNAME'; //username $mail->Password = 'GMAIL_PASSWORD'; //password $mail->SMTPSecure = 'ssl'; $mail->Port = 465; //SMTP port My current form works fine for internal email server, however, need it to adapt to external where mail function is and I am slightly confused on how to proceed. Quote Link to comment https://forums.phpfreaks.com/topic/308652-help-with-embedding-google-smtp-mail-function/#findComment-1566359 Share on other sites More sharing options...
gizmola Posted April 29, 2019 Share Posted April 29, 2019 PHP's built in mail() function is designed to dump off mail to a system Mail Transfer Agent (MTA). It is not a full blown implementation of SMTP with support for SSL etc. To get that level of functionality, unless you plan to spend a good amount of time writing this to SMTP and Email format RFC standards, it is advisable to use a library. The traditional answer to this question involves pointing you to the PHPMailer library. PHPMailer source is here: https://github.com/PHPMailer/PHPMailer I am a big fan of things that have come from the Symfony project, and they have an alternative to PHPMailer named Swiftmailer. Swiftmailer is here: https://swiftmailer.symfony.com/docs/index.html Either library will do the job for you, so I'd advise you look at both and use whichever one you like the best, or that fits in with your overall project. Quote Link to comment https://forums.phpfreaks.com/topic/308652-help-with-embedding-google-smtp-mail-function/#findComment-1566360 Share on other sites More sharing options...
slawotrend Posted April 30, 2019 Author Share Posted April 30, 2019 (edited) 23 hours ago, gizmola said: PHP's built in mail() function is designed to dump off mail to a system Mail Transfer Agent (MTA). It is not a full blown implementation of SMTP with support for SSL etc. To get that level of functionality, unless you plan to spend a good amount of time writing this to SMTP and Email format RFC standards, it is advisable to use a library. The traditional answer to this question involves pointing you to the PHPMailer library. PHPMailer source is here: https://github.com/PHPMailer/PHPMailer I am a big fan of things that have come from the Symfony project, and they have an alternative to PHPMailer named Swiftmailer. Swiftmailer is here: https://swiftmailer.symfony.com/docs/index.html Either library will do the job for you, so I'd advise you look at both and use whichever one you like the best, or that fits in with your overall project. I will probably go for internal mail server with forwarding to Google of the conent from form (the actual email dumped by mail function) I believe this will be much easier to achieve than write complex PHP application I don't have enough experience with yet. I will maybe test it later for separate SMTP implementation, but setting up an internal email server is straightforward and forwarding message through the form or by setting the email forwarding on the server may be easily achieved. PHPMailer or Swiftmailer will do the job, sure but as my form required POST function to enter manually the content that is grabbed in an HTML form from the actual web form and works great for now with current code. I don't see in PHPMailer on how to create email body content that can use sth like POST than can actually bring the manually enetered data from the form to HTML formated email using sth like below. Every manual of PHPMailer has sth like this // Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; Which is manually enter predefined email embeded within the code. How to make it working the the dynamic form that is filled with data and is set to $mail as dynamically changed content grabbed into email body from the form and through the form? Edited April 30, 2019 by slawotrend Quote Link to comment https://forums.phpfreaks.com/topic/308652-help-with-embedding-google-smtp-mail-function/#findComment-1566384 Share on other sites More sharing options...
gizmola Posted May 2, 2019 Share Posted May 2, 2019 Your original source literally has this code: $email_message = '<html><body>'; $email_message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; $email_message .= "<tr style='background: #eee;'><td><strong>Request</strong></td></tr>"; $email_message .= "<tr style='background: #fff;'><td></td><td>" . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Requested By:</strong> </td><td>" . strip_tags($_POST['query']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Assigned To:</strong> </td><td>" . strip_tags($_POST['assigned']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Contact Email:</strong> </td><td>" . strip_tags($_POST['senderemail']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>New Part Number:</strong> </td><td>" . strip_tags($_POST['partno']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Item Description:</strong> </td><td>" . strip_tags($_POST['item']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Expected Annual Volume:</strong> </td><td>" . strip_tags($_POST['volume']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 1:</strong> </td><td>" . strip_tags($_POST['sup1']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec1']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 2:</strong> </td><td>" . strip_tags($_POST['sup2']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec2']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 3:</strong> </td><td>" . strip_tags($_POST['sup3']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec3']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 4:</strong> </td><td>" . strip_tags($_POST['sup4']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec4']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Email Subject:</strong> </td><td>" . strip_tags($_POST['subject']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Message:</strong> </td><td>" . strip_tags($_POST['message']) . "</td></tr>"; $email_message .= "</body></html>"; So, with phpMailer ---- $email_message = '<html><body>'; $email_message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; $email_message .= "<tr style='background: #eee;'><td><strong>Request</strong></td></tr>"; $email_message .= "<tr style='background: #fff;'><td></td><td>" . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Requested By:</strong> </td><td>" . strip_tags($_POST['query']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Assigned To:</strong> </td><td>" . strip_tags($_POST['assigned']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Contact Email:</strong> </td><td>" . strip_tags($_POST['senderemail']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>New Part Number:</strong> </td><td>" . strip_tags($_POST['partno']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Item Description:</strong> </td><td>" . strip_tags($_POST['item']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Expected Annual Volume:</strong> </td><td>" . strip_tags($_POST['volume']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 1:</strong> </td><td>" . strip_tags($_POST['sup1']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec1']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 2:</strong> </td><td>" . strip_tags($_POST['sup2']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec2']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 3:</strong> </td><td>" . strip_tags($_POST['sup3']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec3']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Supplier 4:</strong> </td><td>" . strip_tags($_POST['sup4']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Quotation Received:</strong> </td><td>" . strip_tags($_POST['rec4']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Email Subject:</strong> </td><td>" . strip_tags($_POST['subject']) . "</td></tr>"; $email_message .= "<tr style='background: #eee;'><td><strong>Message:</strong> </td><td>" . strip_tags($_POST['message']) . "</td></tr>"; $email_message .= "</body></html>"; $mail->Body = $email_message; The $mail->AltBody should be a version of the same data without the html, and just newline characters after each line. Quote Link to comment https://forums.phpfreaks.com/topic/308652-help-with-embedding-google-smtp-mail-function/#findComment-1566427 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.