My company is currently trying to streamline our process for submitting forms. I have read that what we are trying to do can be done with PHP or PHPMailer but I seem to have hit a roadblock.
What we are trying to do is open a fillable PDF in browser, complete it, and then click a button at the bottom to email it to a designated recipient.
I currently have a PHP script that allows me to email the blank document using PHPMailer and our server email service.
I have tried using the "AddStringAttachment" feature of PHPMailer:
<?php
require("../PHPMailer_5.2.3/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxx.xxx.org"; // SMTP server
$mail->From = "xxx#xxx.org";
$mail->AddAddress("xxx#xxx.org");
$mail->Subject = "Attachment Test";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
$mail->AddStringAttachment($string,'filename.pdf');
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
...but I was unable to find a way to define the string in a way that would send the completed form. Currently, if I put any data into the "$string" field the email fails.
Is this even possible? Or am I just spinning my wheels?
The problem might be with your mail server. Many servers don't like sending corrupted PDFs, which would be any PDF that doesn't look like a normal PDF - say, a string of English text.
Are you able to send any other kind of attachment, before I get into how to generate a PDF in PHP?
Related
I am sending emails using an online form via phpmailer and trying to use a for each loop to customize the body, specifically for an unsubscribe button. I am currently only using two of my personal emails with no encryption for testing purposes. I will add encryption once this actually starts to work as it should.
My php code:
$mail = new PHPMailer;
/*php mailer settings*/
//All settings for php mailer here - working fine, email sends
/*for each loop to send bcc to each email and customize body*/
//array of emails - really loading from database with while loop
$subs_email("email1#example.com","email2#example.com");
foreach ($subs_email as $email) {
$mail->addBCC($email);
$mail->Body = '<p>This is the body text</p>Unsubscribe';
}
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Using a variation of the code above the email will send (bcc) to the two emails in the array but all emails will get the same unsubscribe link/email address. Note email1# is used both times.
Email to email address 1:
Body text looks good.
Unsubscribe
Email to email address 2:
Body text looks good.
Unsubscribe
This is obviously not what I want. When I do testing and just echo out the for each loop text on a blank page it shows each custom unsubscribe link as it should, one for each email address.
Any help is appreciated, and let me know if you need more or .
working code created based on answer
Main issue was using addBCC instead of addAddress
foreach ($subs_email as $email) {
$mail->addAddress($email);
$mail->Body = '<p>This is the body text</p>Unsubscribe';
if (!$mail->send()) {
echo "Mailer Error" . $mail->ErrorInfo . '<br />';
break; //Abandon sending
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
}
This is how BCC works - the same message is sent to all recipients. You need to send a separate message to each recipient, as described in the mailing list example provided with PHPMailer.
For efficiency, you should create a single instance before your loop, iterate over your list, while setting the body differently for each message (the code you have is fine, if you want more flexibility perhaps use a templating system), send the message, then clear the recipient list so the next message only gets sent to one address. It also helps to use SMTP keepalive to increase throughput. The example script does most of this.
Your approach it's incorrect because you are sending "one" mail with an N recipients you have to use addAddress and clearAddresses() after sent
<?php
foreach($subs_email as $email){
$mail->addAddress($email);
$mail->Body = '<p>This is the body text</p>Unsubscribe';
$mail->send();
$mail->clearAddresses();
}
i used $mailer->ClearAllRecipients()
<?php
foreach($subs_email as $email){
$mailer->isHTML(true);
$mailer->CharSet = 'UTF-8';
$mailer->addAddress($email);
$mailer->Body = '<div>This is the body text</div>Unsubscribe';
$mailer->send();
$mailer->ClearAllRecipients()
}
So as stated in the title I wanna created a function different from mail to send mail(confusing ik)
So using mail () it sends the email from my server regardless of the headers I put so I need to create a function to send the actual email from a set email
Let's say I have an email
flameforgedinc#gmail.com
Lets say I have a bunch of emails in a mailing list
Now I have some code that's gonna use this function to email each one
So this code use's cMail ($to, $sub, $msg, $from);
And the email will appear to the user
From: flameforgedinc#gmail.com
And I actually want it to come from my email
If I use mail then it comes from my server and displays altervista00000 and I don't want the plus my server limits the mail() function to activation emails and I need to be able to send newsletters
Any ideas or workarounds??
My name is Pavel Tashev. I will try to help ;)
The easiest and more correct way from technical point of view is to use your own Mail server which hosts your email account and sends your emails. The purpose of PHP in that cases will be to tell the mail server to send email X to a list of emails Y, from an email account Z.
This will solve all your problems:
max allowed emails per hour;
sender name;
The good news is that you already have a Gmail account which is hosted by Google and you don't need to build your own mail server. You can use Google's mail server. Also, for the email sending I would advice you to use PHPMailer (url: https://github.com/PHPMailer/PHPMailer).
Here is how we can do all of this. Follow these steps:
Integrate PHPMailer in your project. If you use composer, that will be a straightforward process. If you don't, simply download the code and include this file PHPMailerAutoload.php in your project. Just follow the instructions on Github. It is really easy.
So, when you are ready with the installation of PHPMailer you must tell it to connect to your mail server. For Gmail and your email account this would look like this:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'flameforgedinc#gmail.com';
$mail->Password = 'PUT-YOUR-PASSWORD-HERE';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('flameforgedinc#gmail.com', 'YOU CAN PUT YOUR NAMES HERE IF YOU WANT');
The final step is to set up the recipient and the content of the email:
$mail->addAddress('some.user#example.net', 'Joe User');
$mail->addAddress('seconrd.user#example.com', 'The name is optional');
$mail->addReplyTo('flameforgedinc#gmail.com', 'YOU CAN PUT YOUR NAMES HERE IF YOU WANT');
$mail->isHTML(true);
$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';
now you will have to send the email:
!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I hope that will help.
I'm building a PHP based newsletter application. I know it is a good practice to send plain text version of the email beside the html as well. My question is how can I do this in practice? Just simply put one under the other? like:
<p style="font-weight:bold;">This is a newsletter!</p>
<p style="color:red;">
You can read awesome things here!
<br/>
check out us on the web!
</p>
This is a newsletter\r\n
You can read awesome things here!\r\n
check out us on the web: www.the-awesome-site.com
Won't these two interfere with each other? I mean if the mailer client can understand HTML, then the plain text with nearly the same content would be confusing at the end of the mail. Or if the client can't parse html then the user will see bothering raw HTML source before the human friendly plain text. Is there any way hide the useless one depending on the situation? I'm going to use PHPMailer if it is important.
PHPMailer is a great class to use because it detects when the email client doesn't support HTML.
Check out this code snippet. It should help a little.
require("PHPMailer-master/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.somemailserver.com";
$mail->SMTPAuth = false;
$mail->From = $someemail;
$mail->FromName = "Who ever";
$mail->AddAddress($email);
$mail->AddCC($anotheremail);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Some subject";
$mail->Body = "<html>Content goes here</html>";
//if the client doesn't support html email use
$mail->AltBody = "Content";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
By using the $mail->AltBody, you can send plain text emails. Hope this helps!
I am working with PHP Mailer to send the mails. It working fine in my localhost. And when i tested it from my Linux server, i am receiving mails to all as fine except hotmail . Please find the below code im running.
<?php require_once('PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->From = "no-repy#gmail.com"; //From Address -- CHANGE --
$mail->FromName = "myname"; //From Name -- CHANGE --
$mail->AddAddress('*****#hotmail.com'); //To Address -- CHANGE --
$mail->AddAddress('*****#gmail.com');
$mail->AddReplyTo("no-reply#gmail.com", "gmail"); //Reply-To Address -- CHANGE --
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(false); // set email format to HTML
$mail->Subject = "AuthSMTP Test";
$mail->Body = "AuthSMTP Test Message!";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
After running this im getting the response as
Message has been sent
But receiving mail to my Gmail a/c, not to the Hotmail.
Please help regarding this one.
Thanks in advance.
I had the same problem; indeed mail arrived at other destinations except hotmail.
It turns out MS*** recently decided to block emails coming from godaddy mailers (secureserver.net). And these emails are not even sent to spam/deleted folders for hotmail recipients, they are just sent to the ether. Believe it or not.
After calls and mails to both parties, where of course you get no useful info or solution, I just decided to bypass their mailers and instead use a 3rd party service: sendgrid.com. Didn't use their smtp solution, as GD seems to blocks external smtp (at least in shared hosting); but the sendgrid-php works just fine.
I am new to php and have been learning on the fly as I build pages. My current task is to be able to open a PDF in browser, complete the form, and submit the form upon clicking a button at the bottom of the form.
We would like the completed form to be sent without having to save a copy to the server. Having a two form cycle would not be horrible though.
I currently am able to send an email, but the script only sends the blank document.
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxx.xxx.xxx"; // SMTP server
$mail->From = "xxx#xxx.xxx";
$mail->AddAddress("xxx#xxx.xxx");
$mail->Subject = "Attachment Test";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
$mail->AddAttachment('tshirtorderform.pdf');
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I realize that I am telling it to retrieve the blank document, but I am coming up empty on how to attach the completed form.
Thanks.
There's a semi-undocumented AddStringAttachment() method in PHPmailer, which lets you attach directly from a string, instead of requiring an actual on-disk file:
http://phpmailer.worxware.com/index.php?pg=tutorial
search for "String Attachments". Beats me why they don't list in the Methods documentation.
I recommended AddStringAttachment() method also, but first You need to use TCPDF to create PDF's body.
I gave some hint in this topic: AddStringAttachment with PDF in PHPMailer not work