Attempting to build a mass-texting (multiple cellphone recipients) code, using an html form and a php engine.
Side note: My pastor sends a daily text (using a cellphone app) to 300+ subscribers, but only some arrive. Some only receive one or two a month. Often he sends to me 5 to 10 times a day before I get one.
The "answers" I've seen for similar issues just confuse me more. I am a novice; I do not even completely comprehend the instructions for asking questions.
<!DOCTYPE php 5.3 PUBLIC >
<head>
<!---
// Double slash indicates comments
// This page url = http://edwardcnhistianchurch.edwardnc.org/Test-Kitchen/Mass_text/text_engine.php
// Form url = http://edwardcnhistianchurch.edwardnc.org/Test-Kitchen/Mass_text/text.html
--->
<Title>Text Engine</Title>
<src="http://edwardchristianchurch.edwardnc.org/Test-Kitchen/Mass_Text/default.config.php">
</head>
<?php
// Define variables
$EmailFrom = "2524025303#mms.uscc.net" ;
// Add additional addresses in next line 'enclosed' and separated by commas
$EmailTo = "2529169282#vtext.com,2524025305#mms.uscc.net, ";
$Subject = Trim(stripslashes($_POST['Subject']));
$Body = ($_POST['smsMessage']);
$From = Trim(stripslashes($_POST['From']));
$Password = Trim(stripslashes($_POST['Password']));
// <!--- SMTP server = yew.arvixe.com ; domain = mail.edwardnc.org --->;
$host = "yew.arvixe.com";
$username = "2524025305#edwardnc.org";
$SMTP_authentication = "Normal_Password";
$password = $Password;
$port = "587";
// SMTP Configuration
// enable SMTP authentication
$mail->SMTPAuth = true;
$mail->Host = $host;
$mail->Username = $username;
$mail->Password = $password;
$mail->Port = $port;
$mail->From = $EmailFrom;
$additional_parameters = '$mail' ;
// SendEmail
// $success = mail($EmailTo, $Subject, $Body, "From: <no_reply#edwardnc.org>" );
// Next line requires STMP_Authentication, line above works on another page;
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom" );
// Indicate success or failure
if ($success){
print "Message was sent to multiple recipients" ;
}
else {
print "OOPS! Something went wrong";
}
?>
</src="http://edwardchristianchurch.edwardnc.org/Test-Kitchen/Mass_Text/default.config.php">"
Warning: mail() [function.mail]: SMTP server response: 530 SMTP >authentication is required. in E:\HostingSpaces\eeeaim\edwardchristianchurch.org\wwwroot\Test-Kitchen\Mass_Text\text_engine.php on line 41
OOPS! Something went wrong
Just tell me how to correct line 41. or what to add elsewhere.
Please do not tell me to use phpmailer, unless you tell me exactly (in non technical terms) which lines to change and how, as it results in error 404 with no info as to what file/directory is missing.
Note: sender is constant. recipients are constant (subscriber list)
Your implementation is completely wrong. You are using the inbuilt PHP mail function which sends email using the sendmail protocol from your server mostly found in /usr/bin/sendmail for linux. If you need to send email using the SMTP protocol, please use extended libraries like PHPMailer or SwiftMailer. SMTP's are generally slow than API's but they are the most easily available option. This is the most widely used SMTP library for PHP. The link shows a demo and various options you can set with it. Good luck.
Related
I am trying to send two different emails to two different recipients using PHPmailer but only the second email is arriving.
My code:
/**
* This code shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "olaozias#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('olaozias#gmail.com', 'Department of Information Science');
//Set an alternative reply-to address
$mail->addReplyTo('olaozias#gmail.com', 'Department of Information Science');
//Set who the message is to be sent to
$mail->addAddress($email , 'Parent');
//Set the subject line
$mail->Subject = 'Student Attendance System';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->Body = 'Dear Parent \r\n This email is sent from the university of gondar , Department of information science to inform you that your child '. $firstname.' has been registered for semester '.$semister. ' in order to see your child attendance status and to communicate easily with our department use our attendance system. First download and install the mobile application which is attached in this email to your phone and use these login credentials to login to the system \r\n Your child Id: '.$student_no. '\r\n Password: '.$parent_pass.'\r\n Thank you for using our attendance system \r\n University of Gondar \r\n Department of Information Science ';
//Attach an image file
//$mail->addAttachment('AllCallRecorder.apk');
$mail->send();
$mail->ClearAddresses();
$mail->AddAddress($stud_email,'Student');
$mail->Subject = 'Student Attendance System';
$mail->Body = "email 2";
//send the message, check for errors
if (!$mail->Send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
echo '
<script type = "text/javascript">
alert("Mailer Error: " . $mail->ErrorInfo);
window.location = "student.php";
</script>
';
} else {
echo '
<script type = "text/javascript">
alert("student Added successfully and an Email the sent to email address provided");
window.location = "student.php";
</script>
';
//echo "Message sent!";
}
the second email is delivered successfully but the first one is not.
There are a couple of different possibilities. The fact that the second one is sending properly is a good indication that your code is working in general. Focusing on the first one, I'd suggest three things:
Add error checking to the first send() call. You have if (!$mail->Send()) {... on the second one, but you aren't checking the first one. You can use $mail->ErrorInfo as you have in a comment in the second part. (By the way, the $mail->ErrorInfo you have in the script tag will not work. Variables in single quoted strings like that will not be parsed, so you'll just get the literal string "$mail->ErrorInfo" there if there is an error.)
Add error checking to the first addAddress() call. PHPMailer will give you an error that you can check if the email address is invalid for some reason. As far as the code you've shown here, $email appears to be undefined, but so does $stud_email and you've said that one is working properly, so I assume those are both defined somewhere before the code that you've shown here, but a possible cause for this is that $email is undefined or doesn't have the value you expect it to.
The email is being sent, but not received. It's pretty easy for a message to be mis-identified as spam at multiple points between the sender and the receiver. This is more difficult to diagnose, but if you add the error checking to the first send() call and don't get any errors, you'll at least be able to rule that out as a point of failure.
you can do an array with de emails and subject.
$recipients = array(
'person1#domain.com' => 'Person One',
'person2#domain.com' => 'Person Two',
// ..
);
I am trying to get Batch Sending to work over SMTP, but even though I'm sending to multiple recipients and I've specified user variables (and the variables are getting replaced successfully in the email that is sent), every single recipient shows up in the To: field of the resulting messages at the receiver.
Per MailGun's documentation on Batch Sending...
Warning: It is important when using Batch Sending to also use Recipient Variables. This tells Mailgun to send each recipient an individual email with only their email in the to field. If they are not used, all recipients’ email addresses will show up in the to field for each recipient.
Here is an example of my SMTP headers...
To: foo#example.com, bar#example.com
X-Mailgun-Recipient-Variables: {
"foo#example.com":
{
"id":"12345",
"email":"foo#example.com",
"first_name":"Foo"
},
"bar#example.com":
{
"id":"45678",
"email":"bar#example.com",
"first_name":"Bar"
}
}
The resulting emails should only show one recipient per email in the To field. Am I missing something?
I started messing with this yesterday and I think I've found a solution.
The trick is to leave the To: addresses empty and add your recipients to the BCC line. Following that, add a custom header - To: %recipient%. $mail->send() will not complain, and the To: field in the received emails only show the individual recipient's email.
Code Sample:
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.host';
$mail->SMTPAuth = true;
$mail->Username = 'yourUserName';
$mail->Password = 'yourPassword';
$mail->SMTPSecure = 'tls';
$mail->From = 'email#server.net';
$mail->FromName = 'John Doe';
$mail->addBCC('foo1#bar.com');
$mail->addBCC('foo2#bar.com');
$headerLine = $mail->headerLine('X-Mailgun-Recipient-Variables', '{"foo1#bar.com": {"first":"FooBar1", "id":1}, "foo2#bar.com": {"first":"FooBar2", "id": 2}}');
$mail->addCustomHeader($headerLine);
$headerLine = $mail->headerLine('To','%recipient%');
$mail->addCustomHeader($headerLine);
$mail->Subject = 'Hello, %recipient.first%!';
$mail->Body = 'Hello %recipient.first%, Your ID is %recipient.id%.';
if(!$mail->send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message has been sent';
}
I, too, was unsuccessful in getting this to work. I put in a mailgun trouble ticket. Here's the essence of their response:
The "Warning" in our documentation is actually for API only, not SMTP. The reason for this is that when using the API we form/create the email and that allows our service to separate and create a new email per each recipient in the To: field. When using SMTP though, we simply relay the message with the content that was submitted to our service, we don't actually create the message MIME from scratch.
To work around this, you can input %recipient% in the To: field. This will create a separate message for each address specified in the "RCPT TO" during the SMTP session with our server. Now, this is where things get a bit tricky, as my unfamiliarity with ASP SMTP connector starts to show here. In my research I haven't found a way to specify a RCPT TO using the ASP SMTP connector. It seems to rely on what you input in the To and doesn't provide a way to specify a To: field and RCPT TO: field.
When I try to use %recipient% as the TO variable, its built-in method raises an error, "CDO.Message.1 error '8004020c' At least one recipient is required, but none were found." I'm not familiar with other mailers but I would be surprised if any would allow this construct.
Had the same requirement for a WordPress site, here is something I came with for those who need it :
class PHPMailerForMailgunBatch extends PHPMailer {
public function createHeader() {
$header = parent::createHeader();
$header = preg_replace( "/To: .*\n/", "To: %recipient%\n", $header );
return $header;
}
}
and then
global $phpmailer;
$phpmailer = new PHPMailerForMailgunBatch( true );
// Config mailgun SMTP here
$mailgunBatchHeader = "X-Mailgun-Recipient-Variables: " . json_encode( $yourMailgunBatchVariables );
wp_mail( $emails, $subject, $content, [
$mailgunBatchHeader
] );
Once upon a time, I had a nicely-functioning version of a mail script that used an old version of PHPmailer. Without really knowing PHP, I managed to push it reasonably far (in part by using Forms to Go and doing some mods), and got it to redirect to both custom "incomplete" and "thank you" pages, and getting it to refuse to send if the required fields weren't filled out.
I've had to go back to the drawing board a bit as now routing domain-based email through Gmail has become a quite dominant practice. In order to get there, I updated to the latest PHPmailer and finally got it functioning for Gmail setup after a lot of trial and error and research. But in the midst of that, I've lost some of that functionality. The script below works to send mail—but it sends even if the required fields (indeed, everything!) are all empty. So it needs to:
stop sending when the fields aren't filled in properly, and
hopefully redirect to my custom page rather than just display a generic message.
<?php
// CUSTOM: collect data from our web form
$name = $_REQUEST['name'];
$address = $_REQUEST['address'];
$tel = $_REQUEST['tel'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
//set required fields + redirect if incomplete
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
//date_default_timezone_set('Etc/UTC');
require 'mailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
// Gmail pulls from custom domain due to alteration of MX records
$mail->Host = 'server.mydomain.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "me#myemailaddress.com";
//Password to use for SMTP authentication
$mail->Password = "mypassword";
//Set who the message is to be sent from
$mail->setFrom('me#myemailaddress.com', 'web form');
//Set who the message is to be sent to
$mail->addAddress('me#myemailaddress.com', 'My Name');
//Set an alternative reply-to address
$mail->clearReplyTos();
$mail->addReplyTo($address);
//Set the subject line
$mail->Subject = $subject;
$mail->Body = "Name : $name\n\n"
. "Email : $address\n"
. "Telephone : $tel\n"
. "Message :\n\n $message\n"
. "";
//send the message, check for errors
if (!$mail->send()) {
$output .= "Mailer Error: ". $mail->ErrorInfo;
}
else
{
ob_clean();
header('Location: thankyou.php');
exit();
}
echo $output;
Can anyone help me understand how to edit this in order to get that functionality?
A honey pot would be a bonus too, but perhaps that's asking too much. [Edit: Looks like a honey pot solution in this thread will work: Receiving Spam from my Form Using PHPMailer ... will test tomorrow.]
<?php
/**
*/
if( !isset($_REQUEST['name'],$_REQUEST['address'],$_REQUEST['subject'],$_REQUEST['message'])
||
( !$_REQUEST['name'] || !$_REQUEST['address'] || !$_REQUEST['subject'] || !$_REQUEST['message'])
){
/**
* It means that the required field
* is not filled up all
* if any one filed is not required, then remove that from the above condition the whole $_REQUEST['var_not_required']
*/
/**
* you can display a error message or redirect
* 1. Error message
* 2. Redirect
*/
#1
die("Some of the fields are not filled up properly");
#2
header('location: http://yoururl');
/**
* Use exit to stop execution of the rest, to prevent sending or attempt to send
* with exit
*/
exit();
}
$name = $_REQUEST['name'];
$address = $_REQUEST['address'];
$tel = $_REQUEST['address'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
I posted earlier as an email and relocation issue, however after trying several things, and tracing (in production! yech!)
I have found the culprit - but have NO IDEA why or where to go from here.
you will see in the below code I include 'email.php' twice - because one receipt goes to the user, the other includes some meta data about the user and goes to support...
if i comment out the SECOND include, my redirect works, if i leave it in, it bombs...
the notify email is valid, and is the only thing that is different.
Im at a loss
PHP FORM PROCESSOR
...
// send two emails
$_emailTo = $email; // the email of the person requesting
$_emailBody = $text_body; // the stock response with things filled in
include ( 'email.php' );
$_emailTo = $notifyEmail; // the support email address
$_emailBody = $pretext.$text_body; // pretext added as meta data for support w/ same txt sent to user
//I make it this far in my trace - then nothing
include ( 'email.php' );
// relocate
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php" >';
exit;
PHP MAILER (email.php)
<?php
require 'phpmailer/class.phpmailer.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->IsSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Set the hostname of the mail server
$mail->Host = "mail.validmailserver.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 26;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "validusername";
//Password to use for SMTP authentication
$mail->Password = "pass1234";
//Set who the message is to be sent from
$mail->SetFrom('me#validmailserver.com', 'no-reply # this domain');
//Set an alternative reply-to address
//$mail->AddReplyTo('no-reply#validmailserver.com','Support');
//Set who the message is to be sent to
$mail->AddAddress( $_emailTo );
$mail->Subject = $_emailSubject;
$mail->MsgHTML( $_emailBody );
$_emailError = false;
//Send the message, check for errors
if( !$mail -> Send() ) {
$_emailError = true;
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
help - please
It's the require in email.php that is causing the problems. If you want to make the least change possible and make it work, change it to require_once. This will ensure "phpmailer/class.phpmailer.php" is only loaded once, therefore the class will only be defined once.
take the require() line from email.php and put it in the calling file
I have a problem I have been working on for about a week and can't find an answer. As a preface to this all, I have searched the internet for all sorts of things. There are a lot of answers for this problem, but none seem to be helping me.
I am somewhat new to PHP and a lot of the stuff I am asking for (been using it over the past few months). Let me get to the base of the problem:
I am on a school network with my own server set up in my dorm room. I am creating a website where I need to verify a user's email, but the basic PHP mail() function does not work. I have been told that I will need to use SMTP. So I decided the easiest and cheapest way was with Gmail SMTP. I created an account on Gmail called verify.impressions#gmail.com for this reason. Here is the code.
echo "starting mail sending";
require_once("pear/share/pear/Mail.php");
echo "1";
$from = "PersonA `<someone#gmail.com`>"; $to = "`<someoneElse#email.com`>"; $subject = "Activate your account"; $body = "Hey";
$host = "ssl://smtp.gmail.com"; $port = "465"; //also tried 587 $username = "someone#gmail.com"; $password = "password";
echo "2";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
echo "3";
$mailer_params['host'] = $host; $mailer_params['port'] = $port; $mailer_params['auth'] = true; $mailer_params['username'] = $username; $mailer_params['password'] = $password;
$smtp = Mail::factory('smtp', $mailer_params);
echo "4";
error_reporting(E_ALL);
echo "5";
if (PEAR::isError($smtp)) { die("Error : " . $smtp->getMessage()); }
echo "6";
$mail = $smtp->send($to, $headers, $body) or die("Something bad happened");
echo "7";
if (PEAR::isError($mail)) {echo($mail->getMessage();} else {echo(Message successfully sent!);}
echo "mail sent hopefully.";
So basically the code just stops at the line:
$mail = $smtp->send($to, %headers, $);
I have tried printing errors, but I just have no idea what to do now. Any tips and help is appreciated. Thanks!!
Use this class: http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html
The sample code I use:
require("smtp/smtp.php");
require("sasl/sasl.php");
$from = 'youraddress#gmail.com';
$to = 'some#email.com';
$smtp=new smtp_class;
$smtp->host_name="smtp.gmail.com";
$smtp->host_port='465';
$smtp->user='youraddress#gmail.com';
$smtp->password='XXXXXXXXX';
$smtp->ssl=1;
$smtp->debug=1; //0 here in production
$smtp->html_debug=1; //same
$smtp->SendMessage($from,array($to),array(
"From: $from",
"To: $to",
"Subject: Testing Manuel Lemos' SMTP class",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"Hello $to,\n\nIt is just to let you know that your SMTP class is working just fine.\n\nBye.\n"));
If you are you using Linux I recommend setting up postfix on your local machine and they asking that to relay the email forwards via an external SMTP service, in your case Gmail.
http://bookmarks.honewatson.com/2008/04/20/postfix-gmail-smtp-relay/
The reason is that your PHP script can timeout incase there's a delay contact Gmail. So you would use Postfix to queue the email on the local server, let the PHP script execution die and trust Postfix to send the email via Gmail.
If you are using Windows, I am sure you can find an equivalent SMTP relay application (should be built as a rough guess).
Many public networks block connections on the SMTP port to remote machines to stop spammers inside their network.
You have two choices:
Find a smtp server that uses a different port than 25
Use the official SMTP server of your school's network. There is always one since people need to send mails out.
I always use this to send mails using gmail as SMTP server.
Very Simple to configure