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()
}
Related
I am newbie with PHPMailer library , I want to get emails from my contact form But on submission of form I want sender also get a copy of mail With additional message as " thanks for registering with us." As I gone through PHP mailer examples We can use addCC();. But how to embed additional msg.
Could you please help me with this ?
Using CC or BCC will always result in identical message being sent to all; If you want the messages to be different for different recipients, you must send separate messages with different bodies. With PHPMailer:
$mail->addAddress('recipient1#example.com');
$mail->Body = "hello abc";
$mail->send();
$mail->clearAddresses();
$mail->addAddress('recipient2#example.com');
$mail->Body = "hello xyz";
$mail->send();
It's important to call clearAddresses as otherwise the second message will be sent to both recpients.
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.
We unable send an email from CRM at hotmail. We use google app as email host. Our custom applications PHP by our own developer. We able send email at every domain like yahoo, AOL and other domain except hotmail account.
We checked our PHPMailer script and also run google smtm but nothing is working. Please help me out of this issue.
<html>
<head>
<title>PHPMailer - Mail() basic test</title>
</head>
<body>`enter code here`
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
//$mail->IsHTML(true);
//$mail->CharSet = 'windows-1251';
$body = 'This is ....';
$mail->AddReplyTo("task#mytasker.com","Mytasker1");
//$mail->SetFrom('deep_m#outlook.com', 'Deep');
//$mail->SetFrom('saikat_mytasker#hotmail.com', "Saikat Mallick");
$mail->SetFrom("task#mytasker.com","Mytasker");
$address = "saikat#mytasker.com";
//$mail->AddAddress('saikat_mytasker#hotmail.com', "Saikat Mallick");
$mail->AddAddress('deep_m#outlook.com', 'Deep');
$mail->AddAddress($address, "Saikat Mallick");
$mail->Subject = "Mytasker 123 ...";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
This is probably hotmail servers are classifying your email content or servers as spam and is auto blocked. Though this is a little off topic but may i suggest to switch to third party email senders such as mandrill or mailjet. In this way, you will mitigate the risk of your content / servers being classified / marked as spam and ensure 99.99% hitting inbox rate
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?
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