User Tools

Site Tools


connect_to_mysql_database_for_email_send

Connect to mysql database for email send

CREATE TABLE your_table_name (
    id INT NOT NULL AUTO_INCREMENT,
    email VARCHAR(255) NOT NULL,
    company_name VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
);
INSERT INTO your_table_name (email, company_name)
VALUES ('john.doe@example.com', 'ABC Corp'),
       ('jane.smith@example.com', 'XYZ Ltd'),
       ('bob.johnson@example.com', '123 Industries');
#!/bin/bash
# Set the MySQL connection details
DB_USER="your_db_username"
DB_PASS="your_db_password"
DB_HOST="your_db_host"
DB_NAME="your_db_name"
 
# Set the email details
FROM="your_email_address"
SUBJECT="Your email subject"
BODY="Your email body"
 
# Connect to the database and retrieve the emails
EMAILS=$(mysql -u $DB_USER -p$DB_PASS -h $DB_HOST $DB_NAME -se "SELECT email FROM your_table_name")
 
# Send the email to all addresses
for email in $EMAILS; do
    echo $BODY | mail -s "$SUBJECT" $email -- -r $FROM
done

Меняем тело письма на html код:

#!/bin/bash  
 
# Set the MySQL connection details  
DB_USER="your_db_username"  
DB_PASS="your_db_password"  
DB_HOST="your_db_host"  
DB_NAME="your_db_name"  
 
# Set the email details  
FROM="your_email_address"  
SUBJECT="Your email subject"  
 
# Connect to the database and retrieve the emails  
EMAILS=$(mysql -u $DB_USER -p$DB_PASS -h $DB_HOST $DB_NAME -se "SELECT email FROM your_table_name")  
 
# Build the email body in HTML  
BODY="''<body>"  
BODY+="<h1>Your email title</h1>"  
BODY+="<p>Your email message goes here.</p>"  
BODY+="</body>''"  
 
# Send the email to all addresses  
for email in $EMAILS; do  
    echo $BODY | mail -s "$SUBJECT" $email -- -r $FROM -a "Content-Type: text/html"  
done
connect_to_mysql_database_for_email_send.txt · Last modified: 2023/04/06 10:28 (external edit)